Completed
Push — master ( 10097e...bb90b3 )
by Simon
02:03
created

ShortlistTest::testShortListControllerPagination()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 7
Ratio 100 %

Importance

Changes 0
Metric Value
dl 7
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
class ShortlistTest extends FunctionalTest
4
{
5
    protected static $fixture_file = 'fixtures.yml';
6
    protected $fixtureFactory = null;
7
8
    public function setUpOnce()
9
    {
10
        parent::setUpOnce();
11
12
        $this->fixtureFactory = Injector::inst()->create('FixtureFactory');
13
    }
14
15
    public function setup()
16
    {
17
        parent::setup();
18
19
        SecurityToken::enable();
20
    }
21
22
    public function teardown()
23
    {
24
        parent::teardown();
25
26
        SecurityToken::disable();
27
    }
28
29
    public function testShortlistCreation()
30
    {
31
        $shortlists = ShortList::get();
32
33
        $this->assertNotEquals($shortlists->Count(), 0);
34
    }
35
36
    /*
37
    |--------------------------------------------------------------------------
38
    | Add item to short list DBO.
39
    |--------------------------------------------------------------------------
40
    */
41
    public function testAddItemtoShortList()
42
    {
43
        $shortlist = $this->fixtureFactory->get('ShortList', 'test');
44
        $item = $this->fixtureFactory->get('ShortListItem', 'item1');
45
46
        $this->assertEquals($shortlist->ShortListItems()->Count(), 0);
47
48
        // add to the shortlist.
49
        $shortlist->ShortListItems()->add($item);
50
51
        $retrievedItem = $shortlist->ShortListItems()->filter('ID', $item->ID)->first()->getActualRecord();
52
53
        // Check the item has been saved correctly
54
        $this->assertEquals($retrievedItem->ClassName, 'Page');
55
        $this->assertEquals($retrievedItem->ID, $item->ID);
56
57
        // do we have an item added?
58
        $this->assertNotEquals($shortlist->ShortListItems()->Count(), 0);
59
        // what about the reverse side?
60
        $this->assertEquals($item->ShortList()->ID, $shortlist->ID);
61
    }
62
63
    /*
64
    |--------------------------------------------------------------------------
65
    | Remove item from shortlist DBO.
66
    |--------------------------------------------------------------------------
67
    */
68
    public function testRemoveItemFromShortList()
69
    {
70
        $testpage = $this->objFromFixture('Page', 'page1');
71
72
        // create the shortlist
73
        $shortlist = new ShortList();
74
        $shortlist->write();
75
76
        $this->assertEquals($shortlist->ShortListItems()->Count(), 0);
77
78
        // create the item.
79
        $item = new ShortListItem();
80
        $item->ItemType = $testpage->ClassName;
81
        $item->ItemID = $testpage->ID;
82
        $item->write();
83
84
        // add to the shortlist.
85
        $shortlist->ShortListItems()->add($item);
86
        $shortlist->write();
87
88
        // do we have an item added?
89
        $this->assertNotEquals($shortlist->ShortListItems()->Count(), 0);
90
        // what about the reverse side?
91
        $this->assertNotEquals($item->ShortList()->ID, 0);
92
93
        $shortlist->ShortListItems()->remove($item);
94
95
            // do we have an item removed?
96
        $this->assertEquals($shortlist->ShortListItems()->Count(), 0);
97
    }
98
99
    /*
100
    |--------------------------------------------------------------------------
101
    | Check whether we are or are not a browser.
102
    |--------------------------------------------------------------------------
103
    */
104
    public function testisBrowser()
105
    {
106
        $list = $this->objFromFixture('ShortList', 'test');
107
        $ua = Controller::curr()->getRequest()->getHeader('User-Agent');
108
109
        if (empty(strtolower($ua))) {
110
            $this->assertFalse($list->isBrowser());
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<ShortlistTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
111
        } else {
112
            $this->assertTrue($list->isBrowser());
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<ShortlistTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
113
        }
114
    }
115
116
    /*
117
    |--------------------------------------------------------------------------
118
    | Check to see that the generated URL contains the config URL segment
119
    | and the genertated random URL.
120
    |--------------------------------------------------------------------------
121
    */
122
    public function testShortListLink()
123
    {
124
        $list = $this->objFromFixture('ShortList', 'test');
125
        $list->write();
126
127
        $configURL = Config::inst()->get('ShortList', 'URLSegment');
128
        $link = $list->Link();
129
130
        $configPos = strpos($link, $configURL);
131
        $linkPos = strpos($link, $list->URL);
132
133
        $this->assertGreaterThan($configPos, $linkPos);
0 ignored issues
show
Bug introduced by
The method assertGreaterThan() does not seem to exist on object<ShortlistTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
134
    }
135
136
    /*
137
    |--------------------------------------------------------------------------
138
    | Test opening the shortlist controller url.
139
    |--------------------------------------------------------------------------
140
    */
141 View Code Duplication
    public function testShortListController()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
142
    {
143
        $url = Config::inst()->get('ShortList', 'URLSegment');
144
        $response = $this->get($url);
145
146
        // Legit response?
147
        $this->assertEquals($response->getStatusCode(), 200);
148
    }
149
150
    /*
151
    |--------------------------------------------------------------------------
152
    | Add via the controller.
153
    |--------------------------------------------------------------------------
154
    */
155
    public function testShortListControllerAdd()
156
    {
157
        $url = Config::inst()->get('ShortList', 'URLSegment');
158
        $list = ShortListController::getShortListSession();
159
160
        /*
161
        |--------------------------------------------------------------------------
162
        | There should not be anything in the shortlist
163
        |--------------------------------------------------------------------------
164
        */
165
        $this->assertEmpty($list);
1 ignored issue
show
Bug introduced by
The method assertEmpty() does not seem to exist on object<ShortlistTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
166
167
        $id = $this->fixtureFactory->getId('Page', 'page1');
168
        $response = $this->get($url . 'add/?id=' . $id . '&type=Page&s=' . Utilities::getSecurityToken());
0 ignored issues
show
Unused Code introduced by
$response is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
169
170
        $list = ShortListController::getShortListSession();
171
172
        $this->assertNotEmpty($list);
1 ignored issue
show
Bug introduced by
The method assertNotEmpty() does not seem to exist on object<ShortlistTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
173
        $this->assertEquals($list->ShortListItems()->count(), 1);
174
    }
175
176
    /*
177
    |--------------------------------------------------------------------------
178
    | Remove an item from the list.
179
    |--------------------------------------------------------------------------
180
    */
181
    public function testShortListControllerRemove()
182
    {
183
        /*
184
        |--------------------------------------------------------------------------
185
        | First add to the shortlist.
186
        |--------------------------------------------------------------------------
187
        */
188
        $url = Config::inst()->get('ShortList', 'URLSegment');
189
        $list = ShortListController::getShortListSession();
190
191
        $this->assertEmpty($list);
1 ignored issue
show
Bug introduced by
The method assertEmpty() does not seem to exist on object<ShortlistTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
192
193
        $id = $this->fixtureFactory->getId('Page', 'page1');
194
        $response = $this->get($url . 'add/?id=' . $id . '&type=Page&s=' . Utilities::getSecurityToken());
0 ignored issues
show
Unused Code introduced by
$response is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
195
196
        $list = ShortListController::getShortListSession();
197
198
        $this->assertNotEmpty($list);
1 ignored issue
show
Bug introduced by
The method assertNotEmpty() does not seem to exist on object<ShortlistTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
199
        $this->assertEquals($list->ShortListItems()->count(), 1);
200
201
        $this->get($url . 'remove/?id=' . $id . '&type=Page&s=' . Utilities::getSecurityToken());
202
        $this->assertEquals($list->ShortListItems()->count(), 0);
203
    }
204
205
    /*
206
    |--------------------------------------------------------------------------
207
    | Paginate the page.
208
    |--------------------------------------------------------------------------
209
    */
210 View Code Duplication
    public function testShortListControllerPagination()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
211
    {
212
        $url = Config::inst()->get('ShortList', 'URLSegment');
213
        $response = $this->get($url . '?page=1');
214
215
        $this->assertEquals($response->getStatusCode(), 200);
216
    }
217
218
    /*
219
    |--------------------------------------------------------------------------
220
    | Visit the index after the shortlist has been created. We should be
221
    | redirected to the appropriate url - e.g. /shortlist/<uniqueurl>
222
    |--------------------------------------------------------------------------
223
    */
224 View Code Duplication
    public function testShortListControllerReIndex()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
225
    {
226
        /*
227
        |--------------------------------------------------------------------------
228
        | First add to the shortlist.
229
        |--------------------------------------------------------------------------
230
        */
231
        $url = Config::inst()->get('ShortList', 'URLSegment');
232
        $list = ShortListController::getShortListSession();
233
234
        $this->assertEmpty($list);
1 ignored issue
show
Bug introduced by
The method assertEmpty() does not seem to exist on object<ShortlistTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
235
236
        $id = $this->fixtureFactory->getId('Page', 'page1');
237
        $response = $this->get($url . 'add/?id=' . $id . '&type=Page&s=' . Utilities::getSecurityToken());
0 ignored issues
show
Unused Code introduced by
$response is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
238
239
        $response = $this->get($url);
0 ignored issues
show
Unused Code introduced by
$response is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
240
    }
241
242 View Code Duplication
    public function testShortListControllerInvalidAdd()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
243
    {
244
        $url = Config::inst()->get('ShortList', 'URLSegment');
245
        $list = ShortListController::getShortListSession();
246
247
        /*
248
        |--------------------------------------------------------------------------
249
        | There should not be anything in the shortlist
250
        |--------------------------------------------------------------------------
251
        */
252
        $this->assertEmpty($list);
1 ignored issue
show
Bug introduced by
The method assertEmpty() does not seem to exist on object<ShortlistTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
253
254
        $id = $this->fixtureFactory->getId('Page', 'page1');
255
        $response = $this->get($url . 'add/?id=' . $id . '&type=Page');
256
257
        $this->assertEquals($response->getStatusCode(), 404);
258
    }
259
}
260