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