Passed
Pull Request — master (#77)
by Marco
23:46 queued 13:46
created

DocumentFactoryTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 6
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
4
namespace Firesphere\SolrSearch\Tests;
5
6
use Firesphere\SolrSearch\Compat\SubsitesExtension;
7
use Firesphere\SolrSearch\Extensions\DataObjectExtension;
8
use Firesphere\SolrSearch\Factories\DocumentFactory;
9
use Firesphere\SolrSearch\Helpers\SearchIntrospection;
10
use Firesphere\SolrSearch\Indexes\BaseIndex;
11
use Page;
12
use Psr\Log\LoggerInterface;
13
use SilverStripe\CMS\Model\SiteTree;
14
use SilverStripe\Dev\Debug;
15
use SilverStripe\Dev\SapphireTest;
16
use SilverStripe\ORM\DataObject;
17
use SilverStripe\SiteConfig\SiteConfig;
18
use SilverStripe\View\ViewableData;
19
use Solarium\Core\Client\Client;
20
use Solarium\QueryType\Update\Query\Document\Document;
21
22
class DocumentFactoryTest extends SapphireTest
23
{
24
    protected static $fixture_file = '../fixtures/DataResolver.yml';
25
    protected static $extra_dataobjects = [
26
        TestObject::class,
27
        TestPage::class,
28
        TestRelationObject::class,
29
    ];
30
31
    protected static $required_extensions = [
32
        DataObject::class => [DataObjectExtension::class],
33
        BaseIndex::class => [SubsitesExtension::class],
34
    ];
35
36
    public function setUp()
37
    {
38
        $siteConfig = SiteConfig::current_site_config();
39
        $siteConfig->CanViewType = 'Anyone';
40
        $siteConfig->write();
41
        return parent::setUp();
42
    }
43
44
    /**
45
     * We can't use the constant here for unknown reasons
46
     * If you change the constant, please replace id here with the appropriate value
47
     * @var array
48
     */
49
    protected static $expected_docs = [
50
        [
51
            'id'                => 'Page-1',
52
            'ObjectID'          => 1,
53
            'ClassName'         => 'Page',
54
            'ClassHierarchy'    =>
55
                [
56
                    'silverstripe\\view\\viewabledata'   => ViewableData::class,
57
                    'silverstripe\\orm\\dataobject'      => DataObject::class,
58
                    'silverstripe\\cms\\model\\sitetree' => SiteTree::class,
59
                    'page'                               => 'Page',
60
                ],
61
            'ViewStatus'        =>
62
                [
63
                    0 => '1-null',
64
                ],
65
            'SiteTree_Title'    => 'Home',
66
            'SiteTree_Content'  => "<p>Welcome to SilverStripe! This is the default homepage. You can edit this page by opening <a href=\"admin/\">the CMS</a>.</p><p>You can now access the <a href=\"http://docs.silverstripe.org\">developer documentation</a>, or begin the <a href=\"http://www.silverstripe.org/learn/lessons\">SilverStripe lessons</a>.</p>",
67
            'SiteTree_ParentID' => 0,
68
            'SiteTree_ViewStatus' => '1-null',
69
        ],
70
        [
71
            'id'                => 'Page-6',
72
            'ObjectID'          => 6,
73
            'ClassName'         => 'Page',
74
            'ClassHierarchy'    =>
75
                [
76
                    'silverstripe\\view\\viewabledata'   => ViewableData::class,
77
                    'silverstripe\\orm\\dataobject'      => DataObject::class,
78
                    'silverstripe\\cms\\model\\sitetree' => SiteTree::class,
79
                    'page'                               => 'Page',
80
                ],
81
            'ViewStatus'        =>
82
                [
83
                    0 => '1-null',
84
                ],
85
            'SiteTree_Title'    => 'Test 1',
86
            'SiteTree_ParentID' => 1,
87
            'SiteTree_ViewStatus' => '1-null',
88
        ],
89
        [
90
            'id'                => 'Page-2',
91
            'ObjectID'          => 2,
92
            'ClassName'         => 'Page',
93
            'ClassHierarchy'    =>
94
                [
95
                    'silverstripe\\view\\viewabledata'   => ViewableData::class,
96
                    'silverstripe\\orm\\dataobject'      => DataObject::class,
97
                    'silverstripe\\cms\\model\\sitetree' => SiteTree::class,
98
                    'page'                               => 'Page',
99
                ],
100
            'ViewStatus'        =>
101
                [
102
                    0 => '1-null',
103
                ],
104
            'SiteTree_Title'    => 'About Us',
105
            'SiteTree_Content'  => "<p>You can fill this page out with your own content, or delete it and create your own pages.</p>",
106
            'SiteTree_ParentID' => 0,
107
            'SiteTree_ViewStatus' => '1-null',
108
        ],
109
        [
110
            'id'                => 'Page-7',
111
            'ObjectID'          => 7,
112
            'ClassName'         => 'Page',
113
            'ClassHierarchy'    =>
114
                [
115
                    'silverstripe\\view\\viewabledata'   => ViewableData::class,
116
                    'silverstripe\\orm\\dataobject'      => DataObject::class,
117
                    'silverstripe\\cms\\model\\sitetree' => SiteTree::class,
118
                    'page'                               => 'Page',
119
                ],
120
            'ViewStatus'        =>
121
                [
122
                    0 => '1-null',
123
                ],
124
            'SiteTree_Title'    => 'Test 2',
125
            'SiteTree_ParentID' => 1,
126
            'SiteTree_ViewStatus' => '1-null',
127
        ],
128
        [
129
            'id'                => 'Page-3',
130
            'ObjectID'          => 3,
131
            'ClassName'         => 'Page',
132
            'ClassHierarchy'    =>
133
                [
134
                    'silverstripe\\view\\viewabledata'   => ViewableData::class,
135
                    'silverstripe\\orm\\dataobject'      => DataObject::class,
136
                    'silverstripe\\cms\\model\\sitetree' => SiteTree::class,
137
                    'page'                               => 'Page',
138
                ],
139
            'ViewStatus'        =>
140
                [
141
                    0 => '1-null',
142
                ],
143
            'SiteTree_Title'    => 'Contact Us',
144
            'SiteTree_Content'  => "<p>You can fill this page out with your own content, or delete it and create your own pages.</p>",
145
            'SiteTree_ParentID' => 0,
146
            'SiteTree_ViewStatus' => '1-null',
147
        ]
148
    ];
149
150
    public function testConstruct()
151
    {
152
        $factory = new DocumentFactory();
153
        $this->assertInstanceOf(SearchIntrospection::class, $factory->getIntrospection());
154
    }
155
156
    public function testBuildItems()
157
    {
158
        $factory = new DocumentFactory();
159
        $index = new TestIndex();
160
        $fields = $index->getFieldsForIndexing();
161
        $client = new Client([]);
162
        $update = $client->createUpdate();
163
        $factory->setClass(SiteTree::class);
164
        $factory->setItems(Page::get());
165
        $docs = $factory->buildItems($fields, $index, $update);
166
        $this->assertCount(SiteTree::get()->count(), $docs);
167
        $this->assertInternalType('array', $docs);
168
        $this->assertInstanceOf(BaseIndex::class, $factory->getIntrospection()->getIndex());
169
        /** @var Document $doc */
170
        foreach ($docs as $i => $doc) {
171
            $this->assertInstanceOf(Document::class, $doc);
172
            $fields = $doc->getFields();
173
            $this->assertArrayHasKey('SiteTree_Created', $fields);
174
            unset($fields['SiteTree_Created'], $fields['SiteTree_SubsiteID']); // Unset the Created, it changes per run
175
            $this->assertEquals(static::$expected_docs[$i], $fields);
176
        }
177
    }
178
179
    public function testSanitiseField()
180
    {
181
        $factory = new DocumentFactory();
182
183
        $this->assertEquals('hello', $factory->sanitiseName('Test\\Name\\hello'));
184
    }
185
186
    public function testGetLogger()
187
    {
188
        $this->assertInstanceOf(LoggerInterface::class, (new DocumentFactory())->getLogger());
189
    }
190
191
    public function testDebug()
192
    {
193
        $factory = new DocumentFactory();
194
        $this->assertFalse($factory->isDebug());
195
        $this->assertTrue($factory->setDebug(true)->isDebug());
196
    }
197
}
198