/dev/tests/integration/testsuite/Magento/Catalog/Model/Product/Gallery/ProcessorTest.php

https://gitlab.com/axeltizon/magento-demopoweraccess · PHP · 173 lines · 128 code · 23 blank · 22 comment · 2 complexity · c3f42afbf81206e30be2c01566afd2c7 MD5 · raw file

  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Catalog\Model\Product\Gallery;
  7. use Magento\Framework\App\Filesystem\DirectoryList;
  8. /**
  9. * Test class for \Magento\Catalog\Model\Product\Gallery\Processor.
  10. * @magentoDataFixture Magento/Catalog/_files/product_simple.php
  11. */
  12. class ProcessorTest extends \PHPUnit_Framework_TestCase
  13. {
  14. /**
  15. * @var \Magento\Catalog\Model\Product\Gallery\Processor
  16. */
  17. protected $_model;
  18. /**
  19. * @var string
  20. */
  21. protected static $_mediaTmpDir;
  22. /**
  23. * @var string
  24. */
  25. protected static $_mediaDir;
  26. public static function setUpBeforeClass()
  27. {
  28. $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  29. /** @var \Magento\Framework\Filesystem\Directory\WriteInterface $mediaDirectory */
  30. $config = $objectManager->get('Magento\Catalog\Model\Product\Media\Config');
  31. $mediaDirectory = $objectManager->get(
  32. 'Magento\Framework\Filesystem'
  33. )->getDirectoryWrite(
  34. DirectoryList::MEDIA
  35. );
  36. self::$_mediaTmpDir = $mediaDirectory->getAbsolutePath($config->getBaseTmpMediaPath());
  37. self::$_mediaDir = $mediaDirectory->getAbsolutePath($config->getBaseMediaPath());
  38. $fixtureDir = realpath(__DIR__ . '/../../../_files');
  39. $mediaDirectory->create($config->getBaseTmpMediaPath());
  40. $mediaDirectory->create($config->getBaseMediaPath());
  41. copy($fixtureDir . "/magento_image.jpg", self::$_mediaTmpDir . "/magento_image.jpg");
  42. copy($fixtureDir . "/magento_image.jpg", self::$_mediaDir . "/magento_image.jpg");
  43. copy($fixtureDir . "/magento_small_image.jpg", self::$_mediaTmpDir . "/magento_small_image.jpg");
  44. }
  45. public static function tearDownAfterClass()
  46. {
  47. $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  48. /** @var \Magento\Catalog\Model\Product\Media\Config $config */
  49. $config = $objectManager->get('Magento\Catalog\Model\Product\Media\Config');
  50. /** @var \Magento\Framework\Filesystem\Directory\WriteInterface $mediaDirectory */
  51. $mediaDirectory = $objectManager->get(
  52. 'Magento\Framework\Filesystem'
  53. )->getDirectoryWrite(
  54. DirectoryList::MEDIA
  55. );
  56. if ($mediaDirectory->isExist($config->getBaseMediaPath())) {
  57. $mediaDirectory->delete($config->getBaseMediaPath());
  58. }
  59. if ($mediaDirectory->isExist($config->getBaseTmpMediaPath())) {
  60. $mediaDirectory->delete($config->getBaseTmpMediaPath());
  61. }
  62. }
  63. protected function setUp()
  64. {
  65. $this->_model = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
  66. 'Magento\Catalog\Model\Product\Gallery\Processor'
  67. );
  68. }
  69. public function testValidate()
  70. {
  71. $product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
  72. 'Magento\Catalog\Model\Product'
  73. );
  74. $this->assertTrue($this->_model->validate($product));
  75. $this->_model->getAttribute()->setIsRequired(true);
  76. try {
  77. $this->assertFalse($this->_model->validate($product));
  78. $this->_model->getAttribute()->setIsRequired(false);
  79. } catch (\Exception $e) {
  80. $this->_model->getAttribute()->setIsRequired(false);
  81. throw $e;
  82. }
  83. }
  84. public function testAddImage()
  85. {
  86. $product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
  87. 'Magento\Catalog\Model\Product'
  88. );
  89. $product->setId(1);
  90. $file = $this->_model->addImage($product, self::$_mediaTmpDir . '/magento_small_image.jpg');
  91. $this->assertStringMatchesFormat('/m/a/magento_small_image%sjpg', $file);
  92. }
  93. public function testUpdateImage()
  94. {
  95. $product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
  96. 'Magento\Catalog\Model\Product'
  97. );
  98. $product->setData('media_gallery', ['images' => ['image' => ['file' => 'magento_image.jpg']]]);
  99. $this->_model->updateImage($product, 'magento_image.jpg', ['label' => 'test label']);
  100. $this->assertEquals('test label', $product->getData('media_gallery/images/image/label'));
  101. }
  102. public function testRemoveImage()
  103. {
  104. $product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
  105. 'Magento\Catalog\Model\Product'
  106. );
  107. $product->setData('media_gallery', ['images' => ['image' => ['file' => 'magento_image.jpg']]]);
  108. $this->_model->removeImage($product, 'magento_image.jpg');
  109. $this->assertEquals('1', $product->getData('media_gallery/images/image/removed'));
  110. }
  111. public function testGetImage()
  112. {
  113. $product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
  114. 'Magento\Catalog\Model\Product'
  115. );
  116. $product->setData('media_gallery', ['images' => ['image' => ['file' => 'magento_image.jpg']]]);
  117. $this->assertEquals(
  118. ['file' => 'magento_image.jpg'],
  119. $this->_model->getImage($product, 'magento_image.jpg')
  120. );
  121. }
  122. public function testClearMediaAttribute()
  123. {
  124. /** @var $product \Magento\Catalog\Model\Product */
  125. $product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
  126. 'Magento\Catalog\Model\Product'
  127. );
  128. $product->setData(['image' => 'test1', 'small_image' => 'test2', 'thumbnail' => 'test3']);
  129. $this->assertNotEmpty($product->getData('image'));
  130. $this->_model->clearMediaAttribute($product, 'image');
  131. $this->assertNull($product->getData('image'));
  132. $this->assertNotEmpty($product->getData('small_image'));
  133. $this->assertNotEmpty($product->getData('thumbnail'));
  134. $this->_model->clearMediaAttribute($product, ['small_image', 'thumbnail']);
  135. $this->assertNull($product->getData('small_image'));
  136. $this->assertNull($product->getData('thumbnail'));
  137. }
  138. public function testSetMediaAttribute()
  139. {
  140. /** @var $product \Magento\Catalog\Model\Product */
  141. $product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
  142. 'Magento\Catalog\Model\Product'
  143. );
  144. $this->_model->setMediaAttribute($product, 'image', 'test1');
  145. $this->assertEquals('test1', $product->getData('image'));
  146. $this->_model->setMediaAttribute($product, ['non-exist-image-attribute', 'small_image'], 'test');
  147. $this->assertNull($product->getData('non-exist-image-attribute'));
  148. $this->assertEquals('test', $product->getData('small_image'));
  149. }
  150. }