Completed
Push — master ( 3c661d...2a57f9 )
by Will
26s queued 12s
created

tests/php/Model/Product/ProductOrderItemTest.php (3 issues)

1
<?php
2
3
namespace SilverShop\Tests\Model\Product;
4
5
use SilverShop\Cart\ShoppingCart;
6
use SilverShop\Model\Product\OrderItem;
7
use SilverShop\Page\Product;
8
use SilverShop\Tests\ShopTest;
9
use SilverStripe\Dev\FunctionalTest;
10
use SilverStripe\Security\SecurityToken;
11
12
/**
13
 * @package    shop
14
 * @subpackage tests
15
 */
16
class ProductOrderItemTest extends FunctionalTest
17
{
18
    public static $fixture_file = __DIR__ . '/../../Fixtures/shop.yml';
19
    public static $disable_theme = true;
20
    public static $orig = array();
21
22
    /**
23
     * @var Product
24
     */
25
    protected $mp3player;
26
27
    /**
28
     * @var Product
29
     */
30
    protected $socks;
31
32
    /**
33
     * @var Product
34
     */
35
    protected $beachball;
36
37
    /**
38
     * @var Product
39
     */
40
    protected $hdtv;
41
42
    /**
43
     * Create and publish some products.
44
     */
45
    public function setUp()
46
    {
47
        parent::setUp();
48
        ShoppingCart::singleton()->clear();
49
        ShopTest::setConfiguration();
50
51
        $this->logInWithPermission('ADMIN');
52
        $this->mp3player = $this->objFromFixture(Product::class, 'mp3player');
53
        $this->socks = $this->objFromFixture(Product::class, 'socks');
54
        $this->beachball = $this->objFromFixture(Product::class, 'beachball');
55
        $this->hdtv = $this->objFromFixture(Product::class, 'hdtv');
56
57
        $this->mp3player->publishSingle();
58
        $this->socks->publishSingle();
59
        $this->beachball->publishSingle();
60
        $this->hdtv->publishSingle();
61
62
        $this->cart = ShoppingCart::singleton();
0 ignored issues
show
Bug Best Practice introduced by
The property cart does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
63
    }
64
65
    public function testEmptyItem()
66
    {
67
        $emptyitem = $this->mp3player->Item();
68
        $this->assertEquals(1, $emptyitem->Quantity, "Items always have a quantity of at least 1.");
69
    }
70
71
    /**
72
     * Test product updates. These may be caused by an admin, causing everyone's cart to update.
73
     */
74
    public function testProductVersionUpdate()
75
    {
76
        $this->cart->add($this->socks);
77
        $currentorder = $this->cart->current();
0 ignored issues
show
The assignment to $currentorder is dead and can be removed.
Loading history...
78
        $itembefore = $this->cart->get($this->socks);
79
        $this->assertEquals($itembefore->UnitPrice(), 8, "unit price matches product price");
80
        //update product details, whilst items still incart
81
        $this->socks->BasePrice = 9;
0 ignored issues
show
Documentation Bug introduced by
It seems like 9 of type integer is incompatible with the declared type SilverStripe\ORM\FieldType\DBCurrency of property $BasePrice.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
82
        $this->socks->writeToStage('Stage');
83
        $this->socks->publishSingle();
84
        $itemafter = $this->cart->get($this->socks);
85
        $this->assertEquals($itemafter->UnitPrice(), 9, "unit price matches updated product price");
86
    }
87
88
    /**
89
     * Tries to create an order item with a non-existent version.
90
     */
91
    public function testProductVersionDoesNotExist()
92
    {
93
        $brokenitem = OrderItem::create()->update(
94
            [
95
                "ProductID" => $this->socks->ID,
96
                "ProductVersion" => 99999 //non existent version
97
            ]
98
        );
99
        $this->assertNull($brokenitem->Product(), "version does not exist");
100
    }
101
102
    /**
103
     * Check  the links are accurate
104
     */
105
    public function testLinks()
106
    {
107
        SecurityToken::disable();
108
        $product = $this->socks;
109
        $item = $product->Item();
110
        $this->assertEquals(
111
            "shoppingcart/add/SilverShop-Page-Product/{$product->ID}",
112
            $item->addLink()
113
        );
114
        $this->assertEquals(
115
            "shoppingcart/remove/SilverShop-Page-Product/{$product->ID}",
116
            $item->removeLink()
117
        );
118
        $this->assertEquals(
119
            "shoppingcart/removeall/SilverShop-Page-Product/{$product->ID}",
120
            $item->removeallLink()
121
        );
122
        $this->assertEquals(
123
            "shoppingcart/setquantity/SilverShop-Page-Product/{$product->ID}",
124
            $item->setquantityLink()
125
        );
126
    }
127
128
    /**
129
     * Coverage for a bug where there's an error generating the link when ProductID = 0
130
     */
131
    public function testCorruptedOrderItemLinks()
132
    {
133
        SecurityToken::disable();
134
        $product = $this->socks;
135
        $item = $product->Item();
136
        $item->ProductID = 0;
137
        $this->assertEquals('', $item->removeLink());
138
    }
139
}
140