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

Language PHP Lines 1141
MD5 Hash e314377c36743b963ee025d8e3eb4d72 Estimated Cost $14,147 (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
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
<?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: d6ecf5c3d8eaabacf32fd7ad00ca7da3a83c4057 $
 * @link       http://www.litecommerce.com/
 * @see        ____file_see____
 * @since      3.0.0
 */

// Disable the time limit, otherwise some tests may fail due to PHP breaking the script
set_time_limit(1200); // 20 minutes

require_once __DIR__ . '/ACustomer.php';

abstract class XLite_Web_Customer_AProductList extends XLite_Web_Customer_ACustomer
{
    protected $widgetContainerClass = '.items-list';
    
    protected $widgetClass = '';

    protected $currentTestUrl = null;

    protected $currentMode = '';

    protected $testProductLinks = false;

    /*
     *
     * TESTS
     *
     */

    // Table mode

    public function testBasicStructureTableMode()
    {
        $this->setDisplayMode('table');
        $this->testBasicStructure();
    }

    public function testProductsDataTableMode()
    {
        $this->setDisplayMode('table');
        $this->testProductsData();
    }

    public function testPagerTableMode()
    {
        $this->setDisplayMode('table');
        $this->testPager();
    }


    public function testDisplayModeSwitchTableMode()
    {
        $this->setDisplayMode('table');
        $this->testDisplayModeSwitch();
    }

    public function testSortingTableMode()
    {
// TODO need to re-work....
return;
        $this->setDisplayMode('table');
        $this->testSorting();
    }

    // List mode

    public function testBasicStructureListMode()
    {
        $this->setDisplayMode('list');
        $this->testBasicStructure();
    }

    public function testProductsDataListMode()
    {
//        $this->setDisplayMode('list');
//        $this->testProductsData();
    }


    public function testPagerListMode()
    {
//        $this->setDisplayMode('list');
//        $this->testPager();
    }

    public function testDisplayModeSwitchListMode()
    {
        $this->setDisplayMode('list');
        $this->testDisplayModeSwitch();
    }

    public function testSortingListMode()
    {
//        $this->setDisplayMode('list');
//        $this->testSorting();
    }

    // Grid mode

    public function testBasicStructureGridMode()
    {
        $this->setDisplayMode('grid', 3);
        $this->testBasicStructure();
    }

    public function testProductsDataGridMode()
    {
//        $this->setDisplayMode('grid', 3);
//        $this->testProductsData();
    }

    public function testPagerGridMode()
    {
//        $this->setDisplayMode('grid', 3);
//        $this->testPager();
    }

    public function testDisplayModeSwitchGridMode()
    {
        $this->setDisplayMode('grid', 3);
        $this->testDisplayModeSwitch();
    }

    public function testSortingGridMode()
    {
//        $this->setDisplayMode('grid', 3);
//        $this->testSorting();
    }

    /*
     * HELPER FUNCTIONS
     */

    /**
     * Opens the current test page
     * 
     * @return void
     * @access protected
     * @since  3.0.0
     */
    abstract protected function openTestPage();

    /**
     * Returns the number of all test products to be listed in a pager on the current test page
     * 
     * @return int
     * @since  3.0.0
     */
    abstract protected function countAllTestProducts();

    /**
     * Returns all test products to be listed in a pager on the current test page
     * 
     * @return mixed
     * @since  3.0.0
     */
    abstract protected function getAllTestProducts();


    /**
     * Returns jQuery selector to the top widget container element
     * 
     * @return string
     * @access protected
     * @since  3.0.0
     */
    protected function getListSelector()
    {
        return $this->widgetContainerClass;
    }

    /**
     * Test whether all the top UI elements are displayed and have a correct structure
     * 
     * @param string $mode Display mode
     *  
     * @return void
     * @access protected
     * @since  3.0.0
     */
    protected function testBasicStructure()
    {
        $mode = $this->getDisplayMode();

        // Set the pager to display more than one page
        $count = $this->countAllTestProducts();

        $this->configurePager(ceil($count/2), true);

        /*
         * Test all top UI elements
         */
        $this->setVisible('SortBySelector');
        $this->setVisible('DisplayModeSelector');

        $this->resetBrowser();

        $this->openTestPage();

        $selector = $this->getListSelector();

        $elements = array(
            $selector                         => "Widget is missing ($mode mode)",
            $selector . ' .products-' . $mode => "Mode container element is missing ($mode mode)",
//            $selector . ' .list-header' => "List header is missing",
//            $selector . ' .list-header .display-modes' => "Display Mode box is missing",
//            $selector . ' .list-header .sort-box' => "Sort Box is missing",
        );

        if ($mode != 'table') {

            $elements["$selector .cart-tray"] = "Cart tray is missing";

        }

        foreach ($elements as $s => $message) {

            $this->assertJqueryPresent($s, "$message ($mode mode)");
        }

        /*
         * Now test how UI elements are enabled/disabled
         */
        foreach (array(false, true) as $sortBox) {

            foreach (array(false, true) as $displayMode) {

                $sortBox = $sortBox ? $this->setVisible('SortBySelector') : $this->setHidden('SortBySelector');

                $displayMode = $displayMode ? $this->setVisible('DisplayModeSelector') : $this->setHidden('DisplayModeSelector');

                $this->resetBrowser(); 

                $this->openTestPage();

                $sortBoxMethod = $sortBox ? "assertElementPresent" : "assertElementNotPresent";

                $displayModeMethod = $displayMode ? "assertElementPresent" : "assertElementNotPresent";

                $this->$sortBoxMethod(
                    "css=$selector .list-header .sort-box",
                    "Failed assertion ($mode mode): sort box = ".(string)$sortBox."; display mode selector = ".(string)$displayMode
                );

                $this->$displayModeMethod(
                    "css=$selector .list-header .display-modes",
                    "Failed assertion ($mode mode): display mode selector = ".(string)$displayMode."; sort box = ".(string)$sortBox
                );

            }
        }
    }

    /**
     * Test whether the widget displays all products and with correct product data
     * 
     * @return void
     * @access protected
     * @since  3.0.0
     */
    protected function testProductsData()
    {
        $mode = $this->getDisplayMode();

        // Configure the pager to display all test products on one page
        $productsCount = $this->countAllTestProducts();

        $this->configurePager($productsCount, true);
        $this->resetBrowser();
        $this->openTestPage();

        // Get products from the DB
        $products = $this->getAllTestProducts();
        // Get products from the page
        $listedProducts = $this->getListedProducts($mode);

        // Make sure the page displays enough products
        $this->assertEquals(
            count($listedProducts),
            $productsCount,
            "The widget displays a wrong number of products ($mode mode)"
        );

        // Make sure the products are displayed with a correct structure
//        $this->testPagerProducts(array_values($listedProducts), $productsCount, 1);

        $products = array(current($products), $products[count($products) - 1]);

        // Make sure that the page displays all products and the products have correct data
        foreach($products as $product) {

            // find the product
            $id = $product->getProductId();

            $this->assertTrue(
                isset($listedProducts[$id]),
                "A test $id product is missing in the widget ($mode mode)"
            );
/*
            $listedProduct =& $listedProducts[$id];

            // test a product name
            $this->assertEquals(
                $name = $product->getName(),
                $listedProduct['name'],
                "A test $id product is displayed with a wrong name ($mode mode)"
            );

            // test a product sku
            if (isset($listedProduct['sku'])) {
                $this->assertEquals(
                    $product->getSku(),
                    $listedProduct['sku'],
                    "A test $id product is displayed with a wrong sku ($mode mode)"
                );
            }

            // test a product price
            $this->assertEquals(
                (float)$product->getPrice(),
                (float)$listedProduct['parsedPrice'],
                "A test $id product is displayed with a wrong price ($mode mode)"
            );

            if ($this->testProductLinks) {

                // test a link to the product page
                $this->getJSExpression('window.location = "'.$listedProduct['nameUrl'].'";');
                $this->waitForPageToLoad(180000);
                $this->assertElementPresent(
                    "css=h1.fn.title",
                    "Product $id doesn't link to a product page ($mode mode)"
                );
                $title = $this->getJSExpression("jQuery('h1.fn.title').html()");
                $this->assertEquals(
                    $name,
                    $title,
                    "Product $id links to a wrong product page ($mode mode)"
                );

           }
*/

        }

    }

    /**
     * Test whether the pager splits and browses products correctly
     * 
     * @return void
     * @access protected
     * @since  3.0.0
     */
    protected function testPager()
    {
// TODO make this test lighter
return;

        $mode = $this->getDisplayMode();
        $listSelector = $this->getListSelector();

        // Display all products and store displayed product data for further reference
        $productsCount = $this->countAllTestProducts();
        $this->configurePager($productsCount, true);
        $this->resetBrowser();
        $this->openTestPage();
        $allProducts = array_values($this->getListedProducts($mode));

        // Test the pager when there is one page only
        $this->testPagerStructure($productsCount, $productsCount, 1);

        // Now test how the pager displays products split into several pages
        $pagesCount = ($productsCount > 3) ? 3 : 1;
        $perPage = ($productsCount > $pagesCount) ? ceil($productsCount/$pagesCount) : $productCount;
        $this->configurePager($perPage, true);

        // Open the first page
        $this->resetBrowser();
        $this->openTestPage();

        /*
         * Browse all pager pages by clicking "Next page" button
         */

        for($page=1; $page<=$pagesCount; $page++) {

            // Test the structure of the pager widget and listed products
            $this->testPagerStructure($productsCount, $perPage, $page);
            $this->testPagerProducts($allProducts, $perPage, $page);

            // Click "Next page" button on all pager pages except the last one
            if ($page < $pagesCount) {

                $this->assertElementPresent(
                    $link = "css=$listSelector ul.pager li.next-page a",
                    "'Next page' link is missing on the $page page ($mode mode)"
                );

                $this->click($link);
                $this->waitForAjaxProgress();
            }

        }

        // Make sure the last page doesn't display "Next page" link
        $this->assertElementNotPresent(
            "css=$listSelector ul.pager li.next-page a",
            "'Next page' link is missing on the $page page ($mode mode)"
        );

        /*
         * Browse all pager pages by clicking "Previous page" button
         */

        for($page=$pagesCount; $page>=1; $page--) {

            // Test the structure of the pager widget and listed products
            $this->testPagerStructure($productsCount, $perPage, $page);
            $this->testPagerProducts($allProducts, $perPage, $page);

            // Click "Previous page" button on all pager pages except the first one
            if ($page > 1) {

                $this->assertElementPresent(
                    $link = "css=$listSelector ul.pager li.previous-page a",
                    "'Previous page' link is missing on the $page page ($mode mode)"
                );

                $this->click($link);
                $this->waitForAjaxProgress();
            }

        }

        // Make sure the first page doesn't display "Previous page" link
        $this->assertElementNotPresent(
            "css=$listSelector ul.pager li.previous-page a",
            "'Previous page' link is missing on the $page page ($mode mode)"
        );

        /*
         * Now check how changing the number of products per page affects the pager
         */

        $productsSelector = "$listSelector .products .product";

        for ($perPage=1; $perPage <= $productsCount; $perPage++) {

            $inputSelector = "$listSelector .list-pager .pager-items-total input.page-length";

            $this->assertElementPresent(
                "css=$inputSelector",
                "Input element is missing ($perPage products per page, $mode mode)"
            );
            $this->type("css=$inputSelector", $perPage);
            $this->keyPress("css=$inputSelector", '\\13');
            $this->waitForAjaxProgress();

            $this->assertEquals(
                $perPage,
                $this->getJSExpression("jQuery('$productsSelector').size()"),
                "Number of products doesn't match the number to be displayed per page ($perPage per page, $mode mode)"
            );

        }

    }

    /**
     * Test how the mode switching links work
     * 
     * @return void
     * @access protected
     * @since  3.0.0
     */
    protected function testDisplayModeSwitch()
    {
        $mode = $this->getDisplayMode();
        $listSelector = $this->getListSelector();

        $this->configurePager(9, true);

        $this->setVisible('DisplayModeSelector');
        $this->resetBrowser();
        $this->openTestPage();

        $modes = array(
            'grid',
            'list',
            'table',
        );

        foreach ($modes as $m){

            $linkSelector = "$listSelector .list-header ul.display-modes li.list-type-$m a";
            $productsSelector = "$listSelector .products .products-$m";

            $this->assertElementPresent(
                "css=$linkSelector",
                "Link for $m mode is missing ($mode initial mode)"
            );

            $this->click("css=$linkSelector");
            $this->waitForAjaxProgress();

            $this->assertElementPresent(
                "css=$productsSelector",
                "$productsSelector is missing after clicking '$m' mode ($mode initial mode)"
            );

        }

    }


    /**
     * Test the sorting options
     * 
     * @return void
     * @access protected
     * @since  3.0.0
     */
    protected function testSorting()
    {
        $mode = $this->getDisplayMode();
        $listSelector = $this->getListSelector();
        $selector = "$listSelector .list-header .sort-box select.sort-crit";
        $orderSelector = "$listSelector .list-header .sort-box a.sort-order";

        // Display all products and store displayed product data for further reference
        $productsCount = $this->countAllTestProducts();

        $this->configurePager($productsCount, true);

        $this->setVisible('SortBySelector');

        $this->resetBrowser();
        $this->openTestPage();

        $optionLabels = array(
            'Name'=>'name',
            'Price'=>'price',
            'Default'=>'name',   // for some reasons the default sort method is neither by name nor by id; can't find a way to test it
        );

        if ('table' === $mode) {
            $optionLabels['SKU'] = 'sku';
        }

        $sortOrder = 'asc';

        foreach ($optionLabels as $label=>$field) {

            $this->assertElementPresent(
                "css=$selector",
                "Mode selector is missing ($label label, $field field, $mode mode)"
            );

            $this->select("css=$selector", "label=$label");

            $this->waitForAjaxProgress();

            $this->testSortedProducts($field, $label, $mode, ($sortOrder=='asc'));

            $this->assertElementPresent(
                "css=$orderSelector",
                "Asc/Desc link is missing ($label label, $field field, $mode mode)"
            );

            $this->click("css=$orderSelector");

            $this->waitForAjaxProgress();

            $sortOrder = ($sortOrder == 'asc') ? 'desc' : 'asc';

            $this->testSortedProducts($field, $label, $mode, ($sortOrder=='asc'));

        }
    }

    /**
     * Test whether sorted products are displayed in the correct order
     * 
     * @param string  $field    Name of the field to compare
     * @param string  $label    Name of the selected sort option
     * @param string  $mode     Current display mode
     * @param boolean $ascOrder Whether it is an ascending order, or not
     *  
     * @return void
     * @access protected
     * @since  3.0.0
     */
    protected function testSortedProducts($field, $label, $mode, $ascOrder)
    {
        $products = $this->getListedProducts($mode);

        $last = null;

        foreach($products as $product) {

            if (is_null($last)) {

                $last = $product;

            } else {

                if ($ascOrder) {

                    $this->assertTrue(
                        in_array($field, array('price')) ? (strnatcasecmp($product['parsed' . ucfirst($field)], $last['parsed' . ucfirst($field)]) >= 0) : (strcasecmp($product[$field] ,$last[$field]) >= 0),
                        "Wrong order ($label label, $field field, '$last[$field]', '$product[$field]', ASC, $mode mode)"
                    );

                } else {

                    $this->assertTrue(
                        in_array($field, array('price')) ? (strnatcasecmp($product['parsed' . ucfirst($field)], $last['parsed' . ucfirst($field)]) <= 0) : (strcasecmp($product[$field], $last[$field]) <= 0),
                        "Wrong order ($label label, $field field, '$last[$field]', '$product[$field]', DESC, $mode mode)"
                    );
                }

                $last = $product;

            }
        }

    }


    /**
     * Test products displayed on a pager page
     * 
     * @param array $allProducts List of all products that are split by the pager into pages
     * @param int   $perPage     Number of products per page
     * @param int   $page        The pager page to be tested
     *  
     * @return void
     * @access protected
     * @since  3.0.0
     */
    protected function testPagerProducts($allProducts, $perPage, $page)
    {
        $mode = $this->getDisplayMode();

        $pageProducts = array_values($this->getListedProducts($mode));

        $pagesCount = ceil(count($allProducts)/$perPage);
        $max = ($page == $pagesCount) ? (count($allProducts)-$perPage*($page-1)) : $perPage;

        $this->assertEquals(
            $max,
            count($pageProducts),
            "The $page pager page displays more than the configured number of products per page ($mode mode)"
        );

        foreach($pageProducts as $n=>$product) {

            $m = ($page-1)*$perPage + $n;

            // Check whether the listed product is on its correct place among all products
            $this->assertTrue(
                isset($allProducts[$m]) && ($allProducts[$m]===$product),
                "The first pager page displays products in a wrong order ($mode mode)"
            );

            // Check the product structure
            $method = 'test'.ucwords($mode).'ProductStructure';
            $this->$method($product);

        }       
       
    }

    /**
     * Test a structure of a product shown in a product list
     * 
     * @param array $product Product data as if it was returned by getListedProducts()
     *  
     * @return void
     * @access protected
     * @since  3.0.0
     */
    protected function testListProductStructure($product)
    {
        $selector = $this->getListSelector() . " .products-list .product.productid-" . $product['id'];

        $this->assertElementPresent(
            "css=$selector h3.product-name a",
            "$selector product misses a linked product name (list mode)"
        );

        $this->assertElementPresent(
            "css=$selector a.product-thumbnail img",
            "$selector product misses a linked product thumbnail (list mode)"
        );

        $this->assertElementPresent(
            "css=$selector .product-description",
            "$selector product misses a product description (list mode)"
        );

        $this->assertElementPresent(
            "css=$selector .product-price",
            "$selector product misses a product price (list mode)"
        );

        $this->assertEquals(
            $product['nameUrl'],
            $product['imgUrl'],
            "Product image and product name link to different pages (list mode)"
        );

        $this->assertEquals(
            $product['name'],
            $product['imgAlt'],
            "Product name differs from the thumbnail alt value (list mode)"
        );

    }

    /**
     * Test a structure of a product shown in a product grid
     * 
     * @param array $product Product data as if it was returned by getListedProducts()
     *  
     * @return void
     * @access protected
     * @since  3.0.0
     */
    protected function testGridProductStructure($product)
    {
        $selector = $this->getListSelector() . " .products-grid .product.productid-" . $product['id'];

        $this->assertElementPresent(
            "css=$selector h3.product-name a",
            "$selector product misses a linked product name (grid mode)"
        );

        $this->assertElementPresent(
            "css=$selector a.product-thumbnail img",
            "$selector product misses a linked product thumbnail (grid mode)"
        );

        $this->assertElementPresent(
            "css=$selector .product-price",
            "$selector product misses a product price (grid mode)"
        );

        $this->assertEquals(
            $product['nameUrl'],
            $product['imgUrl'],
            "Product image and product name link to different pages (grid mode)"
        );

        $this->assertEquals(
            $product['name'],
            $product['imgAlt'],
            "Product name differs from the thumbnail alt value (grid mode)"
        );

    }

    /**
     * Test a structure of a product shown in a product table
     * 
     * @param array $product Product data as if it was returned by getListedProducts()
     *  
     * @return void
     * @access protected
     * @since  3.0.0
     */
    protected function testTableProductStructure($product)
    {
        $id =& $product['id'];

        $selector = $this->getListSelector() . " .products-table .product.productid-$id";

        $this->assertElementPresent(
            "css=$selector a.product-link",
            "$selector product misses a linked product name (table mode)"
        );

        $this->assertElementPresent(
            "css=$selector .product-sku",
            "$selector product misses a product sku (table mode)"
        );

        $this->assertElementPresent(
            "css=$selector .product-price",
            "$selector product misses a product price (table mode)"
        );
/*
        There is no qty field in table mode!

        $inputSelector = "$selector input.product-qty";
        $this->assertElementPresent(
            "css=$inputSelector",
            "$selector product misses a quantity field (table mode)"
        );

        $qtyFieldName = $this->getJSExpression("jQuery('$inputSelector').attr('name')");
        $this->assertEquals(
            "qty[$id]",
            $qtyFieldName,
            "$selector product has a wrong name of the quantity field (table mode)"
        );
*/
    }

    /**
     * Test whether all pager UI elements are displayed and have a correct structure
     * 
     * @param int $total        The total number of products split by the pager
     * @param int $perPage      Number of products per page
     * @param int $selectedPage The page being tested
     *  
     * @return void
     * @access protected
     * @since  3.0.0
     */
    protected function testPagerStructure($total, $perPage, $selectedPage = 1)
    {
        $mode = $this->getDisplayMode();

        $pager1 = $this->getListSelector() . " .list-pager";
        $pager2 = $this->getListSelector() . " .list-pager-bottom";
        $pages = "ul.pager";
        $info = ".pager-items-total";

        $this->assertElementPresent(
            "css=$pager1",
            "Pager container is missing ($mode mode)"
        );

        $from = ($perPage * ($selectedPage-1)) + 1;
        $till = ($perPage*$selectedPage < $total) ? $perPage*$selectedPage : $total;

        $infoElements = array(
            "$info" => "info block is missing",
            "$info .begin-record-number" => "'from' number is missing",
//            "$info .end-record-number" => "'till' number is missing",
//            "$info .records-count" => "'count' number is missing",
//            "$info input.page-length" => "'per page' field is missing",
//            "$info input.page-length[value=$perPage]" => "'per page' field contains wrong value, not '$perPage'",
        );
        foreach ($infoElements as $selector=>$message) {
            $this->assertJqueryPresent("$pager1 $selector", "Pager 1: $message ($mode mode)");
        }

        $infoFields = array(
            "$info .begin-record-number" => $from,
 //           "$info .end-record-number" => $till,
//            "$info .records-count" => $total,
        );
        foreach ($infoFields as $selector=>$value) {
            $html = $this->getJSExpression("jQuery('$pager1 $selector').html()");
            $this->assertEquals(
                $value,
                $html,
                "Pager 1: '$selector' element has a wrong value that does not match '$value' ($mode mode)"
            );
        }

        if ($total > $perPage) {

            // Multiple pages
            $this->assertElementPresent(
                "css=$pager1 $pages",
                "The first pager container doesn't display pages ($mode mode)"
            );
            $this->assertElementPresent(
                "css=$pager2",
                "The second pager container is missing ($mode mode)"
            );
            $this->assertElementPresent(
                "css=$pager2 $pages",
                "The second pager container doesn't display pages ($mode mode)"
            );

            foreach ($infoElements as $selector=>$message) {
                $this->assertJqueryPresent("$pager2 $selector", "Pager 2: $message ($mode mode)");
            }

/*
            foreach ($infoFields as $selector=>$value) {
                $html = $this->getJSExpression("jQuery('$pager2 $selector').html()");
                $this->assertEquals(
                    $html,
                    $value,
                    "Pager 2: '$selector' element has a wrong value that does not match '$value' ($mode mode)"
                );
            }
*/
            $lastPage = ceil($total/$perPage);
            $pageElements = array(
                "li.item:contains($selectedPage)" => "selected page is missing",
                "li.item.selected" => "selected page is not marked",
            );
            if ($selectedPage > 1) {
                $pageElements["li.item.previous-page"] = "link to the previous page is missing";
            }
            if ($selectedPage < $lastPage) {
                $pageElements["li.item.next-page"] = "link to the next page is missing";
            }
            foreach ($pageElements as $selector=>$message) {
                $this->assertJqueryPresent("$pager1 $selector", "Pager 1: $message ($mode mode)");
                $this->assertJqueryPresent("$pager2 $selector", "Pager 2: $message ($mode mode)");
            }

        } else {

            // One page only
            $this->assertElementNotPresent(
                "css=$pager1 $pages",
                "One-page pager displays pages ($mode mode)"
            );
            $this->assertElementNotPresent(
                "css=$pager2",
                "One-page pager displays two pager containers ($mode mode)"
            );

        }

    }

    /**
     * Returns IDs of the products listed inside a container DOM element
     * 
     * @param string $listSelector jQuery selector for the root widget element (the top container element)
     *  
     * @return array
     * @access protected
     * @since  3.0.0
     */
    protected function getListedProducts($mode)
    {
        $this->assertElementPresent(
            "css=" . $this->getListSelector() . " .products-$mode",
            "Display mode selector is missing ($mode mode)"
        );

        $selector = $this->getListSelector() . " .products-$mode .product";
        $cellSelector = "$selector-cell";

        $products = array();

        $count = $this->getJSExpression("jQuery('$selector').size()");
        $cells = $this->getJSExpression("jQuery('$cellSelector').size()");

        $this->assertEquals(
            $count,
            $cells,
            "Number of product cells differs from the number of products"
        );

        for ($i = 0; $i < $count; $i++) {

            $class = $this->getJSExpression("jQuery('$selector').eq($i).attr('class')");

            $id = preg_replace('/^.*productid-([0-9]+).*$/', '\\1', $class);

            $productSelector = "$selector.productid-$id";

            $product = array('id' => $id);    

            $nameSelector               = ($mode=='table') ? "$productSelector a.product-link" : "$productSelector h3.product-name a";

            $product['name']            = $this->getJSExpression("jQuery('$nameSelector').html()");
//            $product['nameUrl']         = $this->getJSExpression("jQuery('$nameSelector')");
            $product['sku']             = $this->getJSExpression("jQuery('$productSelector .product-sku').html()");
            $product['price']           = $this->getJSExpression("jQuery('$productSelector .product-price').html()");
            $product['parsedPrice']     = preg_replace("/^\D*(\d+\.\d+)\D*$/", "\\1", $product['price']);
//            $product['imgUrl']          = $this->getJSExpression("jQuery('$productSelector a.product-thumbnail')");
//            $product['imgSrc']          = $this->getJSExpression("jQuery('$productSelector a.product-thumbnail img').attr('src')");
//            $product['imgAlt']          = $this->getJSExpression("jQuery('$productSelector a.product-thumbnail img').attr('alt')");
//            $product['description']     = $this->getJSExpression("jQuery('$productSelector .product-description').html()");
            
            foreach ($product as $k => $v) {
                $product[$k] = ($v === 'null') ? null : $v;
            }

           $products[$id] = $product;

        }

        return $products;
        
    }

    /**
     * Configure the pager
     * 
     * @param int $itemsPerPage Number of products per page
     * @param int $showSelector Whether users can change the number of products per page, or not
     *  
     * @return void
     * @access protected
     * @since  3.0.0
     */
    protected function configurePager($itemsPerPage, $showSelector = true)
    {
        if ($showSelector) {

            $this->setVisible('ItemsPerPageSelector');

        } else {

            $this->setHidden('ItemsPerPageSelector');

        }

        $allItems = ($itemsPerPage == 'all');

        $this->setWidgetParam($this->getWidgetId(), 'showAllItemsPerPage', $allItems);

        if (!$allItems && (int)$itemsPerPage) {

            $this->setWidgetParam($this->getWidgetId(), 'itemsPerPage', (int)$itemsPerPage);

        }

    }

    /**
     * Switches the first Drupal block displaying the widget to the specifed display mode
     * 
     * @param string $mode    Display mode ('list', 'grid', 'table', 'rotator')
     * @param mixed  $columns The number of columns to be displayed in Grid mode ("css-defined" for a CSS layout)
     *  
     * @return void
     * @access protected
     * @since  3.0.0
     */
    protected function setDisplayMode($mode = 'list', $columns = null)
    {
        $this->currentMode = $mode;

        $this->setWidgetParam($this->getWidgetId(), 'displayMode', $mode);

        if (!is_null($columns)) {

            $this->setWidgetParam($this->getWidgetId(), 'gridColumns', $columns);

        }
    }

    /**
     * Returns the currently selected display modege
     * 
     * @return string
     * @access protected
     * @since  3.0.0
     */
    protected function getDisplayMode()
    {
        return $this->currentMode;
    }

    /**
     * Waits until the progress bar appears and is hidden then
     * 
     * @return void
     * @access protected
     * @since  3.0.0
     */
    protected function waitForAjaxProgress()
    {
        $listSelector = $this->getListSelector();

        // wait until the progress bar appears
        // it is commented due to the fact that sometimes the progress bar disappers faster than selenium checks whether it is visible
        // $this->waitForLocalCondition("jQuery('$listSelector .blockUI.wait-block:visible').length > 0");

        // wait until the progress bar is hidden
        $this->waitForLocalCondition(
            'jQuery("' . $listSelector . ' .blockUI.block-wait:visible").length <= 0',
            300000
        );
 
    }

    /**
     * Set as visible and return visibility 
     * 
     * @param string $part part of widget
     *  
     * @return boolean
     * @access protected
     * @see    ____func_see____
     * @since  3.0.0
     */
    protected function setVisible($part)
    {
        $this->setWidgetParam($this->getWidgetId(), 'show' . $part, true);

        return true;
    }


    /**  
     * Set as hidden and return visibility 
     * 
     * @param string $part part of widget
     *  
     * @return boolean
     * @access protected
     * @see    ____func_see____
     * @since  3.0.0
     */
    protected function setHidden($part)
    {
        $this->setWidgetParam($this->getWidgetId(), 'show' . $part, false);

        return false;
    }

}
Back to Top