ConfigTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Catalog\Test\Unit\Model;
  8. /**
  9. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  10. */
  11. class ConfigTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * @covers \Magento\Catalog\Model\Config::loadAttributeSets
  15. * @return object
  16. */
  17. public function testLoadAttributeSets()
  18. {
  19. $setCollectionFactory = $this->createPartialMock(
  20. \Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\CollectionFactory::class,
  21. ['create']
  22. );
  23. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  24. $model = $objectManager->getObject(
  25. \Magento\Catalog\Model\Config::class,
  26. ['setCollectionFactory' => $setCollectionFactory]
  27. );
  28. $setItem = $this->createPartialMock(
  29. \Magento\Eav\Model\Entity\Attribute\Set::class,
  30. ['getEntityTypeId', 'getAttributeSetName', '__wakeup']
  31. );
  32. $setItem->expects($this->once())->method('getEntityTypeId')->will($this->returnValue(1));
  33. $setItem->expects($this->once())->method('getAttributeSetName')->will($this->returnValue('name'));
  34. $setCollection = $this->createPartialMock(
  35. \Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\Collection::class,
  36. ['load']
  37. );
  38. $setCollection->expects($this->once())->method('load')->will($this->returnValue([1 => $setItem]));
  39. $setCollectionFactory->expects($this->any())->method('create')->will($this->returnValue($setCollection));
  40. $model->loadAttributeSets();
  41. return $model;
  42. }
  43. /**
  44. * @depends testLoadAttributeSets
  45. * @covers \Magento\Catalog\Model\Config::getAttributeSetName
  46. */
  47. public function testGetAttributeSetName($model)
  48. {
  49. $this->assertEquals('name', $model->getAttributeSetName(1, 1));
  50. $this->assertFalse($model->getAttributeSetName(2, 1));
  51. }
  52. /**
  53. * @depends testLoadAttributeSets
  54. * @covers \Magento\Catalog\Model\Config::getAttributeSetId
  55. */
  56. public function testGetAttributeSetId($model)
  57. {
  58. $this->assertEquals(1, $model->getAttributeSetId(1, 'name'));
  59. $this->assertFalse($model->getAttributeSetId(1, 'noname'));
  60. }
  61. /**
  62. * @covers \Magento\Catalog\Model\Config::loadAttributeGroups
  63. * @return object
  64. */
  65. public function testLoadAttributeGroups()
  66. {
  67. $groupCollectionFactory = $this->createPartialMock(
  68. \Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\CollectionFactory::class,
  69. ['create']
  70. );
  71. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  72. $model = $objectManager->getObject(
  73. \Magento\Catalog\Model\Config::class,
  74. ['groupCollectionFactory' => $groupCollectionFactory]
  75. );
  76. $setItem = $this->createPartialMock(
  77. \Magento\Eav\Model\Entity\Attribute\Group::class,
  78. ['getAttributeSetId', 'getAttributeGroupName', '__wakeup']
  79. );
  80. $setItem->expects($this->once())->method('getAttributeSetId')->will($this->returnValue(1));
  81. $setItem->expects($this->once())->method('getAttributeGroupName')->will($this->returnValue('name'));
  82. $groupCollection = $this->createPartialMock(
  83. \Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\Collection::class,
  84. ['load']
  85. );
  86. $groupCollection->expects($this->once())->method('load')->will($this->returnValue([1 => $setItem]));
  87. $groupCollectionFactory
  88. ->expects($this->any())
  89. ->method('create')
  90. ->will($this->returnValue($groupCollection));
  91. $model->loadAttributeGroups();
  92. return $model;
  93. }
  94. /**
  95. * @depends testLoadAttributeGroups
  96. * @covers \Magento\Catalog\Model\Config::getAttributeGroupName
  97. */
  98. public function testGetAttributeGroupName($model)
  99. {
  100. $this->assertEquals('name', $model->getAttributeGroupName(1, 1));
  101. $this->assertFalse($model->getAttributeGroupName(2, 1));
  102. }
  103. /**
  104. * @depends testLoadAttributeGroups
  105. * @covers \Magento\Catalog\Model\Config::getAttributeGroupId
  106. */
  107. public function testGetAttributeGroupId($model)
  108. {
  109. $this->assertEquals(1, $model->getAttributeGroupId(1, 'name'));
  110. $this->assertFalse($model->getAttributeGroupId(1, 'noname'));
  111. }
  112. /**
  113. * @covers \Magento\Catalog\Model\Config::loadProductTypes
  114. * @return object
  115. */
  116. public function testLoadProductTypes()
  117. {
  118. $productTypeFactory = $this->createPartialMock(\Magento\Catalog\Model\Product\TypeFactory::class, ['create']);
  119. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  120. $model = $objectManager->getObject(
  121. \Magento\Catalog\Model\Config::class,
  122. ['productTypeFactory' => $productTypeFactory]
  123. );
  124. $typeCollection = $this->createPartialMock(\Magento\Catalog\Model\Product\Type::class, ['getOptionArray']);
  125. $typeCollection->expects($this->once())->method('getOptionArray')->will($this->returnValue([1 => 'name']));
  126. $productTypeFactory
  127. ->expects($this->any())
  128. ->method('create')
  129. ->will($this->returnValue($typeCollection));
  130. $model->loadProductTypes();
  131. return $model;
  132. }
  133. /**
  134. * @depends testLoadProductTypes
  135. * @covers \Magento\Catalog\Model\Config::getProductTypeId
  136. */
  137. public function testGetProductTypeId($model)
  138. {
  139. $this->assertEquals(1, $model->getProductTypeId('name'));
  140. $this->assertFalse($model->getProductTypeId('noname'));
  141. }
  142. /**
  143. * @depends testLoadProductTypes
  144. * @covers \Magento\Catalog\Model\Config::getProductTypeName
  145. */
  146. public function testGetProductTypeName($model)
  147. {
  148. $this->assertEquals('name', $model->getProductTypeName(1));
  149. $this->assertFalse($model->getProductTypeName(2));
  150. }
  151. /**
  152. * @param $expected
  153. * @param $data
  154. * @param $search
  155. *
  156. * @covers \Magento\Catalog\Model\Config::getSourceOptionId
  157. * @dataProvider getSourceOptionIdDataProvider
  158. */
  159. public function testGetSourceOptionId($expected, $data, $search)
  160. {
  161. $object = $this->createPartialMock(\Magento\Framework\DataObject::class, ['getAllOptions']);
  162. $object->expects($this->once())->method('getAllOptions')->will($this->returnValue($data));
  163. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  164. $model = $objectManager->getObject(\Magento\Catalog\Model\Config::class);
  165. $this->assertEquals($expected, $model->getSourceOptionId($object, $search));
  166. }
  167. /**
  168. * @return array
  169. */
  170. public function getSourceOptionIdDataProvider()
  171. {
  172. return [
  173. [1, [['label' => 'name', 'value' => 1]], 1],
  174. [1, [['label' => 'name', 'value' => 1]], 'name'],
  175. [null, [['label' => 'name', 'value' => 1]], 2],
  176. ];
  177. }
  178. /**
  179. * @return array
  180. */
  181. protected function prepareConfigModelForAttributes()
  182. {
  183. $storeId = 1;
  184. $attributeData = ['attribute_code' => 1];
  185. $attributesData = [$attributeData];
  186. $entityType = 'catalog_product';
  187. $storeLabel = 'label';
  188. $attributeCode = 'code';
  189. $attribute = $this->createPartialMock(
  190. \Magento\Eav\Model\Entity\Attribute\AbstractAttribute::class,
  191. ['getStoreLabel', 'getAttributeCode', '__wakeup']
  192. );
  193. $attribute->expects($this->any())->method('getStoreLabel')->will($this->returnValue($storeLabel));
  194. $attribute->expects($this->any())->method('getAttributeCode')->will($this->returnValue($attributeCode));
  195. $storeManager = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
  196. $store = $this->createMock(\Magento\Store\Model\Store::class);
  197. $storeManager->expects($this->any())->method('getStore')->will($this->returnValue($store));
  198. $store->expects($this->any())->method('getId')->will($this->returnValue($storeId));
  199. $config = $this->createPartialMock(
  200. \Magento\Catalog\Model\ResourceModel\Config::class,
  201. ['setStoreId', 'getAttributesUsedInListing', 'getAttributesUsedForSortBy', '__wakeup']
  202. );
  203. $config->expects($this->any())->method('setStoreId')->with($storeId)->will($this->returnSelf());
  204. $config->expects($this->any())->method('getAttributesUsedInListing')->will($this->returnValue($attributesData));
  205. $config->expects($this->any())->method('getAttributesUsedForSortBy')->will($this->returnValue($attributesData));
  206. $configFactory =
  207. $this->createPartialMock(\Magento\Catalog\Model\ResourceModel\ConfigFactory::class, ['create']);
  208. $configFactory->expects($this->atLeastOnce())->method('create')->will($this->returnValue($config));
  209. $eavConfig = $this->createPartialMock(
  210. \Magento\Eav\Model\Config::class,
  211. ['getAttribute', 'importAttributesData']
  212. );
  213. $eavConfig->expects($this->once())->method('importAttributesData')->with($entityType, $attributesData)
  214. ->will($this->returnSelf());
  215. $eavConfig->expects($this->once())->method('getAttribute')->with($entityType, $attributeData['attribute_code'])
  216. ->will($this->returnValue($attribute));
  217. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  218. $model = $objectManager->getObject(
  219. \Magento\Catalog\Model\Config::class,
  220. ['configFactory' => $configFactory, 'storeManager' => $storeManager, 'eavConfig' => $eavConfig]
  221. );
  222. return [$model, $attribute];
  223. }
  224. /**
  225. * @covers \Magento\Catalog\Model\Config::getAttributesUsedInProductListing
  226. * return object
  227. */
  228. public function testGetAttributesUsedInProductListing()
  229. {
  230. list($model, $attribute) = $this->prepareConfigModelForAttributes();
  231. $this->assertEquals([1 => $attribute], $model->getAttributesUsedInProductListing());
  232. return $model;
  233. }
  234. /**
  235. * @depends testGetAttributesUsedInProductListing
  236. * @covers \Magento\Catalog\Model\Config::getProductAttributes
  237. */
  238. public function testGetProductAttributes($model)
  239. {
  240. $this->assertEquals([1], $model->getProductAttributes());
  241. }
  242. /**
  243. * @covers \Magento\Catalog\Model\Config::getAttributesUsedForSortBy
  244. */
  245. public function testGetAttributesUsedForSortBy()
  246. {
  247. list($model, $attribute) = $this->prepareConfigModelForAttributes();
  248. $this->assertEquals([1 => $attribute], $model->getAttributesUsedForSortBy());
  249. }
  250. /**
  251. * @covers \Magento\Catalog\Model\Config::getAttributeUsedForSortByArray
  252. */
  253. public function testGetAttributeUsedForSortByArray()
  254. {
  255. list($model) = $this->prepareConfigModelForAttributes();
  256. $this->assertEquals(['position' => 'Position', 'code' => 'label'], $model->getAttributeUsedForSortByArray());
  257. }
  258. /**
  259. * @covers \Magento\Catalog\Model\Config::getProductListDefaultSortBy
  260. */
  261. public function testGetProductListDefaultSortBy()
  262. {
  263. $scopeConfig = $this->createPartialMock(
  264. \Magento\Framework\App\Config\ScopeConfigInterface::class,
  265. ['getValue', 'isSetFlag']
  266. );
  267. $scopeConfig->expects($this->once())->method('getValue')
  268. ->with('catalog/frontend/default_sort_by', 'store', null)->will($this->returnValue(1));
  269. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  270. $model = $objectManager->getObject(\Magento\Catalog\Model\Config::class, ['scopeConfig' => $scopeConfig]);
  271. $this->assertEquals(1, $model->getProductListDefaultSortBy());
  272. }
  273. }