Completed
Push — master ( 44f7bc...3e4910 )
by
unknown
20s queued 14s
created

testCanBeDisabledViaConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
3
namespace SilverStripe\FullTextSearch\Tests;
4
5
use SilverStripe\Core\Config\Config;
6
use SilverStripe\CMS\Model\SiteTree;
7
use SilverStripe\Dev\SapphireTest;
8
use SilverStripe\FullTextSearch\Search\FullTextSearch;
9
use SilverStripe\FullTextSearch\Search\Indexes\SearchIndex_Recording;
10
use SilverStripe\FullTextSearch\Search\Variants\SearchVariantVersioned;
11
use SilverStripe\FullTextSearch\Tests\SearchVariantVersionedTest\SearchVariantVersionedTest_Index;
12
use SilverStripe\FullTextSearch\Tests\SearchVariantVersionedTest\SearchVariantVersionedTest_Item;
13
use SilverStripe\FullTextSearch\Tests\SearchVariantVersionedTest\SearchVariantVersionedTest_IndexNoStage;
14
use SilverStripe\FullTextSearch\Search\Processors\SearchUpdateProcessor;
15
use SilverStripe\FullTextSearch\Search\Processors\SearchUpdateImmediateProcessor;
16
use SilverStripe\FullTextSearch\Search\Updaters\SearchUpdater;
17
18
class SearchVariantVersionedTest extends SapphireTest
19
{
20
    /**
21
     * @var SearchVariantVersionedTest_Index
22
     */
23
    private static $index = null;
24
25
    protected static $extra_dataobjects = array(
26
        SearchVariantVersionedTest_Item::class
27
    );
28
29
    protected function setUp()
30
    {
31
        parent::setUp();
32
33
        if (self::$index === null) {
34
            self::$index = singleton(SearchVariantVersionedTest_Index::class);
35
        }
36
37
        SearchUpdater::bind_manipulation_capture();
38
39
        Config::modify()->set(Injector::class, SearchUpdateProcessor::class, array(
40
            'class' => SearchUpdateImmediateProcessor::class
41
        ));
42
43
        FullTextSearch::force_index_list(self::$index);
44
        SearchUpdater::clear_dirty_indexes();
45
    }
46
47
    public function testPublishing()
48
    {
49
        // Check that write updates Stage
50
51
        $item = new SearchVariantVersionedTest_Item(array('TestText' => 'Foo'));
52
        $item->write();
53
54
        SearchUpdater::flush_dirty_indexes();
55
        $this->assertEquals(array(
56
            array('ID' => $item->ID, '_versionedstage' => 'Stage')
57
        ), self::$index->getAdded(array('ID', '_versionedstage')));
58
59
        // Check that publish updates Live
60
61
        self::$index->reset();
62
63
        $item->copyVersionToStage('Stage', 'Live');
0 ignored issues
show
Documentation Bug introduced by
The method copyVersionToStage does not exist on object<SilverStripe\Full...iantVersionedTest_Item>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
64
65
        SearchUpdater::flush_dirty_indexes();
66
        $this->assertEquals(array(
67
            array('ID' => $item->ID, '_versionedstage' => 'Stage'),
68
            array('ID' => $item->ID, '_versionedstage' => 'Live')
69
        ), self::$index->getAdded(array('ID', '_versionedstage')));
70
71
        // Just update a SiteTree field, and check it updates Stage
72
73
        self::$index->reset();
74
75
        $item->Title = "Pow!";
76
        $item->write();
77
78
        SearchUpdater::flush_dirty_indexes();
79
80
        $expected = array(array(
81
            'ID' => $item->ID,
82
            '_versionedstage' => 'Stage'
83
        ));
84
        $added = self::$index->getAdded(array('ID', '_versionedstage'));
85
        $this->assertEquals($expected, $added);
86
87
        // Test unpublish
88
89
        self::$index->reset();
90
91
        $item->deleteFromStage('Live');
0 ignored issues
show
Documentation Bug introduced by
The method deleteFromStage does not exist on object<SilverStripe\Full...iantVersionedTest_Item>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
92
93
        SearchUpdater::flush_dirty_indexes();
94
95
        $this->assertCount(1, self::$index->deleted);
96
        $this->assertEquals(
97
            SiteTree::class,
98
            self::$index->deleted[0]['base']
99
        );
100
        $this->assertEquals(
101
            $item->ID,
102
            self::$index->deleted[0]['id']
103
        );
104
        $this->assertEquals(
105
            'Live',
106
            self::$index->deleted[0]['state'][SearchVariantVersioned::class]
107
        );
108
    }
109
110
    public function testExcludeVariantState()
111
    {
112
        $index = singleton(SearchVariantVersionedTest_IndexNoStage::class);
113
        FullTextSearch::force_index_list($index);
114
115
        // Check that write doesn't update stage
116
        $item = new SearchVariantVersionedTest_Item(array('TestText' => 'Foo'));
117
        $item->write();
118
        SearchUpdater::flush_dirty_indexes();
119
        $this->assertEquals(array(), $index->getAdded(array('ID', '_versionedstage')));
120
121
        // Check that publish updates Live
122
        $index->reset();
123
124
        $item->copyVersionToStage('Stage', 'Live');
0 ignored issues
show
Documentation Bug introduced by
The method copyVersionToStage does not exist on object<SilverStripe\Full...iantVersionedTest_Item>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
125
126
        SearchUpdater::flush_dirty_indexes();
127
        $this->assertEquals(array(
128
            array('ID' => $item->ID, '_versionedstage' => 'Live')
129
        ), $index->getAdded(array('ID', '_versionedstage')));
130
    }
131
132
    public function testCanBeDisabledViaConfig()
133
    {
134
        $variant = new SearchVariantVersioned;
135
136
        Config::modify()->set(SearchVariantVersioned::class, 'enabled', true);
137
        $this->assertTrue($variant->appliesToEnvironment());
138
139
        Config::modify()->set(SearchVariantVersioned::class, 'enabled', false);
140
        $this->assertFalse($variant->appliesToEnvironment());
141
    }
142
}
143