Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
7 | class ElasticSearchPageControllerTest extends ElasticsearchFunctionalTestBase { |
||
8 | |||
9 | public static $fixture_file = 'elastica/tests/lotsOfPhotos.yml'; |
||
10 | |||
11 | |||
12 | public function setUp() { |
||
13 | parent::setUp(); |
||
14 | $esp = new ElasticSearchPage(); |
||
15 | $esp->Title = 'Search with aggregation'; |
||
16 | $esp->Content = 'Example search page with aggregation'; |
||
17 | $esp->Identifier = 'testwithagg'; |
||
18 | $esp->IndexingOff = true; |
||
19 | $esp->URLSegment = 'search'; |
||
20 | $esp->SiteTreeOnly = false; |
||
21 | $esp->ClassesToSearch = 'FlickrPhotoTO'; |
||
22 | $esp->SearchHelper = 'FlickrPhotoTOElasticaSearchHelper'; |
||
23 | $esp->write(); |
||
24 | |||
25 | $esp2 = new ElasticSearchPage(); |
||
26 | $esp2->Title = 'Search without aggregation'; |
||
27 | $esp2->Content = 'Example search page'; |
||
28 | $esp2->Identifier = 'testwithoutagg'; |
||
29 | $esp2->IndexingOff = true; |
||
30 | $esp2->URLSegment = 'search'; |
||
31 | $esp2->SiteTreeOnly = false; |
||
32 | $esp2->ClassesToSearch = 'FlickrPhotoTO'; |
||
33 | $esp2->ContentForEmptySearch = 'Content for empty search'; |
||
34 | $esp2->write(); |
||
35 | |||
36 | #NOTE: how to edit extra extra fields programatically |
||
37 | $extraFields = array('Searchable' => 1, 'SimilarSearchable' => 1, 'Active' => 1, |
||
38 | 'Weight' => 1); |
||
39 | $esfs2 = $esp2->ElasticaSearchableFields(); |
||
40 | foreach($esfs2 as $sf) { |
||
41 | if($sf->Name == 'Title' || $sf->Name == 'Description') { |
||
42 | $esfs2->remove($sf); |
||
43 | $esfs2->add($sf, $extraFields); |
||
44 | } |
||
45 | } |
||
46 | $esp2->write(); |
||
47 | |||
48 | $esfs = $esp->ElasticaSearchableFields(); |
||
49 | |||
50 | foreach($esfs as $sf) { |
||
51 | if($sf->Name == 'Title' || $sf->Name == 'Description') { |
||
52 | $esfs->remove($sf); |
||
53 | $esfs->add($sf, $extraFields); |
||
54 | } |
||
55 | } |
||
56 | $esp->write(); |
||
57 | $esp->publish('Stage', 'Live'); |
||
58 | $esp2->publish('Stage', 'Live'); |
||
59 | $this->ElasticSearchPage = $esp; |
||
60 | $this->ElasticSearchPage2 = $esp2; |
||
61 | } |
||
62 | |||
63 | |||
64 | public function testAggregationNoneSelected() { |
||
65 | //SearchHelper |
||
66 | |||
67 | $this->autoFollowRedirection = false; |
||
68 | |||
69 | //Note pages need to be published, by default fixtures only reside in Stage |
||
70 | $searchPageObj = $this->ElasticSearchPage; |
||
71 | |||
72 | $url = $searchPageObj->Link(); |
||
73 | $searchPage = $this->get($searchPageObj->URLSegment); |
||
74 | $this->assertEquals(200, $searchPage->getStatusCode()); |
||
75 | $url = rtrim($url, '/'); |
||
76 | |||
77 | $response = $this->get($url); |
||
78 | |||
79 | $this->assertEquals(200, $response->getStatusCode()); |
||
80 | |||
81 | $this->assertSelectorStartsWithOrEquals('ul.iso span.count', 0, '(5)'); |
||
82 | $this->assertSelectorStartsWithOrEquals('ul.iso span.count', 1, '(11)'); |
||
83 | $this->assertSelectorStartsWithOrEquals('ul.iso span.count', 2, '(12)'); |
||
84 | $this->assertSelectorStartsWithOrEquals('ul.iso span.count', 3, '(13)'); |
||
85 | $this->assertSelectorStartsWithOrEquals('ul.iso span.count', 4, '(16)'); |
||
86 | $this->assertSelectorStartsWithOrEquals('ul.iso span.count', 5, '(13)'); |
||
87 | $this->assertSelectorStartsWithOrEquals('ul.iso span.count', 6, '(11)'); |
||
88 | $this->assertSelectorStartsWithOrEquals('ul.iso span.count', 7, '(19)'); |
||
89 | $this->assertSelectorStartsWithOrEquals('ul.focal_length span.count', 0, '(12)'); |
||
90 | $this->assertSelectorStartsWithOrEquals('ul.focal_length span.count', 1, '(11)'); |
||
91 | $this->assertSelectorStartsWithOrEquals('ul.focal_length span.count', 2, '(11)'); |
||
92 | $this->assertSelectorStartsWithOrEquals('ul.focal_length span.count', 3, '(20)'); |
||
93 | $this->assertSelectorStartsWithOrEquals('ul.focal_length span.count', 4, '(12)'); |
||
94 | $this->assertSelectorStartsWithOrEquals('ul.focal_length span.count', 5, '(17)'); |
||
95 | $this->assertSelectorStartsWithOrEquals('ul.focal_length span.count', 6, '(17)'); |
||
96 | $this->assertSelectorStartsWithOrEquals('ul.shutter_speed span.count', 0, '(17)'); |
||
97 | $this->assertSelectorStartsWithOrEquals('ul.shutter_speed span.count', 1, '(15)'); |
||
98 | $this->assertSelectorStartsWithOrEquals('ul.shutter_speed span.count', 2, '(17)'); |
||
99 | $this->assertSelectorStartsWithOrEquals('ul.shutter_speed span.count', 3, '(10)'); |
||
100 | $this->assertSelectorStartsWithOrEquals('ul.shutter_speed span.count', 4, '(18)'); |
||
101 | $this->assertSelectorStartsWithOrEquals('ul.shutter_speed span.count', 5, '(1)'); |
||
102 | $this->assertSelectorStartsWithOrEquals('ul.shutter_speed span.count', 6, '(10)'); |
||
103 | $this->assertSelectorStartsWithOrEquals('ul.shutter_speed span.count', 7, '(12)'); |
||
104 | $this->assertSelectorStartsWithOrEquals('ul.aperture span.count', 0, '(21)'); |
||
105 | $this->assertSelectorStartsWithOrEquals('ul.aperture span.count', 1, '(23)'); |
||
106 | $this->assertSelectorStartsWithOrEquals('ul.aperture span.count', 2, '(17)'); |
||
107 | $this->assertSelectorStartsWithOrEquals('ul.aperture span.count', 3, '(16)'); |
||
108 | $this->assertSelectorStartsWithOrEquals('ul.aperture span.count', 4, '(23)'); |
||
109 | $this->assertSelectorStartsWithOrEquals('ul.aspect span.count', 0, '(9)'); |
||
110 | $this->assertSelectorStartsWithOrEquals('ul.aspect span.count', 1, '(31)'); |
||
111 | $this->assertSelectorStartsWithOrEquals('ul.aspect span.count', 2, '(16)'); |
||
112 | $this->assertSelectorStartsWithOrEquals('ul.aspect span.count', 3, '(39)'); |
||
113 | $this->assertSelectorStartsWithOrEquals('ul.aspect span.count', 4, '(5)'); |
||
114 | } |
||
115 | |||
116 | |||
117 | public function testAggregationOneSelected() { |
||
118 | //SearchHelper |
||
119 | |||
120 | $this->autoFollowRedirection = false; |
||
121 | |||
122 | //Note pages need to be published, by default fixtures only reside in Stage |
||
123 | $searchPageObj = $this->ElasticSearchPage; |
||
124 | |||
125 | $url = $searchPageObj->Link(); |
||
126 | $searchPage = $this->get($searchPageObj->URLSegment); |
||
127 | $this->assertEquals(200, $searchPage->getStatusCode()); |
||
128 | $url = rtrim($url, '/'); |
||
129 | $url .= '?ISO=400'; |
||
130 | |||
131 | $response = $this->get($url); |
||
132 | |||
133 | $this->assertEquals(200, $response->getStatusCode()); |
||
134 | |||
135 | // These are less than in the no facets selected case, as expected |
||
136 | $this->assertSelectorStartsWithOrEquals('ul.focal_length span.count', 0, '(2)'); |
||
137 | $this->assertSelectorStartsWithOrEquals('ul.focal_length span.count', 1, '(1)'); |
||
138 | $this->assertSelectorStartsWithOrEquals('ul.focal_length span.count', 2, '(2)'); |
||
139 | $this->assertSelectorStartsWithOrEquals('ul.focal_length span.count', 3, '(1)'); |
||
140 | $this->assertSelectorStartsWithOrEquals('ul.focal_length span.count', 4, '(3)'); |
||
141 | $this->assertSelectorStartsWithOrEquals('ul.focal_length span.count', 5, '(1)'); |
||
142 | $this->assertSelectorStartsWithOrEquals('ul.focal_length span.count', 6, '(3)'); |
||
143 | $this->assertSelectorStartsWithOrEquals('ul.shutter_speed span.count', 0, '(3)'); |
||
144 | $this->assertSelectorStartsWithOrEquals('ul.shutter_speed span.count', 1, '(2)'); |
||
145 | $this->assertSelectorStartsWithOrEquals('ul.shutter_speed span.count', 2, '(2)'); |
||
146 | $this->assertSelectorStartsWithOrEquals('ul.shutter_speed span.count', 3, '(1)'); |
||
147 | $this->assertSelectorStartsWithOrEquals('ul.shutter_speed span.count', 4, '(2)'); |
||
148 | $this->assertSelectorStartsWithOrEquals('ul.shutter_speed span.count', 5, '(3)'); |
||
149 | $this->assertSelectorStartsWithOrEquals('ul.aperture span.count', 0, '(3)'); |
||
150 | $this->assertSelectorStartsWithOrEquals('ul.aperture span.count', 1, '(4)'); |
||
151 | $this->assertSelectorStartsWithOrEquals('ul.aperture span.count', 2, '(3)'); |
||
152 | $this->assertSelectorStartsWithOrEquals('ul.aperture span.count', 3, '(1)'); |
||
153 | $this->assertSelectorStartsWithOrEquals('ul.aperture span.count', 4, '(2)'); |
||
154 | $this->assertSelectorStartsWithOrEquals('ul.aspect span.count', 0, '(0)'); |
||
155 | $this->assertSelectorStartsWithOrEquals('ul.aspect span.count', 1, '(4)'); |
||
156 | $this->assertSelectorStartsWithOrEquals('ul.aspect span.count', 2, '(1)'); |
||
157 | $this->assertSelectorStartsWithOrEquals('ul.aspect span.count', 3, '(7)'); |
||
158 | $this->assertSelectorStartsWithOrEquals('ul.aspect span.count', 4, '(1)'); |
||
159 | |||
160 | |||
161 | } |
||
162 | |||
163 | |||
164 | |||
165 | public function testAggregationTwoSelected() { |
||
166 | //SearchHelper |
||
167 | |||
168 | $this->autoFollowRedirection = false; |
||
169 | |||
170 | //Note pages need to be published, by default fixtures only reside in Stage |
||
171 | $searchPageObj = $this->ElasticSearchPage; |
||
172 | |||
173 | $url = $searchPageObj->Link(); |
||
174 | $searchPage = $this->get($searchPageObj->URLSegment); |
||
175 | $this->assertEquals(200, $searchPage->getStatusCode()); |
||
176 | $url = rtrim($url, '/'); |
||
177 | $url .= '?ISO=400&ShutterSpeed=2%2F250'; |
||
178 | |||
179 | $response = $this->get($url); |
||
180 | $this->assertEquals(200, $response->getStatusCode()); |
||
181 | |||
182 | // These are less than in the one facet selected case, as expected |
||
183 | $this->assertSelectorStartsWithOrEquals('ul.focal_length span.count', 0, '(1)'); |
||
184 | $this->assertSelectorStartsWithOrEquals('ul.focal_length span.count', 1, '(2)'); |
||
185 | $this->assertSelectorStartsWithOrEquals('ul.aperture span.count', 0, '(1)'); |
||
186 | $this->assertSelectorStartsWithOrEquals('ul.aperture span.count', 1, '(1)'); |
||
187 | $this->assertSelectorStartsWithOrEquals('ul.aperture span.count', 2, '(1)'); |
||
188 | $this->assertSelectorStartsWithOrEquals('ul.aspect span.count', 0, '(0)'); |
||
189 | $this->assertSelectorStartsWithOrEquals('ul.aspect span.count', 1, '(1)'); |
||
190 | $this->assertSelectorStartsWithOrEquals('ul.aspect span.count', 2, '(0)'); |
||
191 | $this->assertSelectorStartsWithOrEquals('ul.aspect span.count', 3, '(2)'); |
||
192 | $this->assertSelectorStartsWithOrEquals('ul.aspect span.count', 4, '(0)'); |
||
193 | } |
||
194 | |||
195 | |||
196 | public function testAggregationThreeSelected() { |
||
218 | |||
219 | |||
220 | public function testQueryIsEmpty() { |
||
242 | |||
243 | View Code Duplication | public function testQueryInSearchBoxForOneFormOnly() { |
|
1 ignored issue
–
show
|
|||
244 | $searchPageObj = $this->ElasticSearchPage2; |
||
245 | $url = rtrim($searchPageObj->Link(), '/'); |
||
246 | $url .= "?q=Auckland&sfid=".$searchPageObj->Identifier; |
||
252 | |||
253 | |||
254 | /* |
||
255 | Check the search form attributes for autocomplete info |
||
256 | */ |
||
257 | public function testSearchFormAutocompleteEnabled() { |
||
287 | |||
288 | |||
289 | public function testTemplateOverrideExtension() { |
||
316 | |||
317 | |||
318 | View Code Duplication | public function testSimilarNotSearchable() { |
|
328 | |||
329 | |||
330 | View Code Duplication | public function testSimilarNull() { |
|
340 | |||
341 | |||
342 | View Code Duplication | public function testSimilarClassDoesNotExist() { |
|
351 | |||
352 | |||
353 | View Code Duplication | public function testSimilarSearchServerDown() { |
|
361 | |||
362 | |||
363 | View Code Duplication | public function testNormalSearchServerDown() { |
|
371 | |||
372 | |||
373 | public function testSimilarValid() { |
||
389 | |||
390 | |||
391 | /* |
||
392 | Search for New Zealind and get search results for New Zealand. Show option to click on |
||
393 | actual search of New Zealind |
||
394 | */ |
||
395 | public function testSuggestion() { |
||
437 | /* |
||
438 | <p class="showingResultsForMsg">Showing results for <a href="./?q=New Zealand">New <strong class="hl">Zealand</strong></a></p> |
||
439 | <p class="searchOriginalQueryMsg">Search instead for <a href="/search-2?q=New Zealind&is=1">New Zealind</a></p> |
||
440 | |||
441 | |||
442 | */ |
||
443 | |||
444 | |||
445 | View Code Duplication | public function testSearchOnePageNoAggregation() { |
|
481 | |||
482 | |||
483 | /* When a search is posted, should redirect to the same URL with the search term attached. This |
||
484 | means that searches can be bookmarked if so required */ |
||
485 | public function testRedirection() { |
||
502 | |||
503 | |||
504 | /* |
||
505 | Add test for redirection of /search/?q=XXX to /search?q=XXX |
||
506 | */ |
||
507 | |||
508 | |||
509 | /* |
||
510 | Test a search for an uncommon term, no pagination here |
||
511 | */ |
||
512 | View Code Duplication | public function testSearchOnePage() { |
|
545 | |||
546 | |||
547 | |||
548 | /* |
||
549 | Test a search for a common term, in order to induce pagination |
||
550 | */ |
||
551 | public function testSiteTreeSearch() { |
||
586 | |||
587 | |||
588 | |||
589 | /* |
||
590 | Test a search for a common term, in order to induce pagination |
||
591 | */ |
||
592 | public function testSearchSeveralPagesPage() { |
||
654 | |||
655 | |||
656 | private function enableHighlights() { |
||
670 | } |
||
671 |
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.