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

Language PHP Lines 460
MD5 Hash 7c02db24ce76c9e1c46bee9be826bcfb Estimated Cost $4,698 (why?)
Repository https://github.com/istran/core.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
<?php
// vim: set ts=4 sw=4 sts=4 et:

/**
 * LiteCommerce
 * 
 * NOTICE OF LICENSE
 * 
 * This source file is subject to the Open Software License (OSL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/osl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to licensing@litecommerce.com so we can send you a copy immediately.
 * 
 * @category   LiteCommerce
 * @package    Tests
 * @subpackage Web
 * @author     Creative Development LLC <info@cdev.ru> 
 * @copyright  Copyright (c) 2010 Creative Development LLC <info@cdev.ru>. All rights reserved
 * @license    http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
 * @version    GIT: $Id: b3cc9be536f66f45f342aa1b7718408c21feebba $
 * @link       http://www.litecommerce.com/
 * @see        ____file_see____
 * @since      3.0.0
 */

require_once __DIR__ . '/ACustomer.php';

class XLite_Web_Customer_TopCategories extends XLite_Web_Customer_ACustomer
{
    /**
     * Test the widget in "list" display mode
     * 
     * @return void
     * @access public
     * @see    ____func_see____
     * @since  3.0.0
     */
    public function testListMode()
    {
       $this->testDisplayMode('list');
    }

    /**
     * Test the widgte in "tree" display mode
     * 
     * @return void
     * @access public
     * @see    ____func_see____
     * @since  3.0.0
     */
    public function testTreeMode()
    {
        $this->testDisplayMode('tree');
    }

    /**
     * Test the widget in "path" display mode (default mode)
     * 
     * @return void
     * @access public
     * @see    ____func_see____
     * @since  3.0.0
     */
    public function testPathMode()
    {
        $this->testDisplayMode('path');
    }

    /**
     * Test the widget in the specified display mode
     * 
     * @param string $mode Display mode ('list', 'path', 'tree')
     *  
     * @return void
     * @access protected
     * @see    ____func_see____
     * @since  3.0.0
     */
    protected function testDisplayMode($mode)
    {
        $this->setDisplayMode($mode);

        $this->openAndWait('store/main');
        $this->testRootCategories($mode);

        $child = $this->getRandomCategory(1);
        $root = is_object($child)
            ? $this->getParentCategory($child->getCategoryId())
            : $this->getRandomCategory(0);

        $this->assertTrue(
            is_object($root),
            'Can\'t find a root category for TopCategories testing (\'' . $mode . '\' mode)'
        );

        $rootName = $root->getName();
        $rootDepth = $this->getRepo()->getCategoryDepth($root->getCategoryId());
        $rootId = $root->getCategoryId();

        // Check whether the random root category is not in the active trail
        $this->assertCategoryNotActiveTrail(
            $rootId,
            '\'' . $rootName . '\' category is in the active trail on the store home page'
        );

        $childId = false;
        if (is_object($child)) {
            $childName = $child->getName();
            $childDepth = $this->getRepo()->getCategoryDepth($child->getCategoryId());
            $childId = $child->getCategoryId();

            if ($mode!='tree') {
               // Make sure the child category is not visible on the home page
                $this->assertCategoryNotVisible(
                    $childId,
                    '\'' . $childName . '\' subcategory is visible on the store home page'
                );
            }
        }

        // Open the root category page
        $rootURL = $this->getJSExpression('jQuery("'.$this->getLinkSelector($rootName, $rootDepth).'")');
        $this->openAndWait($rootURL);
        $this->testRootCategories($mode);

        // Check whether the opened root category is in the active trail
        $this->assertCategoryActiveTrail(
            $rootId,
            'Selected \'' . $rootName . '\' category is not in the active trail'
        );

        // If it is not "list" mode test subcategories ('list' mode doesn't list subcategories at all)
        if ($mode!='list') {

             // Check whether all subcategories of the selected one are shown on the page
            $this->testSubcategories($rootId);

            if ($childId) {
                // Make sure the child category is not in the active trail on the root category page
                $this->assertCategoryNotActiveTrail(
                    $childId,
                    '\'' . $childName . '\' category is in the active trail on the page of its parent category (\'' . $rootName . '\')'
                );

                // Open the child category page
                $childURL = $this->getJSExpression('jQuery("'.$this->getLinkSelector($childName, $childDepth).'")');
                $this->openAndWait($childURL);
                $this->testRootCategories($mode);

                // Check whether the parent root category is still in the active trail
                $this->assertCategoryActiveTrail(
                    $rootId,
                    'Parent \'' . $rootName . '\' category is not in the active trail'
                );

                // Check whether the selected child category is in the active trail
                $this->assertCategoryActiveTrail(
                    $childId,
                    'Child \'' . $childName . '\' category is not in the active trail'
                );

                // Check whether all subcategories of the selected child category are shown on the page
                $this->testSubcategories($childId);
               
            }
        } 

    }

    /**
     * Test whether the widget displays all root categories and has a correct structure
     * 
     * @param string $mode Display mode
     *  
     * @return void
     * @access protected
     * @see    ____func_see____
     * @since  3.0.0
     */
    protected function testRootCategories($mode)
    {
        // Check the widget
        $widgetSelector = $this->getWidgetSelector();
        $this->assertJqueryPresent(
            $widgetSelector,
            'TopCategories widget is missing (\'' . $mode . '\' display mode)'
        );

        // Make sure the widget lists all root categories
        $this->testSubcategories(0);

         if ($mode == 'list') {
            // There should be no links to subcategories
            $this->assertJqueryNotPresent(
                $widgetSelector . ' ul',
                'TopCategories widget displays subcategories (\'' . $mode . '\' display mode)'
            );
        }

        if ($mode=='path') {
            // There should be no links to subcategories beyond the active trail
            $this->assertJqueryNotPresent(
                $widgetSelector . ' li:not(.active-trail) ul',
                'TopCategories widget displays subcategories beyond the active trail (\'' . $mode . '\' display mode)'
            );
        }
    }

    /**
     * Check whether the widget displays all subcategories of a category with the specifeid ID
     * 
     * @param int $categoryId ID of the category that should display all subcategories
     *  
     * @return void
     * @access protected
     * @see    ____func_see____
     * @since  3.0.0
     */
    protected function testSubcategories($categoryId = 0)
    {
       $categories = $this->getSubcategories($categoryId);

        foreach ($categories as $category) {
            $name = $category->getName();
            $depth = $this->getRepo()->getCategoryDepth($category->getCategoryId());

            $selector = $this->getLinkSelector($name, $depth);

            // echo "\nTesting \"$name\" (depth: $depth, parent ID: $categoryId) => selector: $selector\n";

            $this->assertJqueryPresent(
                $selector,
                'Link to \'' . $name . '\' category is missing in TopCategories widget'
            );
        }
    }

    /**
     * Asserts whether the widget displays a link to a category
     * 
     * @param int    $categoryId ID of the category
     * @param string $message    Message to be displayed on an error
     *  
     * @return void
     * @access protected
     * @see    ____func_see____
     * @since  3.0.0
     */
    protected function assertCategoryVisible($categoryId, $message)
    {
        $category = $this->getCategory($categoryId);
        $name = $category->getName();
        $depth = $this->getRepo()->getCategoryDepth($category->getCategoryId());
        $selector = $this->getLinkSelector($name, $depth);
        $this->assertJqueryPresent($selector, $message);
    }

    /**
     * Asserts whether the widget doesn't display a link to a category
     * 
     * @param int    $categoryId ID of the category
     * @param string $message    Message to be displayed on an error
     *  
     * @return void
     * @access protected
     * @see    ____func_see____
     * @since  3.0.0
     */
    protected function assertCategoryNotVisible($categoryId, $message)
    {
        $category = $this->getCategory($categoryId);
        $name = $category->getName();
        $depth = $this->getRepo()->getCategoryDepth($category->getCategoryId());
        $selector = $this->getLinkSelector($name, $depth);
        $this->assertJqueryNotPresent($selector, $message);
    }

    /**
     * Asserts whether the widget displays a category link in the active trail
     * 
     * @param int    $categoryId ID of the category
     * @param string $message    Message to be displayed on an error
     *  
     * @return void
     * @access protected
     * @see    ____func_see____
     * @since  3.0.0
     */
    protected function assertCategoryActiveTrail($categoryId, $message)
    {
        $category = $this->getCategory($categoryId);
        $name = $category->getName();
        $depth = $this->getRepo()->getCategoryDepth($category->getCategoryId());
        $selector = str_replace('li', 'li.active-trail', $this->getLinkSelector($name, $depth));
        $this->assertJqueryPresent($selector, $message);
    }

    /**
     * Asserts whether the widget doesn't display a category link in the active trail
     * 
     * @param int    $categoryId ID of the category
     * @param string $message    Message to be displayed on an error
     *  
     * @return void
     * @access protected
     * @see    ____func_see____
     * @since  3.0.0
     */
    protected function assertCategoryNotActiveTrail($categoryId, $message)
    {
        $category = $this->getCategory($categoryId);
        $name = $category->getName();
        $depth = $this->getRepo()->getCategoryDepth($category->getCategoryId());
        $selector = str_replace('li', 'li.active-trail', $this->getLinkSelector($name, $depth));
        $this->assertJqueryNotPresent($selector, $message);
    }

    /**
     * Returns a jQuery selector to the widget element
     * 
     * @return string
     * @access protected
     * @see    ____func_see____
     * @since  3.0.0
     */
    protected function getWidgetSelector()
    {
        return 'ul.catalog-categories:visible';
    }

    /**
     * Returns a jQuery selector to a link
     * 
     * @param string $name  Category name
     * @param int    $depth Category depth (1 for root categories)
     *  
     * @return string
     * @access protected
     * @see    ____func_see____
     * @since  3.0.0
     */
    protected function getLinkSelector($name, $depth)
    {
        return $this->getWidgetSelector() . ' > li' .str_repeat(' > ul > li', ($depth)) . " a:contains($name)";
    }

    /**
     * Returns a Doctrine repository for the categories model
     * 
     * @return object
     * @access private
     * @see    ____func_see____
     * @since  3.0.0
     */
    private function getRepo()
    {
        return \Xlite\Core\Database::getRepo('XLite\Model\Category');
    }

    /**
     * Returns a category model
     * 
     * @param int $id Category ID
     *  
     * @return \XLite\Model\Category
     * @access protected
     * @see    ____func_see____
     * @since  3.0.0
     */
    protected function getCategory($id = null)
    {
        return $this->getRepo()->getCategory($id);
    }

    /**
     * Returns subcategories model
     * 
     * @param int $categoryId ID of the parent category
     *  
     * @return object
     * @access protected
     * @see    ____func_see____
     * @since  3.0.0
     */
    protected function getSubcategories($categoryId)
    {
        return $this->getRepo()->getSubcategories($categoryId);
    }

    /**
     * Returns a random category of the specified depth
     * 
     * @param int $depth Category depth
     *  
     * @return \XLite\Model\Category
     * @access protected
     * @see    ____func_see____
     * @since  3.0.0
     */
    protected function getRandomCategory($depth = 1)
    {
        $result = null;

        foreach ($this->getRepo()->findAll() as $c) {
            if ($this->getRepo()->getCategoryDepth($c->getCategoryId()) == $depth) {
                $result = $c;
                break;
            }
        }

        $this->assertTrue(
            !is_null($result),
            "Can't find a test category of the $depth depth level"
        );

        return $result;
    }

    /**
     * Returns a parent category
     * 
     * @param int $id ID of the child category
     *  
     * @return \XLite\Model\Category
     * @access protected
     * @see    ____func_see____
     * @since  3.0.0
     */
    protected function getParentCategory($id = 0)
    {
        $parent = $this->getRepo()->getCategory($id)->getParent();
        return ($parent->getCategoryId() > 1) ? $parent : null;
    }

    /**
     * Switches the widget to the specifed display mode
     * 
     * @param string $mode Display mode ('list', 'path' or 'tree')
     *  
     * @return void
     * @access protected
     * @see    ____func_see____
     * @since  3.0.0
     */
    protected function setDisplayMode($mode = 'path')
    {
        $id = $this->findWidgetID('\\XLite\\View\\TopCategories');
        $this->assertFalse(is_null($id), "Can't find the widget in the database");

        $r = $this->query("UPDATE drupal_block_lc_widget_settings SET value='$mode' WHERE bid='$id' AND name='displayMode'");

    }


}
 
Back to Top