PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/dev/tests/api-functional/testsuite/Magento/Catalog/Api/CategoryRepositoryTest.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 277 lines | 210 code | 21 blank | 46 comment | 3 complexity | 251b620581cf69d24c249150ca0ab75d MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. * Copyright © 2016 Magento. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Catalog\Api;
  8. use Magento\TestFramework\Helper\Bootstrap;
  9. use Magento\TestFramework\TestCase\WebapiAbstract;
  10. use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;
  11. use Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator;
  12. class CategoryRepositoryTest extends WebapiAbstract
  13. {
  14. const RESOURCE_PATH = '/V1/categories';
  15. const SERVICE_NAME = 'catalogCategoryRepositoryV1';
  16. private $modelId = 333;
  17. /**
  18. * @magentoApiDataFixture Magento/Catalog/_files/category_backend.php
  19. */
  20. public function testGet()
  21. {
  22. $expected = [
  23. 'parent_id' => 2,
  24. 'path' => '1/2/333',
  25. 'position' => 1,
  26. 'level' => 2,
  27. 'available_sort_by' => ['position', 'name'],
  28. 'include_in_menu' => true,
  29. 'name' => 'Category 1',
  30. 'id' => 333,
  31. 'is_active' => true,
  32. ];
  33. $result = $this->getInfoCategory($this->modelId);
  34. $this->assertArrayHasKey('created_at', $result);
  35. $this->assertArrayHasKey('updated_at', $result);
  36. $this->assertArrayHasKey('children', $result);
  37. $this->assertArrayHasKey('custom_attributes', $result);
  38. unset($result['created_at'], $result['updated_at'], $result['children'], $result['custom_attributes']);
  39. ksort($expected);
  40. ksort($result);
  41. $this->assertEquals($expected, $result);
  42. }
  43. public function testInfoNoSuchEntityException()
  44. {
  45. try {
  46. $this->getInfoCategory(-1);
  47. } catch (\Exception $e) {
  48. $this->assertContains('No such entity with %fieldName = %fieldValue', $e->getMessage());
  49. }
  50. }
  51. /**
  52. * @param int $id
  53. * @return string
  54. */
  55. protected function getInfoCategory($id)
  56. {
  57. $serviceInfo = [
  58. 'rest' => [
  59. 'resourcePath' => self::RESOURCE_PATH . '/' . $id,
  60. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
  61. ],
  62. 'soap' => [
  63. 'service' => self::SERVICE_NAME,
  64. 'serviceVersion' => 'V1',
  65. 'operation' => self::SERVICE_NAME . 'Get',
  66. ],
  67. ];
  68. return $this->_webApiCall($serviceInfo, ['categoryId' => $id]);
  69. }
  70. /**
  71. * Test for create category process
  72. *
  73. * @magentoApiDataFixture Magento/Catalog/Model/Category/_files/service_category_create.php
  74. */
  75. public function testCreate()
  76. {
  77. $categoryData = $this->getSimpleCategoryData(['name' => 'Test Category Name']);
  78. $result = $this->createCategory($categoryData);
  79. $this->assertGreaterThan(0, $result['id']);
  80. foreach (['name', 'parent_id', 'available_sort_by'] as $fieldName) {
  81. $this->assertEquals(
  82. $categoryData[$fieldName],
  83. $result[$fieldName],
  84. sprintf('"%s" field value is invalid', $fieldName)
  85. );
  86. }
  87. // delete category to clean up auto-generated url rewrites
  88. $this->deleteCategory($result['id']);
  89. }
  90. /**
  91. * @magentoApiDataFixture Magento/Catalog/_files/category.php
  92. */
  93. public function testDelete()
  94. {
  95. /** @var \Magento\UrlRewrite\Model\Storage\DbStorage $storage */
  96. $storage = Bootstrap::getObjectManager()->get('Magento\UrlRewrite\Model\Storage\DbStorage');
  97. $categoryId = $this->modelId;
  98. $data = [
  99. UrlRewrite::ENTITY_ID => $categoryId,
  100. UrlRewrite::ENTITY_TYPE => CategoryUrlRewriteGenerator::ENTITY_TYPE
  101. ];
  102. /** @var \Magento\UrlRewrite\Service\V1\Data\UrlRewrite $urlRewrite*/
  103. $urlRewrite = $storage->findOneByData($data);
  104. // Assert that a url rewrite is auto-generated for the category created from the data fixture
  105. $this->assertEquals(1, $urlRewrite->getIsAutogenerated());
  106. $this->assertEquals($categoryId, $urlRewrite->getEntityId());
  107. $this->assertEquals(CategoryUrlRewriteGenerator::ENTITY_TYPE, $urlRewrite->getEntityType());
  108. $this->assertEquals('category-1.html', $urlRewrite->getRequestPath());
  109. // Assert deleting category is successful
  110. $this->assertTrue($this->deleteCategory($this->modelId));
  111. // After the category is deleted, assert that the associated url rewrite is also auto-deleted
  112. $this->assertNull($storage->findOneByData($data));
  113. }
  114. public function testDeleteNoSuchEntityException()
  115. {
  116. try {
  117. $this->deleteCategory(-1);
  118. } catch (\Exception $e) {
  119. $this->assertContains('No such entity with %fieldName = %fieldValue', $e->getMessage());
  120. }
  121. }
  122. /**
  123. * @dataProvider deleteSystemOrRootDataProvider
  124. * @expectedException \Exception
  125. */
  126. public function testDeleteSystemOrRoot()
  127. {
  128. $this->deleteCategory($this->modelId);
  129. }
  130. public function deleteSystemOrRootDataProvider()
  131. {
  132. return [
  133. [\Magento\Catalog\Model\Category::TREE_ROOT_ID],
  134. [2] //Default root category
  135. ];
  136. }
  137. /**
  138. * @magentoApiDataFixture Magento/Catalog/_files/category.php
  139. */
  140. public function testUpdate()
  141. {
  142. $categoryId = 333;
  143. $categoryData = [
  144. 'name' => "Update Category Test",
  145. 'custom_attributes' => [
  146. [
  147. 'attribute_code' => 'description',
  148. 'value' => "Update Category Description Test",
  149. ],
  150. ],
  151. ];
  152. $result = $this->updateCategory($categoryId, $categoryData);
  153. $this->assertEquals($categoryId, $result['id']);
  154. /** @var \Magento\Catalog\Model\Category $model */
  155. $model = Bootstrap::getObjectManager()->get('Magento\Catalog\Model\Category');
  156. $category = $model->load($categoryId);
  157. $this->assertEquals("Update Category Test", $category->getName());
  158. $this->assertEquals("Update Category Description Test", $category->getDescription());
  159. // delete category to clean up auto-generated url rewrites
  160. $this->deleteCategory($categoryId);
  161. }
  162. protected function getSimpleCategoryData($categoryData = [])
  163. {
  164. return [
  165. 'path' => '2',
  166. 'parent_id' => '2',
  167. 'name' => isset($categoryData['name'])
  168. ? $categoryData['name'] : uniqid('Category-', true),
  169. 'is_active' => '1',
  170. 'include_in_menu' => "1",
  171. 'available_sort_by' => ['position', 'name'],
  172. 'custom_attributes' => [
  173. ['attribute_code' => 'url_key', 'value' => ''],
  174. ['attribute_code' => 'description', 'value' => 'Custom description'],
  175. ['attribute_code' => 'meta_title', 'value' => ''],
  176. ['attribute_code' => 'meta_keywords', 'value' => ''],
  177. ['attribute_code' => 'meta_description', 'value' => ''],
  178. ['attribute_code' => 'display_mode', 'value' => 'PRODUCTS'],
  179. ['attribute_code' => 'landing_page', 'value' => ''],
  180. ['attribute_code' => 'is_anchor', 'value' => '0'],
  181. ['attribute_code' => 'custom_use_parent_settings', 'value' => '0'],
  182. ['attribute_code' => 'custom_apply_to_products', 'value' => '0'],
  183. ['attribute_code' => 'custom_design', 'value' => ''],
  184. ['attribute_code' => 'custom_design_from', 'value' => ''],
  185. ['attribute_code' => 'custom_design_to', 'value' => ''],
  186. ['attribute_code' => 'page_layout', 'value' => ''],
  187. ]
  188. ];
  189. }
  190. /**
  191. * Create category process
  192. *
  193. * @param $category
  194. * @return int
  195. */
  196. protected function createCategory($category)
  197. {
  198. $serviceInfo = [
  199. 'rest' => [
  200. 'resourcePath' => self::RESOURCE_PATH,
  201. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST
  202. ],
  203. 'soap' => [
  204. 'service' => self::SERVICE_NAME,
  205. 'serviceVersion' => 'V1',
  206. 'operation' => self::SERVICE_NAME . 'Save',
  207. ],
  208. ];
  209. $requestData = ['category' => $category];
  210. return $this->_webApiCall($serviceInfo, $requestData);
  211. }
  212. /**
  213. * @param int $id
  214. * @return bool
  215. * @throws \Exception
  216. */
  217. protected function deleteCategory($id)
  218. {
  219. $serviceInfo =
  220. [
  221. 'rest' => [
  222. 'resourcePath' => self::RESOURCE_PATH . '/' . $id,
  223. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_DELETE,
  224. ],
  225. 'soap' => [
  226. 'service' => self::SERVICE_NAME,
  227. 'serviceVersion' => 'V1',
  228. 'operation' => self::SERVICE_NAME . 'DeleteByIdentifier',
  229. ],
  230. ];
  231. return $this->_webApiCall($serviceInfo, ['categoryId' => $id]);
  232. }
  233. protected function updateCategory($id, $data)
  234. {
  235. $serviceInfo =
  236. [
  237. 'rest' => [
  238. 'resourcePath' => self::RESOURCE_PATH . '/' . $id,
  239. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
  240. ],
  241. 'soap' => [
  242. 'service' => self::SERVICE_NAME,
  243. 'serviceVersion' => 'V1',
  244. 'operation' => self::SERVICE_NAME . 'Save',
  245. ],
  246. ];
  247. if (TESTS_WEB_API_ADAPTER == self::ADAPTER_SOAP) {
  248. $data['id'] = $id;
  249. return $this->_webApiCall($serviceInfo, ['id' => $id, 'category' => $data]);
  250. } else {
  251. $data['id'] = $id;
  252. return $this->_webApiCall($serviceInfo, ['id' => $id, 'category' => $data]);
  253. return $this->_webApiCall($serviceInfo, ['category' => $data]);
  254. }
  255. }
  256. }