PageRenderTime 43ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/.dev/tests/Web/Customer/TopCategories.php

https://github.com/istran/core
PHP | 459 lines | 213 code | 50 blank | 196 comment | 9 complexity | 7c02db24ce76c9e1c46bee9be826bcfb MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause
  1. <?php
  2. // vim: set ts=4 sw=4 sts=4 et:
  3. /**
  4. * LiteCommerce
  5. *
  6. * NOTICE OF LICENSE
  7. *
  8. * This source file is subject to the Open Software License (OSL 3.0)
  9. * that is bundled with this package in the file LICENSE.txt.
  10. * It is also available through the world-wide-web at this URL:
  11. * http://opensource.org/licenses/osl-3.0.php
  12. * If you did not receive a copy of the license and are unable to
  13. * obtain it through the world-wide-web, please send an email
  14. * to licensing@litecommerce.com so we can send you a copy immediately.
  15. *
  16. * @category LiteCommerce
  17. * @package Tests
  18. * @subpackage Web
  19. * @author Creative Development LLC <info@cdev.ru>
  20. * @copyright Copyright (c) 2010 Creative Development LLC <info@cdev.ru>. All rights reserved
  21. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  22. * @version GIT: $Id: b3cc9be536f66f45f342aa1b7718408c21feebba $
  23. * @link http://www.litecommerce.com/
  24. * @see ____file_see____
  25. * @since 3.0.0
  26. */
  27. require_once __DIR__ . '/ACustomer.php';
  28. class XLite_Web_Customer_TopCategories extends XLite_Web_Customer_ACustomer
  29. {
  30. /**
  31. * Test the widget in "list" display mode
  32. *
  33. * @return void
  34. * @access public
  35. * @see ____func_see____
  36. * @since 3.0.0
  37. */
  38. public function testListMode()
  39. {
  40. $this->testDisplayMode('list');
  41. }
  42. /**
  43. * Test the widgte in "tree" display mode
  44. *
  45. * @return void
  46. * @access public
  47. * @see ____func_see____
  48. * @since 3.0.0
  49. */
  50. public function testTreeMode()
  51. {
  52. $this->testDisplayMode('tree');
  53. }
  54. /**
  55. * Test the widget in "path" display mode (default mode)
  56. *
  57. * @return void
  58. * @access public
  59. * @see ____func_see____
  60. * @since 3.0.0
  61. */
  62. public function testPathMode()
  63. {
  64. $this->testDisplayMode('path');
  65. }
  66. /**
  67. * Test the widget in the specified display mode
  68. *
  69. * @param string $mode Display mode ('list', 'path', 'tree')
  70. *
  71. * @return void
  72. * @access protected
  73. * @see ____func_see____
  74. * @since 3.0.0
  75. */
  76. protected function testDisplayMode($mode)
  77. {
  78. $this->setDisplayMode($mode);
  79. $this->openAndWait('store/main');
  80. $this->testRootCategories($mode);
  81. $child = $this->getRandomCategory(1);
  82. $root = is_object($child)
  83. ? $this->getParentCategory($child->getCategoryId())
  84. : $this->getRandomCategory(0);
  85. $this->assertTrue(
  86. is_object($root),
  87. 'Can\'t find a root category for TopCategories testing (\'' . $mode . '\' mode)'
  88. );
  89. $rootName = $root->getName();
  90. $rootDepth = $this->getRepo()->getCategoryDepth($root->getCategoryId());
  91. $rootId = $root->getCategoryId();
  92. // Check whether the random root category is not in the active trail
  93. $this->assertCategoryNotActiveTrail(
  94. $rootId,
  95. '\'' . $rootName . '\' category is in the active trail on the store home page'
  96. );
  97. $childId = false;
  98. if (is_object($child)) {
  99. $childName = $child->getName();
  100. $childDepth = $this->getRepo()->getCategoryDepth($child->getCategoryId());
  101. $childId = $child->getCategoryId();
  102. if ($mode!='tree') {
  103. // Make sure the child category is not visible on the home page
  104. $this->assertCategoryNotVisible(
  105. $childId,
  106. '\'' . $childName . '\' subcategory is visible on the store home page'
  107. );
  108. }
  109. }
  110. // Open the root category page
  111. $rootURL = $this->getJSExpression('jQuery("'.$this->getLinkSelector($rootName, $rootDepth).'")');
  112. $this->openAndWait($rootURL);
  113. $this->testRootCategories($mode);
  114. // Check whether the opened root category is in the active trail
  115. $this->assertCategoryActiveTrail(
  116. $rootId,
  117. 'Selected \'' . $rootName . '\' category is not in the active trail'
  118. );
  119. // If it is not "list" mode test subcategories ('list' mode doesn't list subcategories at all)
  120. if ($mode!='list') {
  121. // Check whether all subcategories of the selected one are shown on the page
  122. $this->testSubcategories($rootId);
  123. if ($childId) {
  124. // Make sure the child category is not in the active trail on the root category page
  125. $this->assertCategoryNotActiveTrail(
  126. $childId,
  127. '\'' . $childName . '\' category is in the active trail on the page of its parent category (\'' . $rootName . '\')'
  128. );
  129. // Open the child category page
  130. $childURL = $this->getJSExpression('jQuery("'.$this->getLinkSelector($childName, $childDepth).'")');
  131. $this->openAndWait($childURL);
  132. $this->testRootCategories($mode);
  133. // Check whether the parent root category is still in the active trail
  134. $this->assertCategoryActiveTrail(
  135. $rootId,
  136. 'Parent \'' . $rootName . '\' category is not in the active trail'
  137. );
  138. // Check whether the selected child category is in the active trail
  139. $this->assertCategoryActiveTrail(
  140. $childId,
  141. 'Child \'' . $childName . '\' category is not in the active trail'
  142. );
  143. // Check whether all subcategories of the selected child category are shown on the page
  144. $this->testSubcategories($childId);
  145. }
  146. }
  147. }
  148. /**
  149. * Test whether the widget displays all root categories and has a correct structure
  150. *
  151. * @param string $mode Display mode
  152. *
  153. * @return void
  154. * @access protected
  155. * @see ____func_see____
  156. * @since 3.0.0
  157. */
  158. protected function testRootCategories($mode)
  159. {
  160. // Check the widget
  161. $widgetSelector = $this->getWidgetSelector();
  162. $this->assertJqueryPresent(
  163. $widgetSelector,
  164. 'TopCategories widget is missing (\'' . $mode . '\' display mode)'
  165. );
  166. // Make sure the widget lists all root categories
  167. $this->testSubcategories(0);
  168. if ($mode == 'list') {
  169. // There should be no links to subcategories
  170. $this->assertJqueryNotPresent(
  171. $widgetSelector . ' ul',
  172. 'TopCategories widget displays subcategories (\'' . $mode . '\' display mode)'
  173. );
  174. }
  175. if ($mode=='path') {
  176. // There should be no links to subcategories beyond the active trail
  177. $this->assertJqueryNotPresent(
  178. $widgetSelector . ' li:not(.active-trail) ul',
  179. 'TopCategories widget displays subcategories beyond the active trail (\'' . $mode . '\' display mode)'
  180. );
  181. }
  182. }
  183. /**
  184. * Check whether the widget displays all subcategories of a category with the specifeid ID
  185. *
  186. * @param int $categoryId ID of the category that should display all subcategories
  187. *
  188. * @return void
  189. * @access protected
  190. * @see ____func_see____
  191. * @since 3.0.0
  192. */
  193. protected function testSubcategories($categoryId = 0)
  194. {
  195. $categories = $this->getSubcategories($categoryId);
  196. foreach ($categories as $category) {
  197. $name = $category->getName();
  198. $depth = $this->getRepo()->getCategoryDepth($category->getCategoryId());
  199. $selector = $this->getLinkSelector($name, $depth);
  200. // echo "\nTesting \"$name\" (depth: $depth, parent ID: $categoryId) => selector: $selector\n";
  201. $this->assertJqueryPresent(
  202. $selector,
  203. 'Link to \'' . $name . '\' category is missing in TopCategories widget'
  204. );
  205. }
  206. }
  207. /**
  208. * Asserts whether the widget displays a link to a category
  209. *
  210. * @param int $categoryId ID of the category
  211. * @param string $message Message to be displayed on an error
  212. *
  213. * @return void
  214. * @access protected
  215. * @see ____func_see____
  216. * @since 3.0.0
  217. */
  218. protected function assertCategoryVisible($categoryId, $message)
  219. {
  220. $category = $this->getCategory($categoryId);
  221. $name = $category->getName();
  222. $depth = $this->getRepo()->getCategoryDepth($category->getCategoryId());
  223. $selector = $this->getLinkSelector($name, $depth);
  224. $this->assertJqueryPresent($selector, $message);
  225. }
  226. /**
  227. * Asserts whether the widget doesn't display a link to a category
  228. *
  229. * @param int $categoryId ID of the category
  230. * @param string $message Message to be displayed on an error
  231. *
  232. * @return void
  233. * @access protected
  234. * @see ____func_see____
  235. * @since 3.0.0
  236. */
  237. protected function assertCategoryNotVisible($categoryId, $message)
  238. {
  239. $category = $this->getCategory($categoryId);
  240. $name = $category->getName();
  241. $depth = $this->getRepo()->getCategoryDepth($category->getCategoryId());
  242. $selector = $this->getLinkSelector($name, $depth);
  243. $this->assertJqueryNotPresent($selector, $message);
  244. }
  245. /**
  246. * Asserts whether the widget displays a category link in the active trail
  247. *
  248. * @param int $categoryId ID of the category
  249. * @param string $message Message to be displayed on an error
  250. *
  251. * @return void
  252. * @access protected
  253. * @see ____func_see____
  254. * @since 3.0.0
  255. */
  256. protected function assertCategoryActiveTrail($categoryId, $message)
  257. {
  258. $category = $this->getCategory($categoryId);
  259. $name = $category->getName();
  260. $depth = $this->getRepo()->getCategoryDepth($category->getCategoryId());
  261. $selector = str_replace('li', 'li.active-trail', $this->getLinkSelector($name, $depth));
  262. $this->assertJqueryPresent($selector, $message);
  263. }
  264. /**
  265. * Asserts whether the widget doesn't display a category link in the active trail
  266. *
  267. * @param int $categoryId ID of the category
  268. * @param string $message Message to be displayed on an error
  269. *
  270. * @return void
  271. * @access protected
  272. * @see ____func_see____
  273. * @since 3.0.0
  274. */
  275. protected function assertCategoryNotActiveTrail($categoryId, $message)
  276. {
  277. $category = $this->getCategory($categoryId);
  278. $name = $category->getName();
  279. $depth = $this->getRepo()->getCategoryDepth($category->getCategoryId());
  280. $selector = str_replace('li', 'li.active-trail', $this->getLinkSelector($name, $depth));
  281. $this->assertJqueryNotPresent($selector, $message);
  282. }
  283. /**
  284. * Returns a jQuery selector to the widget element
  285. *
  286. * @return string
  287. * @access protected
  288. * @see ____func_see____
  289. * @since 3.0.0
  290. */
  291. protected function getWidgetSelector()
  292. {
  293. return 'ul.catalog-categories:visible';
  294. }
  295. /**
  296. * Returns a jQuery selector to a link
  297. *
  298. * @param string $name Category name
  299. * @param int $depth Category depth (1 for root categories)
  300. *
  301. * @return string
  302. * @access protected
  303. * @see ____func_see____
  304. * @since 3.0.0
  305. */
  306. protected function getLinkSelector($name, $depth)
  307. {
  308. return $this->getWidgetSelector() . ' > li' .str_repeat(' > ul > li', ($depth)) . " a:contains($name)";
  309. }
  310. /**
  311. * Returns a Doctrine repository for the categories model
  312. *
  313. * @return object
  314. * @access private
  315. * @see ____func_see____
  316. * @since 3.0.0
  317. */
  318. private function getRepo()
  319. {
  320. return \Xlite\Core\Database::getRepo('XLite\Model\Category');
  321. }
  322. /**
  323. * Returns a category model
  324. *
  325. * @param int $id Category ID
  326. *
  327. * @return \XLite\Model\Category
  328. * @access protected
  329. * @see ____func_see____
  330. * @since 3.0.0
  331. */
  332. protected function getCategory($id = null)
  333. {
  334. return $this->getRepo()->getCategory($id);
  335. }
  336. /**
  337. * Returns subcategories model
  338. *
  339. * @param int $categoryId ID of the parent category
  340. *
  341. * @return object
  342. * @access protected
  343. * @see ____func_see____
  344. * @since 3.0.0
  345. */
  346. protected function getSubcategories($categoryId)
  347. {
  348. return $this->getRepo()->getSubcategories($categoryId);
  349. }
  350. /**
  351. * Returns a random category of the specified depth
  352. *
  353. * @param int $depth Category depth
  354. *
  355. * @return \XLite\Model\Category
  356. * @access protected
  357. * @see ____func_see____
  358. * @since 3.0.0
  359. */
  360. protected function getRandomCategory($depth = 1)
  361. {
  362. $result = null;
  363. foreach ($this->getRepo()->findAll() as $c) {
  364. if ($this->getRepo()->getCategoryDepth($c->getCategoryId()) == $depth) {
  365. $result = $c;
  366. break;
  367. }
  368. }
  369. $this->assertTrue(
  370. !is_null($result),
  371. "Can't find a test category of the $depth depth level"
  372. );
  373. return $result;
  374. }
  375. /**
  376. * Returns a parent category
  377. *
  378. * @param int $id ID of the child category
  379. *
  380. * @return \XLite\Model\Category
  381. * @access protected
  382. * @see ____func_see____
  383. * @since 3.0.0
  384. */
  385. protected function getParentCategory($id = 0)
  386. {
  387. $parent = $this->getRepo()->getCategory($id)->getParent();
  388. return ($parent->getCategoryId() > 1) ? $parent : null;
  389. }
  390. /**
  391. * Switches the widget to the specifed display mode
  392. *
  393. * @param string $mode Display mode ('list', 'path' or 'tree')
  394. *
  395. * @return void
  396. * @access protected
  397. * @see ____func_see____
  398. * @since 3.0.0
  399. */
  400. protected function setDisplayMode($mode = 'path')
  401. {
  402. $id = $this->findWidgetID('\\XLite\\View\\TopCategories');
  403. $this->assertFalse(is_null($id), "Can't find the widget in the database");
  404. $r = $this->query("UPDATE drupal_block_lc_widget_settings SET value='$mode' WHERE bid='$id' AND name='displayMode'");
  405. }
  406. }