Completed
Pull Request — master (#264)
by Robbie
13:29
created

TranslatableSearchFormTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 9

Importance

Changes 0
Metric Value
wmc 5
lcom 2
cbo 9
dl 0
loc 105
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A waitUntilIndexingFinished() 0 7 2
A setUpBeforeClass() 0 11 1
A setUp() 0 13 1
A testPublishedPagesMatchedByTitleInDefaultLanguage() 0 51 1
1
<?php
2
3
namespace SilverStripe\Translatable\Tests;
4
5
use SilverStripe\CMS\Controllers\ContentController;
6
use SilverStripe\CMS\Model\SiteTree;
7
use SilverStripe\CMS\Search\ContentControllerSearchExtension;
8
use SilverStripe\CMS\Search\SearchForm;
9
use SilverStripe\Dev\FunctionalTest;
10
use SilverStripe\ORM\DB;
11
use SilverStripe\ORM\Search\FulltextSearchable;
12
use SilverStripe\Security\Member;
13
use SilverStripe\Translatable\Model\Translatable;
14
use SilverStripe\Versioned\Versioned;
15
use SilverStripe\Control\HTTPRequest;
16
17
/**
18
 * @package translatable
19
 */
20
class TranslatableSearchFormTest extends FunctionalTest
21
{
22
    protected static $fixture_file = 'TranslatableSearchFormTest.yml';
23
24
    protected $mockController;
25
26
    protected static $required_extensions = [
27
        SiteTree::class => [
28
            Translatable::class,
29
            FulltextSearchable::class . "('Title,MenuTitle,Content,MetaDescription')",
30
        ],
31
        ContentController::class => [
32
            ContentControllerSearchExtension::class,
33
        ],
34
    ];
35
36
    /**
37
     * @see {@link ZZZSearchFormTest}
38
     */
39
    protected function waitUntilIndexingFinished()
40
    {
41
        $schema = DB::get_schema();
42
        if (method_exists($schema, 'waitUntilIndexingFinished')) {
43
            $schema->waitUntilIndexingFinished();
0 ignored issues
show
Bug introduced by
The method waitUntilIndexingFinished() does not seem to exist on object<SilverStripe\ORM\Connect\DBSchemaManager>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
44
        }
45
    }
46
47
    public static function setUpBeforeClass()
48
    {
49
        static::start();
50
        // HACK Postgres doesn't refresh TSearch indexes when the schema changes after CREATE TABLE
51
        // MySQL will need a different table type
52
        static::$tempDB->kill();
53
        FulltextSearchable::enable();
54
        static::$tempDB->build();
55
        static::resetDBSchema(true, true);
56
        parent::setUpBeforeClass();
57
    }
58
59
    protected function setUp()
60
    {
61
        parent::setUp();
62
63
        $holderPage = $this->objFromFixture(SiteTree::class, 'searchformholder');
64
        $this->mockController = new ContentController($holderPage);
0 ignored issues
show
Bug introduced by
It seems like $holderPage defined by $this->objFromFixture(\S...ss, 'searchformholder') on line 63 can also be of type object<SilverStripe\ORM\DataObject>; however, SilverStripe\CMS\Control...ntroller::__construct() does only seem to accept object<SilverStripe\CMS\Model\SiteTree>|null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
65
66
        // whenever a translation is created, canTranslate() is checked
67
        $admin = $this->objFromFixture(Member::class, 'admin');
68
        $admin->logIn();
69
70
        $this->waitUntilIndexingFinished();
71
    }
72
73
    public function testPublishedPagesMatchedByTitleInDefaultLanguage()
74
    {
75
        $publishedPage = $this->objFromFixture(SiteTree::class, 'publishedPage');
76
        $publishedPage->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
77
78
        $translatedPublishedPage = $publishedPage->createTranslation('de_DE');
79
        $translatedPublishedPage->Title = 'translatedPublishedPage';
80
        $translatedPublishedPage->Content = 'German content';
81
        $translatedPublishedPage->write();
82
        $publishedPage->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
83
84
        $this->waitUntilIndexingFinished();
85
86
        $lang = 'en_US';
87
        $request = new HTTPRequest('GET', 'search', ['Search'=>'content', 'searchlocale' => $lang]);
88
        $request->setSession($this->session());
89
        $this->mockController->setRequest($request);
90
        $sf = new SearchForm($this->mockController);
91
        $results = $sf->getResults();
92
93
        $this->assertContains(
94
            $publishedPage->ID,
95
            $results->column('ID'),
96
            'Published pages are found by searchform in default language'
97
        );
98
        $this->assertNotContains(
99
            $translatedPublishedPage->ID,
100
            $results->column('ID'),
101
            'Published pages in another language are not found when searching in default language'
102
        );
103
104
        $lang = 'de_DE';
105
        $request = new HTTPRequest('GET', 'search', ['Search'=>'content', 'searchlocale' => $lang]);
106
        $request->setSession($this->session());
107
        $this->mockController->setRequest($request);
108
        $sf2 = new SearchForm($this->mockController);
109
        $results = $sf2->getResults();
110
111
        $this->assertNotContains(
112
            $publishedPage->ID,
113
            $results->column('ID'),
114
            'Published pages in default language are not found when searching in another language'
115
        );
116
        $actual = $results->column('ID');
117
        array_walk($actual, 'intval');
118
        $this->assertContains(
119
            (int)$translatedPublishedPage->ID,
120
            $actual,
121
            'Published pages in another language are found when searching in this language'
122
        );
123
    }
124
}
125