Issues (186)

tests/SearchVariantSubsitesTest.php (3 issues)

Labels
Severity
1
<?php
2
3
namespace SilverStripe\FullTextSearch\Tests;
4
5
use SilverStripe\Core\Config\Config;
6
use SilverStripe\Core\Injector\Injector;
7
use SilverStripe\Dev\SapphireTest;
8
use SilverStripe\FullTextSearch\Search\FullTextSearch;
9
use SilverStripe\FullTextSearch\Search\Processors\SearchUpdateProcessor;
10
use SilverStripe\FullTextSearch\Search\Queries\SearchQuery;
11
use SilverStripe\FullTextSearch\Search\Updaters\SearchUpdater;
12
use SilverStripe\FullTextSearch\Search\Variants\SearchVariantSubsites;
13
use SilverStripe\FullTextSearch\Tests\SearchUpdaterTest\SearchUpdaterTest_Container;
14
use SilverStripe\FullTextSearch\Tests\SolrIndexTest\SolrIndexTest_FakeIndex;
15
use SilverStripe\Subsites\Model\Subsite;
0 ignored issues
show
The type SilverStripe\Subsites\Model\Subsite was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
17
class SearchVariantSubsiteTest extends SapphireTest
18
{
19
    private static $index = null;
20
21
    protected function setUp()
22
    {
23
        parent::setUp();
24
25
        // Check versioned available
26
        if (!class_exists(Subsite::class)) {
27
            return $this->markTestSkipped('The subsites module is not installed');
28
        }
29
30
        if (self::$index === null) {
31
            self::$index = singleton(static::class);
32
        }
33
34
        Config::inst()->update(Injector::class, SearchUpdateProcessor::class, [
0 ignored issues
show
The method update() does not exist on SilverStripe\Config\Coll...nfigCollectionInterface. It seems like you code against a sub-type of SilverStripe\Config\Coll...nfigCollectionInterface such as SilverStripe\Config\Coll...\MemoryConfigCollection. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

34
        Config::inst()->/** @scrutinizer ignore-call */ update(Injector::class, SearchUpdateProcessor::class, [
Loading history...
35
            'class' => SearchUpdateImmediateProcessor::class
0 ignored issues
show
The type SilverStripe\FullTextSea...pdateImmediateProcessor was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
36
        ]);
37
38
        FullTextSearch::force_index_list(self::$index);
39
        SearchUpdater::clear_dirty_indexes();
40
    }
41
42
    public function testQueryIsAlteredWhenSubsiteNotSet()
43
    {
44
        $index = new SolrIndexTest_FakeIndex();
45
        $query = new SearchQuery();
46
47
        //typical behaviour: nobody is explicitly filtering on subsite, so the search variant adds a filter to the query
48
        $this->assertArrayNotHasKey('_subsite', $query->require);
49
        $variant = new SearchVariantSubsites();
50
        $variant->alterDefinition(SearchUpdaterTest_Container::class, $index);
51
        $variant->alterQuery($query, $index);
52
53
        //check that the "default" query has been put in place: it's not empty, and we're searching on Subsite ID:0 and
54
        // an object of SearchQuery::missing
55
        $this->assertNotEmpty($query->require['_subsite']);
56
        $this->assertEmpty($query->require['_subsite'][0]);
57
58
        //check that SearchQuery::missing is set (by default, it is an object of stdClass)
59
        $this->assertInstanceOf('stdClass', $query->require['_subsite'][1]);
60
    }
61
62
63
    public function testQueryIsAlteredWhenSubsiteIsSet()
64
    {
65
        //now we want to test if somebody has already applied the _subsite filter to the query
66
        $index = new SolrIndexTest_FakeIndex();
67
        $query = new SearchQuery();
68
69
        //check that _subsite is not applied yet
70
        //this key should not be exist until the SearchVariant applies it later
71
        $this->assertArrayNotHasKey('_subsite', $query->require);
72
73
        //apply the subsite filter on the query (for example, if it's passed into a controller and set before searching)
74
        //we've chosen an arbirary value of 2 here, to check if it is changed later
75
        $query->addFilter('_subsite', 2);
76
        $this->assertNotEmpty($query->require['_subsite']);
77
78
        //apply the search variant's definition and query
79
        $variant = new SearchVariantSubsites();
80
        $variant->alterDefinition(SearchUpdaterTest_Container::class, $index);
81
82
        //the protected function isFieldFiltered is implicitly tested here
83
        $variant->alterQuery($query, $index);
84
85
        //confirm that the query has been altered, but NOT with default values
86
        //first check that _subsite filter is not empty
87
        $this->assertNotEmpty($query->require['_subsite']);
88
        //subsite filter first value is not 0
89
        $this->assertNotEquals(0, $query->require['_subsite'][0]);
90
91
        //subsite filter SearchQuery::missing should not be set so its expected location is empty
92
        $this->assertArrayNotHasKey(1, $query->require['_subsite']);
93
94
        //subsite filter has been modified with our arbitrary test value. The second value is not set
95
        //this proves that the query has not been altered by the variant
96
        $this->assertEquals(2, $query->require['_subsite'][0]);
97
    }
98
}
99