AbstractWriterTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. <?php
  2. /**
  3. * This file is part of the PHPExiftool package.
  4. *
  5. * (c) Alchemy <support@alchemy.fr>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace PHPExiftool\Test;
  11. use PHPExiftool\Driver;
  12. use PHPExiftool\Writer;
  13. use PHPExiftool\Reader;
  14. use PHPExiftool\RDFParser;
  15. abstract class AbstractWriterTest extends \PHPUnit_Framework_TestCase
  16. {
  17. /**
  18. * @var Writer
  19. */
  20. protected $object;
  21. protected $in;
  22. protected $inWithICC;
  23. protected $inPlace;
  24. protected $out;
  25. protected function setUp()
  26. {
  27. $this->object = new Writer($this->getExiftool());
  28. $this->in = __DIR__ . '/../../../files/ExifTool.jpg';
  29. $this->inWithICC = __DIR__ . '/../../../files/pixelWithIcc.jpg';
  30. $this->out = __DIR__ . '/../../../files/ExifTool_erased.jpg';
  31. $this->inPlace = __DIR__ . '/../../../files/ExifToolCopied.jpg';
  32. copy($this->in, $this->inPlace);
  33. }
  34. protected function tearDown()
  35. {
  36. if (file_exists($this->out) && is_writable($this->out)) {
  37. unlink($this->out);
  38. }
  39. if (file_exists($this->inPlace) && is_writable($this->inPlace)) {
  40. unlink($this->inPlace);
  41. }
  42. }
  43. /**
  44. * @covers PHPExiftool\Writer::setMode
  45. * @covers PHPExiftool\Writer::isMode
  46. */
  47. public function testSetMode()
  48. {
  49. $this->object->setMode(Writer::MODE_EXIF2IPTC, true);
  50. $this->assertTrue($this->object->isMode(Writer::MODE_EXIF2IPTC));
  51. $this->object->setMode(Writer::MODE_XMP2EXIF, true);
  52. $this->assertTrue($this->object->isMode(Writer::MODE_XMP2EXIF));
  53. $this->object->setMode(Writer::MODE_EXIF2IPTC, false);
  54. $this->assertFalse($this->object->isMode(Writer::MODE_EXIF2IPTC));
  55. $this->object->setMode(Writer::MODE_XMP2EXIF, true);
  56. $this->assertTrue($this->object->isMode(Writer::MODE_XMP2EXIF));
  57. }
  58. /**
  59. * @covers PHPExiftool\Writer::copy
  60. */
  61. public function testCopy()
  62. {
  63. $metadatas = new Driver\Metadata\MetadataBag();
  64. $this->object->erase(true, true);
  65. $changedFiles = $this->object->write($this->inWithICC, $metadatas, $this->out);
  66. $this->assertEquals(1, $changedFiles);
  67. $reader = new Reader($this->getExiftool(), new RDFParser());
  68. $metadatasRead = $reader->files($this->out)->first()->getMetadatas();
  69. $this->assertFalse(is_object($metadatasRead->get('IPTC:ObjectName')));
  70. $this->object->copy($this->in, $this->out);
  71. $metadatasRead = $reader->files($this->out)->first()->getMetadatas();
  72. $this->assertTrue(is_object($metadatasRead->get('IPTC:ObjectName')));
  73. $this->assertEquals("Test IPTC picture", $metadatasRead->get('IPTC:ObjectName')->getValue()->asString());
  74. }
  75. /**
  76. * @covers PHPExiftool\Writer::setModule
  77. * @covers PHPExiftool\Writer::hasModule
  78. */
  79. public function testSetModule()
  80. {
  81. $this->assertFalse($this->object->hasModule(Writer::MODULE_MWG));
  82. $this->object->setModule(Writer::MODULE_MWG, true);
  83. $this->assertTrue($this->object->hasModule(Writer::MODULE_MWG));
  84. $this->object->setModule(Writer::MODULE_MWG, false);
  85. $this->assertFalse($this->object->hasModule(Writer::MODULE_MWG));
  86. }
  87. /**
  88. * @covers PHPExiftool\Writer::write
  89. * @covers PHPExiftool\Writer::erase
  90. */
  91. public function testEraseWithoutICC()
  92. {
  93. $uniqueId = 'UNI-QUE-ID';
  94. $metadatas = new Driver\Metadata\MetadataBag();
  95. $metadatas->add(new Driver\Metadata\Metadata(new Driver\Tag\IPTC\UniqueDocumentID(), new Driver\Value\Mono($uniqueId)));
  96. $metadatas->add(new Driver\Metadata\Metadata(new Driver\Tag\XMPExif\ImageUniqueID(), new Driver\Value\Mono($uniqueId)));
  97. $this->object->erase(true, false);
  98. $changedFiles = $this->object->write($this->inWithICC, $metadatas, $this->out);
  99. $this->assertEquals(1, $changedFiles);
  100. $reader = new Reader($this->getExiftool(), new RDFParser());
  101. $this->assertGreaterThan(200, count($reader->files($this->in)->first()->getMetadatas()));
  102. $reader = new Reader($this->getExiftool(), new RDFParser());
  103. $this->assertGreaterThan(4, count($reader->files($this->out)->first()->getMetadatas()));
  104. $this->assertLessThan(30, count($reader->files($this->out)->first()->getMetadatas()));
  105. $acceptedMetas = array(
  106. 'Exiftool:\w+',
  107. 'System:\w+',
  108. 'File:\w+',
  109. 'Composite:\w+',
  110. 'IPTC:CodedCharacterSet',
  111. 'IPTC:EnvelopeRecordVersion',
  112. 'IPTC:UniqueDocumentID',
  113. 'IPTC:ApplicationRecordVersion',
  114. 'Photoshop:IPTCDigest',
  115. 'XMP-x:XMPToolkit',
  116. 'XMP-exif:ImageUniqueID',
  117. 'Adobe:DCTEncodeVersion',
  118. 'Adobe:APP14Flags0',
  119. 'Adobe:APP14Flags1',
  120. 'Adobe:ColorTransform',
  121. );
  122. foreach ($reader->files($this->out)->first()->getMetadatas() as $meta) {
  123. $found = false;
  124. foreach ($acceptedMetas as $accepted) {
  125. if (preg_match('/' . $accepted . '/i', $meta->getTag()->getTagname())) {
  126. $found = true;
  127. break;
  128. }
  129. }
  130. if ( ! $found) {
  131. $this->fail(sprintf('Unexpected meta %s found', $meta->getTag()->getTagname()));
  132. }
  133. }
  134. }
  135. public function testEraseWithICC()
  136. {
  137. $uniqueId = 'UNI-QUE-ID';
  138. $metadatas = new Driver\Metadata\MetadataBag();
  139. $metadatas->add(new Driver\Metadata\Metadata(new Driver\Tag\IPTC\UniqueDocumentID(), new Driver\Value\Mono($uniqueId)));
  140. $metadatas->add(new Driver\Metadata\Metadata(new Driver\Tag\XMPExif\ImageUniqueID(), new Driver\Value\Mono($uniqueId)));
  141. $this->object->erase(true, true);
  142. $changedFiles = $this->object->write($this->inWithICC, $metadatas, $this->out);
  143. $this->assertEquals(1, $changedFiles);
  144. $reader = new Reader($this->getExiftool(), new RDFParser());
  145. $this->assertGreaterThan(200, count($reader->files($this->in)->first()->getMetadatas()));
  146. $reader = new Reader($this->getExiftool(), new RDFParser());
  147. $this->assertGreaterThan(4, count($reader->files($this->out)->first()->getMetadatas()));
  148. $acceptedMetas = array(
  149. 'Exiftool:\w+',
  150. 'System:\w+',
  151. 'File:\w+',
  152. 'Composite:\w+',
  153. 'IPTC:CodedCharacterSet',
  154. 'ICC-header:\w+',
  155. 'IPTC:EnvelopeRecordVersion',
  156. 'IPTC:UniqueDocumentID',
  157. 'IPTC:ApplicationRecordVersion',
  158. 'Photoshop:IPTCDigest',
  159. 'XMP-x:XMPToolkit',
  160. 'XMP-exif:ImageUniqueID',
  161. 'Adobe:DCTEncodeVersion',
  162. 'Adobe:APP14Flags0',
  163. 'Adobe:APP14Flags1',
  164. 'Adobe:ColorTransform',
  165. );
  166. foreach ($reader->files($this->out)->first()->getMetadatas() as $meta) {
  167. $found = false;
  168. foreach ($acceptedMetas as $accepted) {
  169. if (preg_match('/' . $accepted . '/i', $meta->getTag()->getTagname())) {
  170. $found = true;
  171. break;
  172. }
  173. }
  174. if ( ! $found) {
  175. $this->fail(sprintf('Unexpected meta %s found', $meta->getTag()->getTagname()));
  176. }
  177. }
  178. }
  179. /**
  180. * @covers PHPExiftool\Writer::write
  181. */
  182. public function testWrite()
  183. {
  184. $metadatas = new Driver\Metadata\MetadataBag();
  185. $metadatas->add(new Driver\Metadata\Metadata(new Driver\Tag\IPTC\ObjectName(), new Driver\Value\Mono('Beautiful Object')));
  186. $metadatas->add(new Driver\Metadata\Metadata(new Driver\Tag\IPTC\ObjectName(), new Driver\Value\Mono('Beautiful Object')));
  187. $metadatas->add(new Driver\Metadata\Metadata(new Driver\Tag\XMPIptcExt\PersonInImage(), new Driver\Value\Multi(array('Romain', 'Nicolas'))));
  188. $changedFiles = $this->object->write($this->in, $metadatas, $this->out);
  189. $this->assertEquals(1, $changedFiles);
  190. $reader = new Reader($this->getExiftool(), new RDFParser());
  191. $metadatasRead = $reader->files($this->out)->first()->getMetadatas();
  192. $this->assertGreaterThan(200, count($metadatasRead));
  193. $this->assertEquals('Beautiful Object', $metadatasRead->get('IPTC:ObjectName')->getValue()->asString());
  194. $this->assertEquals(array('Romain', 'Nicolas'), $metadatasRead->get('XMP-iptcExt:PersonInImage')->getValue()->asArray());
  195. }
  196. /**
  197. * @covers PHPExiftool\Writer::write
  198. */
  199. public function testWriteInPlace()
  200. {
  201. $metadatas = new Driver\Metadata\MetadataBag();
  202. $metadatas->add(new Driver\Metadata\Metadata(new Driver\Tag\IPTC\ObjectName(), new Driver\Value\Mono('Beautiful Object')));
  203. $metadatas->add(new Driver\Metadata\Metadata(new Driver\Tag\IPTC\ObjectName(), new Driver\Value\Mono('Beautiful Object')));
  204. $metadatas->add(new Driver\Metadata\Metadata(new Driver\Tag\XMPIptcExt\PersonInImage(), new Driver\Value\Multi(array('Romain', 'Nicolas'))));
  205. $changedFiles = $this->object->write($this->inPlace, $metadatas);
  206. $this->assertEquals(1, $changedFiles);
  207. $reader = new Reader($this->getExiftool(), new RDFParser());
  208. $metadatasRead = $reader->files($this->inPlace)->first()->getMetadatas();
  209. $this->assertGreaterThan(200, count($metadatasRead));
  210. $this->assertEquals('Beautiful Object', $metadatasRead->get('IPTC:ObjectName')->getValue()->asString());
  211. $this->assertEquals(array('Romain', 'Nicolas'), $metadatasRead->get('XMP-iptcExt:PersonInImage')->getValue()->asArray());
  212. }
  213. /**
  214. * @covers PHPExiftool\Writer::write
  215. */
  216. public function testWriteInPlaceErased()
  217. {
  218. $metadatas = new Driver\Metadata\MetadataBag();
  219. $metadatas->add(new Driver\Metadata\Metadata(new Driver\Tag\IPTC\ObjectName(), new Driver\Value\Mono('Beautiful Object')));
  220. $metadatas->add(new Driver\Metadata\Metadata(new Driver\Tag\IPTC\ObjectName(), new Driver\Value\Mono('Beautiful Object')));
  221. $metadatas->add(new Driver\Metadata\Metadata(new Driver\Tag\XMPIptcExt\PersonInImage(), new Driver\Value\Multi(array('Romain', 'Nicolas'))));
  222. $this->object->erase(true);
  223. $changedFiles = $this->object->write($this->inPlace, $metadatas);
  224. $this->assertEquals(1, $changedFiles);
  225. $reader = new Reader($this->getExiftool(), new RDFParser());
  226. $metadatasRead = $reader->files($this->inPlace)->first()->getMetadatas();
  227. $this->assertLessThan(50, count($metadatasRead));
  228. $this->assertEquals('Beautiful Object', $metadatasRead->get('IPTC:ObjectName')->getValue()->asString());
  229. $this->assertEquals(array('Romain', 'Nicolas'), $metadatasRead->get('XMP-iptcExt:PersonInImage')->getValue()->asArray());
  230. }
  231. /**
  232. * @covers PHPExiftool\Writer::write
  233. * @covers PHPExiftool\Exception\InvalidArgumentException
  234. * @expectedException PHPExiftool\Exception\InvalidArgumentException
  235. */
  236. public function testWriteFail()
  237. {
  238. $this->object->write('ici', new Driver\Metadata\MetadataBag());
  239. }
  240. /**
  241. * @covers PHPExiftool\Writer::addMetadatasArg
  242. */
  243. public function testAddMetadatasArg()
  244. {
  245. $metadatas = new Driver\Metadata\MetadataBag();
  246. $metadatas->add(new Driver\Metadata\Metadata(new Driver\Tag\IPTC\ObjectName(), new Driver\Value\Mono('Beautiful Object')));
  247. $metadatas->add(new Driver\Metadata\Metadata(new Driver\Tag\XMPIptcExt\PersonInImage(), new Driver\Value\Multi(array('Romain', 'Nicolas'))));
  248. $writer = new WriterTester($this->getExiftool());
  249. $this->assertNotContains('@', trim($writer->getSyncCommandTester()));
  250. $writer->setMode(WriterTester::MODE_EXIF2IPTC, true);
  251. $this->assertContains('@ exif2iptc.args', $writer->getSyncCommandTester());
  252. $writer->setMode(WriterTester::MODE_EXIF2XMP, true);
  253. $this->assertContains('@ exif2xmp.args', $writer->getSyncCommandTester());
  254. $writer->setMode(WriterTester::MODE_IPTC2EXIF, true);
  255. $this->assertContains('@ iptc2exif', $writer->getSyncCommandTester());
  256. $writer->setMode(WriterTester::MODE_IPTC2XMP, true);
  257. $this->assertContains('@ iptc2xmp', $writer->getSyncCommandTester());
  258. $writer->setMode(WriterTester::MODE_GPS2XMP, true);
  259. $this->assertContains('@ gps2xmp', $writer->getSyncCommandTester());
  260. $writer->setMode(WriterTester::MODE_PDF2XMP, true);
  261. $this->assertContains('@ pdf2xmp', $writer->getSyncCommandTester());
  262. $writer->setMode(WriterTester::MODE_XMP2PDF, true);
  263. $this->assertContains('@ xmp2pdf', $writer->getSyncCommandTester());
  264. $writer->setMode(WriterTester::MODE_XMP2GPS, true);
  265. $this->assertContains('@ xmp2gps', $writer->getSyncCommandTester());
  266. $writer->setMode(WriterTester::MODE_XMP2EXIF, true);
  267. $this->assertContains('@ xmp2exif', $writer->getSyncCommandTester());
  268. $writer->setMode(WriterTester::MODE_XMP2IPTC, true);
  269. $this->assertContains('@ xmp2iptc', $writer->getSyncCommandTester());
  270. $writer->setMode(WriterTester::MODE_XMP2IPTC, false);
  271. $this->assertNotContains('@ xmp2iptc', $writer->getSyncCommandTester());
  272. $writer->setModule(WriterTester::MODULE_MWG, true);
  273. $this->assertContains('-use MWG', $writer->addMetadatasArgTester($metadatas));
  274. $writer->setModule(WriterTester::MODULE_MWG, false);
  275. $this->assertNotContains('-use MWG', $writer->addMetadatasArgTester($metadatas));
  276. $this->assertRegExp("/\ -XMP-iptcExt:PersonInImage=['\"]Nicolas['\"]/", $writer->addMetadatasArgTester($metadatas));
  277. }
  278. abstract protected function getExiftool();
  279. }
  280. class WriterTester extends Writer
  281. {
  282. public function addMetadatasArgTester($metadatas)
  283. {
  284. return parent::addMetadatasArg($metadatas);
  285. }
  286. public function getSyncCommandTester()
  287. {
  288. return parent::getSyncCommand();
  289. }
  290. }