Issues (404)

tests/SearchTest.php (1 issue)

Labels
Severity
1
<?php
2
3
/**
4
 * Provides test methods for search functionality.
5
 * Currently only the highlighting and constituency search.
6
 */
7
8
class SearchTest extends FetchPageTestCase {
9
    public function setUp(): void {
10
        parent::setUp();
11
        include_once('www/includes/easyparliament/searchengine.php');
12
    }
13
14
    public function getDataSet() {
15
        return $this->createMySQLXMLDataSet(dirname(__FILE__) . '/_fixtures/search.xml');
0 ignored issues
show
Are you sure the usage of $this->createMySQLXMLDat.../_fixtures/search.xml') targeting TWFY_Database_TestCase::createMySQLXMLDataSet() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
16
    }
17
18
    private function fetch_page($vars) {
19
        return $this->base_fetch_page($vars, 'search');
20
    }
21
22
    public function testConstituencySearch() {
23
        $this->assertEquals(
24
            [ [ 'Alyn and Deeside' ], false ],
25
            \MySociety\TheyWorkForYou\Utility\Search::searchConstituenciesByQuery('Alyn')
26
        );
27
        $this->assertEquals(
28
            [ [ 'Alyn and Deeside' ], false ],
29
            \MySociety\TheyWorkForYou\Utility\Search::searchConstituenciesByQuery('Alyn and Deeside')
30
        );
31
    }
32
33
    /**
34
     * Test looking up a person by a single name works as expected.
35
     */
36
37
    public function testSearchMemberDbLookupSingleName() {
38
        // Test a single (first) name.
39
        $results = \MySociety\TheyWorkForYou\Utility\Search::searchMemberDbLookup('Joseph');
40
41
        $this->assertEquals(1, count($results));
42
        $this->assertEquals(2, $results[0]);
43
44
        // Test a single (last) name.
45
        $results = \MySociety\TheyWorkForYou\Utility\Search::searchMemberDbLookup('Bloggs');
46
47
        $this->assertEquals(1, count($results));
48
        $this->assertEquals(2, $results[0]);
49
50
    }
51
52
    /**
53
     * Test looking up a person by full name works as expected.
54
     */
55
56
    public function testSearchMemberDbLookupFullName() {
57
58
        // Test a full name.
59
        $results = \MySociety\TheyWorkForYou\Utility\Search::searchMemberDbLookup('Mary Smith');
60
61
        $this->assertEquals(1, count($results));
62
        $this->assertEquals(3, $results[0]);
63
64
        // Test an inverse full name.
65
        $results = \MySociety\TheyWorkForYou\Utility\Search::searchMemberDbLookup('Smith Mary');
66
67
        $this->assertEquals(1, count($results));
68
        $this->assertEquals(3, $results[0]);
69
70
        // Test a name with title.
71
        $results = \MySociety\TheyWorkForYou\Utility\Search::searchMemberDbLookup('Mrs Smith');
72
73
        $this->assertEquals(1, count($results));
74
        $this->assertEquals(3, $results[0]);
75
76
    }
77
78
    /**
79
     * Test that glossarising a single word works as expected.
80
     *
81
     * @group xapian
82
     */
83
    public function testSearchNormal() {
84
        $SEARCHENGINE = new SEARCHENGINE('test');
85
86
        $this->assertEquals(
87
            'This is a <span class="hi">test</span> of the highlighting.',
88
            $SEARCHENGINE->highlight('This is a test of the highlighting.')
89
        );
90
    }
91
92
    /**
93
     * Test that search term highlighting skips tag attributes
94
     *
95
     * @group xapian
96
     */
97
    public function testSearchLink() {
98
        $SEARCHENGINE = new SEARCHENGINE('test');
99
100
        $this->assertEquals(
101
            '<a href="/mp/?m=40584" title="Our page on Mr Test - \'the Member for Birmingham (Mr Test)\'">Mr <span class="hi">Test</span></a>',
102
            $SEARCHENGINE->highlight('<a href="/mp/?m=40584" title="Our page on Mr Test - \'the Member for Birmingham (Mr Test)\'">Mr Test</a>')
103
        );
104
    }
105
106
    /**
107
     * Test fetching the search page
108
     *
109
     * @group xapian
110
     */
111
    public function testSearchPage() {
112
        $page = $this->fetch_page([ ]);
113
        $this->assertStringContainsString('Search', $page);
114
    }
115
116
    /**
117
     * Test searching for an MP
118
     *
119
     * @group xapian
120
     */
121
    public function testSearchPageMP() {
122
        $page = $this->fetch_page([ 'q' => 'Mary Smith' ]);
123
        $this->assertStringContainsString('Mary Smith', $page);
124
        $this->assertStringContainsString('MP, Amber Valley', $page);
125
    }
126
127
    /**
128
     * Test that matches for multiple MPs are displayed
129
     *
130
     * @group xapian
131
     */
132
    public function testSearchPageMultipleMP() {
133
        $page = $this->fetch_page([ 'q' => 'Jones' ]);
134
        $this->assertStringContainsString('People matching <em class="current-search-term">Jones</em>', $page);
135
        $this->assertStringContainsString('Andrew Jones', $page);
136
        $this->assertStringContainsString('Simon Jones', $page);
137
    }
138
139
    /**
140
     * Test that matching a consituency name lists the MP
141
     *
142
     * @group xapian
143
     */
144
    public function testSearchPageCons() {
145
        $page = $this->fetch_page([ 'q' => 'Amber' ]);
146
        $this->assertStringContainsString('MP for <em class="current-search-term">Amber</em>', $page);
147
        $this->assertStringContainsString('Mary Smith', $page);
148
    }
149
150
    /**
151
     * Test that if the matching constituency does not have an MP the
152
     * exception is handled
153
     *
154
     * @group xapian
155
     */
156
    public function testSearchPageConsWithNoMp() {
157
        $page = $this->fetch_page([ 'q' => 'Alyn' ]);
158
        $this->assertStringNotContainsString('MP for <em class="current-search-term">Alyn</em>', $page);
159
        $this->assertStringNotContainsString('MPs in constituencies matching', $page);
160
    }
161
162
    /**
163
     * Test that if the search term matched multiple constituency names the
164
     * MPs for all of them are displayed
165
     *
166
     * @group xapian
167
     */
168
    public function testSearchPageMultipleCons() {
169
        $page = $this->fetch_page([ 'q' => 'Liverpool' ]);
170
        $this->assertStringContainsString('MPs in constituencies matching <em class="current-search-term">Liverpool</em>', $page);
171
        $this->assertStringContainsString('Susan Brown', $page);
172
        $this->assertStringContainsString('MP, Liverpool, Riverside', $page);
173
        $this->assertStringContainsString('Andrew Jones', $page);
174
        $this->assertStringContainsString('MP, Liverpool, Walton', $page);
175
    }
176
177
    /**
178
     * Test that glossary matches are displayed
179
     *
180
     * @group xapian
181
     */
182
    public function testSearchPageGlossary() {
183
        $page = $this->fetch_page([ 'q' => 'other place' ]);
184
        $this->assertStringContainsString('Glossary items matching', $page);
185
        $this->assertStringContainsString('<a href="/glossary/?gl=1">&ldquo;other place', $page);
186
    }
187
188
    /**
189
     * Test that spelling corrections are displayed
190
     *
191
     * @group xapian
192
     */
193
    public function testSearchPageSpellCorrect() {
194
        $page = $this->fetch_page([ 'q' => 'plice' ]);
195
        $this->assertStringContainsString('Did you mean <a href="/search/?q=place">place', $page);
196
    }
197
198
    /**
199
     * Test that grouping by speaker works
200
     *
201
     * @group xapian
202
     */
203
    public function testSearchBySpeakerNoResults() {
204
        $page = $this->fetch_page([ 'q' => 'splice', 'o' => 'p' ]);
205
        $this->assertStringContainsString('Who says splice the most', $page);
206
        $this->assertStringContainsString('No results', $page);
207
    }
208
209
    /**
210
     * Test that search highlighting with phrases skips words contained in link title attributes.
211
     *
212
     * @group xapian
213
     */
214
    public function testSearchPhraseHighlightingInTags() {
215
        $SEARCHENGINE = new SEARCHENGINE('"Shabana"');
216
217
        $expected_text = '<p pid="b.893.4/1">On a point of order, Mr <a href="/glossary/?gl=21" title="The Speaker is an MP who has been elected to act as Chairman during debates..." class="glossary">Speaker</a>. In yesterday’s Finance Bill debate, <a href="/mp/?m=40084" title="Our page on Shabana Mahmood - \'the hon. Member for Birmingham, Ladywood (Shabana Mahmood)\'"><span class="hi">Shabana</span> Mahmood</a> said that the tax gap was 32 billion when the previous Government left office and that it has now gone up to 35 billion. Official Her Majesty’s Revenue and Customs figures show the tax gap was actually 42 billion when Labour left office, so there has been a fall of 7 billion under this Government';
218
        $text = '<p pid="b.893.4/1">On a point of order, Mr <a href="/glossary/?gl=21" title="The Speaker is an MP who has been elected to act as Chairman during debates..." class="glossary">Speaker</a>. In yesterday&#8217;s Finance Bill debate, <a href="/mp/?m=40084" title="Our page on Shabana Mahmood - \'the hon. Member for Birmingham, Ladywood (Shabana Mahmood)\'">Shabana Mahmood</a> said that the tax gap was 32 billion when the previous Government left office and that it has now gone up to 35 billion. Official Her Majesty&#8217;s Revenue and Customs figures show the tax gap was actually 42 billion when Labour left office, so there has been a fall of 7 billion under this Government';
219
        $this->assertEquals(
220
            $expected_text,
221
            $SEARCHENGINE->highlight($text)
222
        );
223
    }
224
225
    /**
226
     * Test that search RSS links are displayed
227
     *
228
     * @group xapian
229
     */
230
    public function testSearchPageRSS() {
231
        $page = $this->fetch_page([ 'q' => 'test' ]);
232
        $this->assertStringContainsString('<a href="/search/rss/?s=test">get an RSS feed', $page);
233
    }
234
235
}
236