Passed
Pull Request — master (#50)
by
unknown
01:25
created

testFilteredByRelationIDSearchResults()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 24
rs 9.7998
c 0
b 0
f 0
cc 1
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
    ];
18
19
    protected static $extra_dataobjects = [
20
        RegistryPageTestContact::class,
21
        RegistryPageTestContactExtra::class,
22
        RegistryPageTestPage::class
23
    ];
24
25
    protected static $use_draft_site = true;
26
27
    public function testUseLink()
28
    {
29
        // Page with links
30
        $page = $this->objFromFixture(RegistryPageTestPage::class, 'contact-registrypage-extra');
31
        $response = $this->get($page->Link());
32
        $parser = new CSSContentParser($response->getBody());
33
34
        $cells = $parser->getBySelector('table.results tbody tr td');
35
36
        $this->assertContains('/contact-search-extra/', (string) $cells[0]->a->attributes()->href[0]);
37
    }
38
39
    public function testFilteredSearchResults()
40
    {
41
        $page = $this->objFromFixture(RegistryPageTestPage::class, 'contact-registrypage');
42
        $uri = Controller::join_links(
43
            $page->RelativeLink('RegistryFilterForm'),
44
            '?' .
45
            http_build_query(array(
46
                'FirstName' => 'Alexander',
47
                'action_doRegistryFilter' => 'Filter'
48
            ))
49
        );
50
        $response = $this->get($uri);
51
52
        $parser = new CSSContentParser($response->getBody());
53
        $rows = $parser->getBySelector('table.results tbody tr');
54
55
        $cells = $rows[0]->td;
56
57
        $this->assertCount(1, $rows);
58
        $this->assertEquals('Alexander', (string) $cells[0]);
59
        $this->assertEquals('Bernie', (string) $cells[1]);
60
    }
61
62
    public function testFilteredByRelationSearchResults()
63
    {
64
        $page = $this->objFromFixture(RegistryPageTestPage::class, 'contact-registrypage-extra');
65
        $uri = Controller::join_links(
66
            $page->RelativeLink('RegistryFilterForm'),
67
            '?' . http_build_query(array(
68
                'RegistryPage.Title' => $page->Title,
69
                'action_doRegistryFilter' => 'Filter'
70
            ))
71
        );
72
73
        $response = $this->get($uri);
74
75
        $parser = new CSSContentParser($response->getBody());
76
77
        $rows = $parser->getBySelector('table.results tbody tr');
78
        $cells = $rows[0]->td;
79
80
        $this->assertCount(1, $rows);
81
        $this->assertEquals('Jimmy', (string) $cells[0]->a[0]);
82
        $this->assertEquals('Sherson', (string) $cells[1]->a[0]);
83
    }
84
85
    /**
86
     * Check that RegistryPageController can filter for ExactMatches (ID) for relationships.
87
     *
88
     * @throws \Exception
89
     */
90
    public function testFilteredByRelationIDSearchResults()
91
    {
92
        $page = $this->objFromFixture(RegistryPageTestPage::class, 'contact-registrypage-extra');
93
        $uri = Controller::join_links(
94
            $page->RelativeLink('RegistryFilterForm'),
95
            '?' . http_build_query(array(
96
                'RegistryPage.ID' => 2,
97
                'action_doRegistryFilter' => 'Filter',
98
            ))
99
        );
100
101
        // If this is wrong then the configuration system is broken.
102
        $this->assertCount(4, $page->getDataSingleton()->config()->get('searchable_fields'));
103
104
        $response = $this->get($uri);
105
106
        $parser = new CSSContentParser($response->getBody());
107
108
        $rows = $parser->getBySelector('table.results tbody tr');
109
        $cells = $rows[0]->td;
110
111
        $this->assertCount(1, $rows);
112
        $this->assertEquals('Jimmy', (string)trim($cells[0]->a[0]));
113
        $this->assertEquals('Sherson', (string)trim($cells[1]->a[0]));
114
    }
115
116
    public function testUserCustomSummaryField()
117
    {
118
        $page = $this->objFromFixture(RegistryPageTestPage::class, 'contact-registrypage-extra');
119
        $response = $this->get($page->Link());
120
        $parser = new CSSContentParser($response->getBody());
121
122
        $cells = $parser->getBySelector('table.results tbody tr td');
123
124
        $this->assertContains($page->getDataSingleton()->getStaticReference(), (string) $cells[3]->a[0]);
125
    }
126
127
    public function testSearchResultsLimitAndStart()
128
    {
129
        $page = $this->objFromFixture(RegistryPageTestPage::class, 'contact-registrypage-limit');
130
        $uri = Controller::join_links(
131
            $page->RelativeLink('RegistryFilterForm'),
132
            '?' . http_build_query(array(
133
                'Sort' => 'FirstName',
134
                'Dir' => 'DESC',
135
                'action_doRegistryFilter' => 'Filter'
136
            ))
137
        );
138
139
        $response = $this->get($uri);
140
141
        $parser = new CSSContentParser($response->getBody());
142
        $rows = $parser->getBySelector('table.results tbody tr');
143
        $anchors = $parser->getBySelector('ul.pageNumbers li a');
144
145
        $this->assertCount(3, $rows, 'Limited to 3 search results');
146
        $this->assertCount(4, $anchors, '4 paging anchors, including next');
147
148
        $this->assertContains('Sort=FirstName', (string) $anchors[0]['href']);
149
        $this->assertContains('Dir=DESC', (string) $anchors[0]['href']);
150
151
        $this->assertContains('start=0', (string) $anchors[0]['href']);
152
        $this->assertContains('start=3', (string) $anchors[1]['href']);
153
        $this->assertContains('start=6', (string) $anchors[2]['href']);
154
    }
155
156
    public function testGetParamsPopulatesSearchForm()
157
    {
158
        $page = $this->objFromFixture(RegistryPageTestPage::class, 'contact-registrypage');
159
        $uri = Controller::join_links(
160
            $page->RelativeLink('RegistryFilterForm'),
161
            '?' . http_build_query(array(
162
                'FirstName' => 'Alexander',
163
                'Sort' => 'FirstName',
164
                'Dir' => 'DESC',
165
                'action_doRegistryFilter' => 'Filter'
166
            ))
167
        );
168
        $response = $this->get($uri);
169
170
        $parser = new CSSContentParser($response->getBody());
171
        $firstNameField = $parser->getBySelector('#Form_RegistryFilterForm_FirstName');
172
        $sortField = $parser->getBySelector('#Form_RegistryFilterForm_Sort');
173
        $dirField = $parser->getBySelector('#Form_RegistryFilterForm_Dir');
174
175
        $this->assertEquals('Alexander', (string) $firstNameField[0]['value']);
176
        $this->assertEquals('FirstName', (string) $sortField[0]['value']);
177
        $this->assertEquals('DESC', (string) $dirField[0]['value']);
178
    }
179
180
    public function testQueryLinks()
181
    {
182
        $page = $this->objFromFixture(RegistryPageTestPage::class, 'contact-registrypage');
183
        $uri = Controller::join_links(
184
            $page->RelativeLink('RegistryFilterForm'),
185
            '?' . http_build_query(array(
186
                'FirstName' => 'Alexander',
187
                'action_doRegistryFilter' => 'Filter'
188
            ))
189
        );
190
        $response = $this->get($uri);
191
192
        $parser = new CSSContentParser($response->getBody());
193
        $rows = $parser->getBySelector('table.results thead tr');
194
        $anchors = $rows[0]->th->a;
195
196
        $this->assertContains('FirstName=Alexander', (string) $anchors[0]['href']);
197
        $this->assertContains('Surname=', (string) $anchors[0]['href']);
198
        $this->assertContains('Sort=FirstName', (string) $anchors[0]['href']);
199
        $this->assertContains('Dir=ASC', (string) $anchors[0]['href']);
200
        $this->assertContains('action_doRegistryFilter=Filter', (string) $anchors[0]['href']);
201
    }
202
203
    public function testShowExistingRecord()
204
    {
205
        $record = $this->objFromFixture(RegistryPageTestContact::class, 'alexander');
206
        $page = $this->objFromFixture(RegistryPageTestPage::class, 'contact-registrypage');
207
        $response = $this->get(Controller::join_links($page->RelativeLink(), 'show', $record->ID));
208
209
        $this->assertContains('Alexander Bernie', $response->getBody());
210
    }
211
212
    public function testPageNotFoundNonExistantRecord()
213
    {
214
        $page = $this->objFromFixture(RegistryPageTestPage::class, 'contact-registrypage');
215
        $response = $this->get(Controller::join_links($page->RelativeLink(), 'show', '123456'));
216
        $this->assertEquals(404, $response->getStatusCode());
217
    }
218
219
    public function testColumnName()
220
    {
221
        $page = $this->objFromFixture(RegistryPageTestPage::class, 'contact-registrypage');
222
        $uri = Controller::join_links(
223
            $page->RelativeLink('RegistryFilterForm'),
224
            '?' . http_build_query(array(
225
                'action_doRegistryFilter' => 'Filter'
226
            ))
227
        );
228
        $response = $this->get($uri);
229
230
        $parser = new CSSContentParser($response->getBody());
231
        $rows = $parser->getBySelector('table.results thead tr');
232
        $anchors = $rows[0]->th->a;
233
234
        $this->assertEquals('First name', (string) $anchors[0]);
235
    }
236
237
    public function testSortableColumns()
238
    {
239
        $page = $this->objFromFixture(RegistryPageTestPage::class, 'contact-registrypage-extra');
240
        $response = $this->get($page->Link());
241
        $parser = new CSSContentParser($response->getBody());
242
        $columns = $parser->getBySelector('table.results thead tr th');
243
244
        $this->assertNotEmpty($columns[0]->a);
245
        $this->assertNotEmpty($columns[1]->a);
246
        $this->assertNotEmpty($columns[2]->a);
247
        $this->assertEquals('Other', $columns[3]);
248
    }
249
250
    public function testExportLink()
251
    {
252
        $page = $this->objFromFixture(RegistryPageTestPage::class, 'contact-registrypage');
253
        $uri = Controller::join_links(
254
            $page->RelativeLink('RegistryFilterForm'),
255
            '?' . http_build_query(array(
256
                'FirstName' => 'Alexander',
257
                'Sort' => 'FirstName',
258
                'Dir' => 'DESC',
259
                'action_doRegistryFilter' => 'Filter'
260
            ))
261
        );
262
        $response = $this->get($uri);
263
264
        $parser = new CSSContentParser($response->getBody());
265
        $anchor = $parser->getBySelector('a.export');
266
267
        $this->assertContains('export?', (string) $anchor[0]['href']);
268
        $this->assertContains('FirstName=Alexander', (string) $anchor[0]['href']);
269
        $this->assertContains('Surname=', (string) $anchor[0]['href']);
270
        $this->assertContains('Sort=FirstName', (string) $anchor[0]['href']);
271
        $this->assertContains('Dir=DESC', (string) $anchor[0]['href']);
272
        $this->assertContains('action_doRegistryFilter=Filter', (string) $anchor[0]['href']);
273
    }
274
}
275