CMSMainSearchFormTest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 35
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A testTitleFilter() 0 19 1
A getPageTitles() 0 10 3
1
<?php
2
3
namespace SilverStripe\CMS\Tests\Search;
4
5
use SilverStripe\CMS\Controllers\CMSSiteTreeFilter_Search;
6
use SilverStripe\Dev\FunctionalTest;
7
use SilverStripe\Security\Member;
8
9
class CMSMainSearchFormTest extends FunctionalTest
10
{
11
    protected static $fixture_file = '../Controllers/CMSMainTest.yml';
12
13
    public function testTitleFilter()
14
    {
15
        $this->session()->set('loggedInAs', $this->idFromFixture(Member::class, 'admin'));
16
17
        $this->get(
18
            'admin/pages/?' .
19
            http_build_query([
20
                'q' => [
21
                    'Term' => 'Page 10',
22
                    'FilterClass' => CMSSiteTreeFilter_Search::class,
23
                ]
24
            ])
25
        );
26
27
        $titles = $this->getPageTitles();
28
        $this->assertEquals(count($titles), 1);
29
        // For some reason the title gets split into two lines
30
31
        $this->assertStringContainsString('Page 1', $titles[0]);
32
    }
33
34
    protected function getPageTitles()
35
    {
36
        $titles = [];
37
        $links = $this->cssParser()->getBySelector('.col-getTreeTitle span.item');
38
        if ($links) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $links of type SimpleXMLElement[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
39
            foreach ($links as $link) {
40
                $titles[] = preg_replace('/\n/', ' ', $link->asXML());
41
            }
42
        }
43
        return $titles;
44
    }
45
}
46