Passed
Pull Request — master (#47)
by
unknown
01:32
created

RegistryPageFunctionalTest::testUseLink()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 0
1
<?php
2
3
namespace SilverStripe\Registry\Tests;
4
5
use SilverStripe\Control\Controller;
6
use SilverStripe\Dev\CSSContentParser;
7
use SilverStripe\Dev\FunctionalTest;
8
use SilverStripe\Registry\Tests\Stub\RegistryPageTestContact;
9
use SilverStripe\Registry\Tests\Stub\RegistryPageTestContactExtra;
10
use SilverStripe\Registry\Tests\Stub\RegistryPageTestPage;
11
12
class RegistryPageFunctionalTest extends FunctionalTest
13
{
14
    protected static $fixture_file = [
15
        'fixtures/RegistryPageFunctionalTest.yml',
16
        'fixtures/RegistryPageTestContact.yml',
17
        'fixtures/RegistryPageTestContactExtra.yml'
18
    ];
19
20
    protected static $extra_dataobjects = [
21
        RegistryPageTestContact::class,
22
        RegistryPageTestContactExtra::class,
23
        RegistryPageTestPage::class
24
    ];
25
26
    protected static $use_draft_site = true;
27
28
    public function testUseLink()
29
    {
30
        // Page with links
31
        $page = $this->objFromFixture(RegistryPageTestPage::class, 'contact-registrypage-extra');
32
        $response = $this->get($page->Link());
33
        $parser = new CSSContentParser($response->getBody());
34
35
        $rows = $parser->getBySelector('table.results tbody tr td');
36
37
        $this->assertEquals($rows[0]->a->attributes()->href[0], '/contact-search-extra/1');
38
39
        // Page without links
40
        $page = $this->objFromFixture(RegistryPageTestPage::class, 'contact-registrypage');
41
        $response = $this->get($page->Link());
42
        $parser = new CSSContentParser($response->getBody());
43
44
        $rows = $parser->getBySelector('table.results tbody tr td');
45
46
        $this->assertEquals($rows[0][0], 'Alexander');
47
    }
48
49
    public function testFilteredSearchResults()
50
    {
51
        $page = $this->objFromFixture(RegistryPageTestPage::class, 'contact-registrypage');
52
        $response = $this->get($page->RelativeLink('RegistryFilterForm') . '?' . http_build_query(array(
53
            'FirstName' => 'Alexander',
54
            'action_doRegistryFilter' => 'Filter'
55
        )));
56
57
        $parser = new CSSContentParser($response->getBody());
58
        $rows = $parser->getBySelector('table.results tbody tr');
59
        $cells = $rows[0]->td;
60
61
        $this->assertEquals(1, count($rows));
62
        $this->assertEquals('Alexander', (string) $cells[0]);
63
        $this->assertEquals('Bernie', (string) $cells[1]);
64
    }
65
66
    public function testFilteredByRelationSearchResults()
67
    {
68
        $page = $this->objFromFixture(RegistryPageTestPage::class, 'contact-registrypage-extra');
69
70
        $response = $this->get($page->RelativeLink('RegistryFilterForm') . '?' . http_build_query(array(
71
            'RegistryPageID' => $page->ID,
72
            'action_doRegistryFilter' => 'Filter'
73
        )));
74
75
        $parser = new CSSContentParser($response->getBody());
76
77
        $rows = $parser->getBySelector('table.results tbody tr');
78
        $cells = $rows[0]->td;
79
80
        $this->assertEquals(1, count($rows));
81
        $this->assertEquals('Jimmy', (string) $cells[0]->a[0]);
82
        $this->assertEquals('Sherson', (string) $cells[1]->a[0]);
83
    }
84
85
    public function testSearchResultsLimitAndStart()
86
    {
87
        $page = $this->objFromFixture(RegistryPageTestPage::class, 'contact-registrypage-limit');
88
        $response = $this->get($page->RelativeLink('RegistryFilterForm') . '?' . http_build_query(array(
89
            'Sort' => 'FirstName',
90
            'Dir' => 'DESC',
91
            'action_doRegistryFilter' => 'Filter'
92
        )));
93
94
        $parser = new CSSContentParser($response->getBody());
95
        $rows = $parser->getBySelector('table.results tbody tr');
96
        $anchors = $parser->getBySelector('ul.pageNumbers li a');
97
98
        $this->assertEquals(3, count($rows), 'Limited to 3 search results');
99
        $this->assertEquals(4, count($anchors), '4 paging anchors, including next');
100
101
        $this->assertContains('Sort=FirstName', (string) $anchors[0]['href']);
102
        $this->assertContains('Dir=DESC', (string) $anchors[0]['href']);
103
104
        $this->assertContains('start=0', (string) $anchors[0]['href']);
105
        $this->assertContains('start=3', (string) $anchors[1]['href']);
106
        $this->assertContains('start=6', (string) $anchors[2]['href']);
107
    }
108
109
    public function testGetParamsPopulatesSearchForm()
110
    {
111
        $page = $this->objFromFixture(RegistryPageTestPage::class, 'contact-registrypage');
112
        $response = $this->get($page->RelativeLink('RegistryFilterForm') . '?' . http_build_query(array(
113
            'FirstName' => 'Alexander',
114
            'Sort' => 'FirstName',
115
            'Dir' => 'DESC',
116
            'action_doRegistryFilter' => 'Filter'
117
        )));
118
119
        $parser = new CSSContentParser($response->getBody());
120
        $firstNameField = $parser->getBySelector('#Form_RegistryFilterForm_FirstName');
121
        $sortField = $parser->getBySelector('#Form_RegistryFilterForm_Sort');
122
        $dirField = $parser->getBySelector('#Form_RegistryFilterForm_Dir');
123
124
        $this->assertEquals('Alexander', (string) $firstNameField[0]['value']);
125
        $this->assertEquals('FirstName', (string) $sortField[0]['value']);
126
        $this->assertEquals('DESC', (string) $dirField[0]['value']);
127
    }
128
129
    public function testQueryLinks()
130
    {
131
        $page = $this->objFromFixture(RegistryPageTestPage::class, 'contact-registrypage');
132
        $response = $this->get($page->RelativeLink('RegistryFilterForm') . '?' . http_build_query(array(
133
            'FirstName' => 'Alexander',
134
            'action_doRegistryFilter' => 'Filter'
135
        )));
136
137
        $parser = new CSSContentParser($response->getBody());
138
        $rows = $parser->getBySelector('table.results thead tr');
139
        $anchors = $rows[0]->th->a;
140
141
        $this->assertContains('FirstName=Alexander', (string) $anchors[0]['href']);
142
        $this->assertContains('Surname=', (string) $anchors[0]['href']);
143
        $this->assertContains('Sort=FirstName', (string) $anchors[0]['href']);
144
        $this->assertContains('Dir=ASC', (string) $anchors[0]['href']);
145
        $this->assertContains('action_doRegistryFilter=Filter', (string) $anchors[0]['href']);
146
    }
147
148
    public function testShowExistingRecord()
149
    {
150
        $record = $this->objFromFixture(RegistryPageTestContact::class, 'alexander');
151
        $page = $this->objFromFixture(RegistryPageTestPage::class, 'contact-registrypage');
152
        $response = $this->get(Controller::join_links($page->RelativeLink(), 'show', $record->ID));
153
154
        $this->assertContains('Alexander Bernie', $response->getBody());
155
    }
156
157
    public function testPageNotFoundNonExistantRecord()
158
    {
159
        $page = $this->objFromFixture(RegistryPageTestPage::class, 'contact-registrypage');
160
        $response = $this->get(Controller::join_links($page->RelativeLink(), 'show', '123456'));
161
        $this->assertEquals(404, $response->getStatusCode());
162
    }
163
164
    public function testColumnName()
165
    {
166
        $page = $this->objFromFixture(RegistryPageTestPage::class, 'contact-registrypage');
167
        $response = $this->get($page->RelativeLink('RegistryFilterForm') . '?' . http_build_query(array(
168
            'action_doRegistryFilter' => 'Filter'
169
        )));
170
171
        $parser = new CSSContentParser($response->getBody());
172
        $rows = $parser->getBySelector('table.results thead tr');
173
        $anchors = $rows[0]->th->a;
174
175
        $this->assertEquals('First name', (string) $anchors[0]);
176
    }
177
178
    public function testSortableColumns()
179
    {
180
        $page = $this->objFromFixture(RegistryPageTestPage::class, 'contact-registrypage-extra');
181
        $response = $this->get($page->Link());
182
        $parser = new CSSContentParser($response->getBody());
183
        $columns = $parser->getBySelector('table.results thead tr th');
184
185
        $this->assertNotEmpty($columns[0]->a);
186
        $this->assertNotEmpty($columns[1]->a);
187
        $this->assertNotEmpty($columns[2]->a);
188
        $this->assertEquals('Other', $columns[3]);
189
    }
190
191
    public function testExportLink()
192
    {
193
        $page = $this->objFromFixture(RegistryPageTestPage::class, 'contact-registrypage');
194
        $response = $this->get($page->RelativeLink('RegistryFilterForm') . '?' . http_build_query(array(
195
            'FirstName' => 'Alexander',
196
            'Sort' => 'FirstName',
197
            'Dir' => 'DESC',
198
            'action_doRegistryFilter' => 'Filter'
199
        )));
200
201
        $parser = new CSSContentParser($response->getBody());
202
        $anchor = $parser->getBySelector('a.export');
203
204
        $this->assertContains('export?', (string) $anchor[0]['href']);
205
        $this->assertContains('FirstName=Alexander', (string) $anchor[0]['href']);
206
        $this->assertContains('Surname=', (string) $anchor[0]['href']);
207
        $this->assertContains('Sort=FirstName', (string) $anchor[0]['href']);
208
        $this->assertContains('Dir=DESC', (string) $anchor[0]['href']);
209
        $this->assertContains('action_doRegistryFilter=Filter', (string) $anchor[0]['href']);
210
    }
211
}
212