AbstractTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Catalog\Model;
  7. class AbstractTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * Stub class name for class under test
  11. */
  12. const STUB_CLASS = 'Magento_Catalog_Model_AbstractModel_Stub';
  13. /**
  14. * @var \Magento\Catalog\Model\AbstractModel
  15. */
  16. protected $_model;
  17. /**
  18. * Flag is stub class was created
  19. *
  20. * @var bool
  21. */
  22. protected static $_isStubClass = false;
  23. protected function setUp()
  24. {
  25. if (!self::$_isStubClass) {
  26. $this->getMockForAbstractClass(
  27. \Magento\Catalog\Model\AbstractModel\Stub::class,
  28. [],
  29. self::STUB_CLASS,
  30. false
  31. );
  32. self::$_isStubClass = true;
  33. }
  34. $this->_model = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(self::STUB_CLASS);
  35. $resourceProperty = new \ReflectionProperty(get_class($this->_model), '_resourceName');
  36. $resourceProperty->setAccessible(true);
  37. $resourceProperty->setValue($this->_model, \Magento\Catalog\Model\ResourceModel\Product::class);
  38. $collectionProperty = new \ReflectionProperty(get_class($this->_model), '_collectionName');
  39. $collectionProperty->setAccessible(true);
  40. $collectionProperty->setValue($this->_model, \Magento\Catalog\Model\ResourceModel\Product\Collection::class);
  41. }
  42. /**
  43. * @covers \Magento\Catalog\Model\AbstractModel::lockAttribute
  44. * @covers \Magento\Catalog\Model\AbstractModel::unlockAttribute
  45. * @covers \Magento\Catalog\Model\AbstractModel::unlockAttributes
  46. * @covers \Magento\Catalog\Model\AbstractModel::getLockedAttributes
  47. * @covers \Magento\Catalog\Model\AbstractModel::hasLockedAttributes
  48. * @covers \Magento\Catalog\Model\AbstractModel::isLockedAttribute
  49. */
  50. public function testLockedAttributeApi()
  51. {
  52. $this->assertEquals([], $this->_model->getLockedAttributes());
  53. $this->assertFalse($this->_model->hasLockedAttributes());
  54. $this->assertFalse($this->_model->isLockedAttribute('some_code'));
  55. $this->_model->lockAttribute('code');
  56. $this->assertTrue($this->_model->isLockedAttribute('code'));
  57. $this->assertEquals(['code'], $this->_model->getLockedAttributes());
  58. $this->assertTrue($this->_model->hasLockedAttributes());
  59. $this->_model->unlockAttribute('code');
  60. $this->assertFalse($this->_model->isLockedAttribute('code'));
  61. $this->_model->lockAttribute('code1');
  62. $this->_model->lockAttribute('code2');
  63. $this->_model->unlockAttributes();
  64. $this->assertEquals([], $this->_model->getLockedAttributes());
  65. $this->assertFalse($this->_model->hasLockedAttributes());
  66. }
  67. public function testSetData()
  68. {
  69. // locked filter on setting all
  70. $this->_model->lockAttribute('key1');
  71. $this->_model->setData(['key1' => 'value1', 'key2' => 'value2']);
  72. $this->assertEquals(['key2' => 'value2'], $this->_model->getData());
  73. // locked filter per setting one
  74. $this->_model->setData('key1', 'value1');
  75. $this->_model->setData('key3', 'value3');
  76. $this->assertEquals(['key2' => 'value2', 'key3' => 'value3'], $this->_model->getData());
  77. // set one with read only
  78. $this->_model->unlockAttributes()->unsetData();
  79. $this->_model->setIsReadonly(true);
  80. $this->_model->setData(uniqid(), uniqid());
  81. $this->assertEquals([], $this->_model->getData());
  82. }
  83. public function testUnsetData()
  84. {
  85. $data = ['key1' => 'value1', 'key2' => 'value2'];
  86. $this->_model->setData($data);
  87. // unset one locked
  88. $this->_model->lockAttribute('key1')->unsetData('key1');
  89. $this->assertEquals($data, $this->_model->getData());
  90. // unset all with read only
  91. $this->_model->setIsReadonly(true)->unsetData();
  92. $this->assertEquals($data, $this->_model->getData());
  93. // unset all
  94. $this->_model->unlockAttributes()->setIsReadonly(false)->unsetData();
  95. $this->assertEquals([], $this->_model->getData());
  96. }
  97. public function testGetResourceCollection()
  98. {
  99. $this->_model->setStoreId(99);
  100. $collection = $this->_model->getResourceCollection();
  101. $this->assertInstanceOf(
  102. \Magento\Catalog\Model\ResourceModel\Collection\AbstractCollection::class,
  103. $collection
  104. );
  105. $this->assertEquals(99, $collection->getStoreId());
  106. }
  107. /**
  108. * @magentoDataFixture Magento/Catalog/_files/products.php
  109. */
  110. public function testLoadByAttribute()
  111. {
  112. $object = $this->_model->loadByAttribute('sku', 'simple');
  113. $this->assertNotSame($object, $this->_model);
  114. // fixture
  115. $result = $this->_model->loadByAttribute('sku', uniqid());
  116. // specifying wrong attribute code leads to fatal
  117. $this->assertFalse($result);
  118. }
  119. public function testGetStore()
  120. {
  121. $store = $this->_model->getStore();
  122. $this->assertSame(
  123. $store,
  124. \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
  125. \Magento\Store\Model\StoreManagerInterface::class
  126. )->getStore()
  127. );
  128. }
  129. public function testGetWebsiteStoreIds()
  130. {
  131. $ids = $this->_model->getWebsiteStoreIds();
  132. $storeId = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
  133. \Magento\Store\Model\StoreManagerInterface::class
  134. )->getStore()->getId();
  135. $this->assertEquals([$storeId => $storeId], $ids);
  136. }
  137. public function testSetGetAttributeDefaultValue()
  138. {
  139. $this->assertFalse($this->_model->getAttributeDefaultValue('key'));
  140. $this->_model->setAttributeDefaultValue('key', 'value');
  141. $this->assertEquals('value', $this->_model->getAttributeDefaultValue('key'));
  142. }
  143. public function testSetGetExistsStoreValueFlag()
  144. {
  145. $this->assertFalse($this->_model->getExistsStoreValueFlag('key'));
  146. $this->_model->setExistsStoreValueFlag('key');
  147. $this->assertTrue($this->_model->getExistsStoreValueFlag('key'));
  148. }
  149. /**
  150. * @covers \Magento\Catalog\Model\AbstractModel::isDeleteable
  151. * @covers \Magento\Catalog\Model\AbstractModel::setIsDeleteable
  152. */
  153. public function testIsDeleteable()
  154. {
  155. $this->assertTrue($this->_model->isDeleteable());
  156. $this->_model->setIsDeleteable(false);
  157. $this->assertFalse($this->_model->isDeleteable());
  158. }
  159. /**
  160. * @covers \Magento\Catalog\Model\AbstractModel::isReadonly
  161. * @covers \Magento\Catalog\Model\AbstractModel::setIsReadonly
  162. */
  163. public function testIsReadonly()
  164. {
  165. $this->assertFalse($this->_model->isReadonly());
  166. $this->_model->setIsReadonly(true);
  167. $this->assertTrue($this->_model->isReadonly());
  168. }
  169. }