PageRenderTime 26ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/dev/tests/integration/testsuite/Magento/UrlRewrite/Block/Catalog/Edit/FormTest.php

https://gitlab.com/crazybutterfly815/magento2
PHP | 268 lines | 174 code | 12 blank | 82 comment | 4 complexity | 52f3a1880b3299c6b1f25a37391062d1 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\UrlRewrite\Block\Catalog\Edit;
  7. /**
  8. * Test for \Magento\UrlRewrite\Block\Catalog\Edit\Form
  9. * @magentoAppArea adminhtml
  10. */
  11. class FormTest extends \PHPUnit_Framework_TestCase
  12. {
  13. /**
  14. * @var \Magento\Framework\ObjectManagerInterface
  15. */
  16. protected $objectManager;
  17. protected function setUp()
  18. {
  19. $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  20. }
  21. /**
  22. * Get form instance
  23. *
  24. * @param array $args
  25. * @return \Magento\Framework\Data\Form
  26. */
  27. protected function _getFormInstance($args = [])
  28. {
  29. /** @var $layout \Magento\Framework\View\Layout */
  30. $layout = $this->objectManager->get(
  31. \Magento\Framework\View\LayoutInterface::class
  32. );
  33. /** @var $block \Magento\UrlRewrite\Block\Catalog\Edit\Form */
  34. $block = $layout->createBlock(
  35. \Magento\UrlRewrite\Block\Catalog\Edit\Form::class,
  36. 'block',
  37. ['data' => $args]
  38. );
  39. $block->setTemplate(null);
  40. $block->toHtml();
  41. return $block->getForm();
  42. }
  43. /**
  44. * Check _formPostInit set expected fields values
  45. *
  46. * @covers \Magento\UrlRewrite\Block\Catalog\Edit\Form::_formPostInit
  47. *
  48. * @dataProvider formPostInitDataProvider
  49. *
  50. * @param array $productData
  51. * @param array $categoryData
  52. * @param string $action
  53. * @param string $requestPath
  54. * @param string $targetPath
  55. * @magentoConfigFixture current_store general/single_store_mode/enabled 1
  56. * @magentoAppIsolation enabled
  57. */
  58. public function testFormPostInitNew($productData, $categoryData, $action, $requestPath, $targetPath)
  59. {
  60. $args = [];
  61. if ($productData) {
  62. $args['product'] = $this->objectManager->create(
  63. \Magento\Catalog\Model\Product::class,
  64. ['data' => $productData]
  65. );
  66. }
  67. if ($categoryData) {
  68. $args['category'] = $this->objectManager->create(
  69. \Magento\Catalog\Model\Category::class,
  70. ['data' => $categoryData]
  71. );
  72. }
  73. $form = $this->_getFormInstance($args);
  74. $this->assertContains($action, $form->getAction());
  75. $this->assertEquals($requestPath, $form->getElement('request_path')->getValue());
  76. $this->assertEquals($targetPath, $form->getElement('target_path')->getValue());
  77. $this->assertTrue($form->getElement('target_path')->getData('disabled'));
  78. }
  79. /**
  80. * Test entity stores
  81. *
  82. * @dataProvider getEntityStoresDataProvider
  83. * @magentoAppIsolation enabled
  84. * @magentoDataFixture Magento/Store/_files/core_fixturestore.php
  85. *
  86. * @param array $productData
  87. * @param array $categoryData
  88. * @param array $expectedStores
  89. */
  90. public function testGetEntityStores($productData, $categoryData, $expectedStores)
  91. {
  92. $args = [];
  93. if ($productData) {
  94. $args['product'] = $this->objectManager->create(
  95. \Magento\Catalog\Model\Product::class,
  96. ['data' => $productData]
  97. );
  98. }
  99. if ($categoryData) {
  100. $args['category'] = $this->objectManager->create(
  101. \Magento\Catalog\Model\Category::class,
  102. ['data' => $categoryData]
  103. );
  104. }
  105. $form = $this->_getFormInstance($args);
  106. $this->assertEquals($expectedStores, $form->getElement('store_id')->getValues());
  107. }
  108. /**
  109. * Check exception is thrown when product does not associated with stores
  110. *
  111. * @magentoAppIsolation enabled
  112. * @magentoDataFixture Magento/Store/_files/core_fixturestore.php
  113. */
  114. public function testGetEntityStoresProductStoresException()
  115. {
  116. $args = [
  117. 'product' => $this->objectManager->create(
  118. \Magento\Catalog\Model\Product::class,
  119. ['data' => ['entity_id' => 1, 'name' => 'product1', 'url_key' => 'product2']]
  120. ),
  121. ];
  122. $form = $this->_getFormInstance($args);
  123. $this->assertEquals([], $form->getElement('store_id')->getValues());
  124. $this->assertEquals(
  125. 'We can\'t set up a URL rewrite because the product you chose is not associated with a website.',
  126. $form->getElement('store_id')->getAfterElementHtml()
  127. );
  128. }
  129. /**
  130. * Check exception is thrown when product stores in intersection with category stores is empty
  131. *
  132. * @magentoAppIsolation enabled
  133. * @magentoDataFixture Magento/Store/_files/core_fixturestore.php
  134. *
  135. */
  136. public function testGetEntityStoresProductCategoryStoresException()
  137. {
  138. $args = [
  139. 'product' => $this->objectManager->create(
  140. \Magento\Catalog\Model\Product::class,
  141. ['data' => ['entity_id' => 1, 'name' => 'product1', 'url_key' => 'product1', 'store_ids' => [1]]]
  142. ),
  143. 'category' => $this->objectManager->create(
  144. \Magento\Catalog\Model\Category::class,
  145. ['data' => ['entity_id' => 1, 'name' => 'category1', 'url_key' => 'category1', 'store_ids' => [3]]]
  146. ),
  147. ];
  148. $form = $this->_getFormInstance($args);
  149. $this->assertEquals([], $form->getElement('store_id')->getValues());
  150. $this->assertEquals(
  151. 'We can\'t set up a URL rewrite because the product you chose is not associated with a website.',
  152. $form->getElement('store_id')->getAfterElementHtml()
  153. );
  154. }
  155. /**
  156. * Check exception is thrown when category does not associated with stores
  157. *
  158. * @magentoAppIsolation enabled
  159. * @magentoDataFixture Magento/Store/_files/core_fixturestore.php
  160. */
  161. public function testGetEntityStoresCategoryStoresException()
  162. {
  163. $args = ['category' => $this->objectManager->create(
  164. \Magento\Catalog\Model\Category::class,
  165. ['data' => ['entity_id' => 1, 'name' => 'product1', 'url_key' => 'product', 'initial_setup_flag' => true]]
  166. )];
  167. $form = $this->_getFormInstance($args);
  168. $this->assertEquals([], $form->getElement('store_id')->getValues());
  169. $this->assertEquals(
  170. 'Please assign a website to the selected category.',
  171. $form->getElement('store_id')->getAfterElementHtml()
  172. );
  173. }
  174. /**
  175. * Data provider for testing formPostInit
  176. * 1) Category selected
  177. * 2) Product selected
  178. * 3) Product with category selected
  179. *
  180. * @static
  181. * @return array
  182. */
  183. public static function formPostInitDataProvider()
  184. {
  185. return [
  186. [
  187. null,
  188. ['entity_id' => 3, 'level' => 2, 'parent_id' => 2, 'url_key' => 'category', 'store_id' => 1],
  189. 'category/3',
  190. 'category.html',
  191. 'catalog/category/view/id/3',
  192. ],
  193. [
  194. ['entity_id' => 2, 'level' => 2, 'url_key' => 'product', 'store_id' => 1],
  195. null,
  196. 'product/2',
  197. 'product.html',
  198. 'catalog/product/view/id/2'
  199. ],
  200. [
  201. ['entity_id' => 2, 'name' => 'product', 'url_key' => 'product', 'store_id' => 1],
  202. ['entity_id' => 3, 'parent_id' => 2, 'level' => 2, 'url_key' => 'category', 'store_id' => 1],
  203. 'product/2/category/3',
  204. 'category/product.html',
  205. 'catalog/product/view/id/2/category/3'
  206. ]
  207. ];
  208. }
  209. /**
  210. * Entity stores data provider
  211. * 1) Category assigned to 1 store
  212. * 2) Product assigned to 1 store
  213. * 3) Product and category are assigned to same store
  214. *
  215. * @static
  216. * @return array
  217. */
  218. public static function getEntityStoresDataProvider()
  219. {
  220. return [
  221. [
  222. null,
  223. ['entity_id' => 3, 'store_ids' => [1]],
  224. [
  225. ['label' => 'Main Website', 'value' => []],
  226. [
  227. 'label' => '    Main Website Store',
  228. 'value' => [['label' => '    Default Store View', 'value' => 1]]
  229. ]
  230. ],
  231. ],
  232. [
  233. ['entity_id' => 2, 'name' => 'product2', 'url_key' => 'product2', 'store_ids' => [1]],
  234. null,
  235. [
  236. ['label' => 'Main Website', 'value' => []],
  237. [
  238. 'label' => '    Main Website Store',
  239. 'value' => [['label' => '    Default Store View', 'value' => 1]]
  240. ]
  241. ]
  242. ],
  243. [
  244. ['entity_id' => 2, 'name' => 'product2', 'url_key' => 'product2', 'store_ids' => [1]],
  245. ['entity_id' => 3, 'name' => 'product3', 'url_key' => 'product3', 'store_ids' => [1]],
  246. [
  247. ['label' => 'Main Website', 'value' => []],
  248. [
  249. 'label' => '    Main Website Store',
  250. 'value' => [['label' => '    Default Store View', 'value' => 1]]
  251. ]
  252. ]
  253. ]
  254. ];
  255. }
  256. }