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