1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NerdsAndCompany\Schematic\Services; |
4
|
|
|
|
5
|
|
|
use Craft\Craft; |
6
|
|
|
use Craft\BaseTest; |
7
|
|
|
use Craft\CategoryElementType; |
8
|
|
|
use Craft\ElementIndexesService; |
9
|
|
|
use Craft\ElementsService; |
10
|
|
|
use Craft\EntryElementType; |
11
|
|
|
use Craft\FieldModel; |
12
|
|
|
use Craft\FieldsService; |
13
|
|
|
use NerdsAndCompany\Schematic\Models\Result; |
14
|
|
|
use PHPUnit_Framework_MockObject_MockObject as Mock; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class ElementIndexSettingsTest. |
18
|
|
|
* |
19
|
|
|
* @author Nerds & Company |
20
|
|
|
* @copyright Copyright (c) 2015-2017, Nerds & Company |
21
|
|
|
* @license MIT |
22
|
|
|
* |
23
|
|
|
* @link http://www.nerds.company |
24
|
|
|
* |
25
|
|
|
* @coversDefaultClass NerdsAndCompany\Schematic\Services\ElementIndexSettings |
26
|
|
|
* @covers ::__construct |
27
|
|
|
* @covers ::<!public> |
28
|
|
|
*/ |
29
|
|
|
class ElementIndexSettingsTest extends BaseTest |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var ElementIndexSettings |
33
|
|
|
*/ |
34
|
|
|
private $schematicElementIndexSettingsService; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritdoc} |
38
|
|
|
*/ |
39
|
|
|
public function setUp() |
40
|
|
|
{ |
41
|
|
|
$this->schematicElementIndexSettingsService = new ElementIndexSettings(); |
42
|
|
|
$this->setMockSources(); |
43
|
|
|
$this->setMockFieldsService(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return ElementsService|Mock |
48
|
|
|
* |
49
|
|
|
* @param array $getAllElementTypesResponse |
50
|
|
|
* |
51
|
|
|
* @return Mock |
52
|
|
|
*/ |
53
|
|
|
protected function getMockElementsService($getAllElementTypesResponse = []) |
54
|
|
|
{ |
55
|
|
|
$mock = $this->getMockBuilder(ElementsService::class)->getMock(); |
56
|
|
|
$mock->expects($this->any())->method('getAllElementTypes')->willReturn($getAllElementTypesResponse); |
57
|
|
|
|
58
|
|
|
return $mock; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return ElementIndexesService|Mock |
63
|
|
|
* @return Mock |
64
|
|
|
*/ |
65
|
|
|
protected function getMockElementIndexesService() |
66
|
|
|
{ |
67
|
|
|
$getSettingsResponse = $this->getElementIndexSettingsSavedData(); |
68
|
|
|
$mock = $this->getMockBuilder(ElementIndexesService::class)->getMock(); |
69
|
|
|
$mock->expects($this->any())->method('getSettings')->will($this->returnValueMap([ |
70
|
|
|
['Entry', $getSettingsResponse['Entry']], |
71
|
|
|
['Category', $getSettingsResponse['Category']], |
72
|
|
|
])); |
73
|
|
|
$mock->expects($this->any())->method('saveSettings')->willReturn(false); |
74
|
|
|
|
75
|
|
|
return $mock; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Test default import functionality. |
80
|
|
|
* |
81
|
|
|
* @covers ::import |
82
|
|
|
*/ |
83
|
|
|
public function testImport() |
84
|
|
|
{ |
85
|
|
|
$data = $this->getElementIndexSettingsExportedData(); |
86
|
|
|
$mockElementIndexesService = $this->getMockElementIndexesService(); |
87
|
|
|
$this->setComponent(Craft::app(), 'elementIndexes', $mockElementIndexesService); |
88
|
|
|
|
89
|
|
|
$import = $this->schematicElementIndexSettingsService->import($data); |
90
|
|
|
|
91
|
|
|
$this->assertTrue($import instanceof Result); |
92
|
|
|
$this->assertTrue($import->hasErrors()); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Test export functionality. |
97
|
|
|
* |
98
|
|
|
* @covers ::export |
99
|
|
|
*/ |
100
|
|
|
public function testExport() |
101
|
|
|
{ |
102
|
|
|
$data = $this->getElementsData(); |
103
|
|
|
$mockElementsService = $this->getMockElementsService($data); |
104
|
|
|
$this->setComponent(Craft::app(), 'elements', $mockElementsService); |
105
|
|
|
|
106
|
|
|
$data = $this->getElementIndexSettingsExportedData(); |
107
|
|
|
$mockElementIndexesService = $this->getMockElementIndexesService(); |
108
|
|
|
$this->setComponent(Craft::app(), 'elementIndexes', $mockElementIndexesService); |
109
|
|
|
|
110
|
|
|
$export = $this->schematicElementIndexSettingsService->export(); |
111
|
|
|
$this->assertEquals($data, $export); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Returns elements data. |
116
|
|
|
* |
117
|
|
|
* @return array |
118
|
|
|
*/ |
119
|
|
|
public function getElementsData() |
120
|
|
|
{ |
121
|
|
|
return [ |
122
|
|
|
new CategoryElementType(), |
123
|
|
|
new EntryElementType(), |
124
|
|
|
]; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Returns element index settings saved data. |
129
|
|
|
* |
130
|
|
|
* @return array |
131
|
|
|
*/ |
132
|
|
|
private function getElementIndexSettingsSavedData() |
133
|
|
|
{ |
134
|
|
|
return [ |
135
|
|
|
'Category' => [ |
136
|
|
|
'sourceOrder' => [ |
137
|
|
|
['heading', 'Channels'], |
138
|
|
|
['key', 'source:1'], |
139
|
|
|
], |
140
|
|
|
'sources' => [ |
141
|
|
|
'*' => [ |
142
|
|
|
'tableAttributes' => [ |
143
|
|
|
'1' => 'section', |
144
|
|
|
'2' => 'postDate', |
145
|
|
|
'3' => 'expiryDate', |
146
|
|
|
'4' => 'author', |
147
|
|
|
'5' => 'link', |
148
|
|
|
'6' => 'field:1', |
149
|
|
|
], |
150
|
|
|
], |
151
|
|
|
], |
152
|
|
|
], |
153
|
|
|
'Entry' => [ |
154
|
|
|
'sourceOrder' => [ |
155
|
|
|
['heading', 'Channels'], |
156
|
|
|
['key', 'source:1'], |
157
|
|
|
], |
158
|
|
|
'sources' => [ |
159
|
|
|
'*' => [ |
160
|
|
|
'tableAttributes' => [ |
161
|
|
|
'1' => 'section', |
162
|
|
|
'2' => 'postDate', |
163
|
|
|
'3' => 'expiryDate', |
164
|
|
|
'4' => 'author', |
165
|
|
|
'5' => 'link', |
166
|
|
|
], |
167
|
|
|
], |
168
|
|
|
], |
169
|
|
|
], |
170
|
|
|
]; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Returns element index settings exported data. |
175
|
|
|
* |
176
|
|
|
* @return array |
177
|
|
|
*/ |
178
|
|
|
private function getElementIndexSettingsExportedData() |
179
|
|
|
{ |
180
|
|
|
return [ |
181
|
|
|
'Category' => [ |
182
|
|
|
'sourceOrder' => [ |
183
|
|
|
['heading', 'Channels'], |
184
|
|
|
['key', 'source:handle'], |
185
|
|
|
], |
186
|
|
|
'sources' => [ |
187
|
|
|
'*' => [ |
188
|
|
|
'tableAttributes' => [ |
189
|
|
|
'1' => 'section', |
190
|
|
|
'2' => 'postDate', |
191
|
|
|
'3' => 'expiryDate', |
192
|
|
|
'4' => 'author', |
193
|
|
|
'5' => 'link', |
194
|
|
|
'6' => 'field:handle', |
195
|
|
|
], |
196
|
|
|
], |
197
|
|
|
], |
198
|
|
|
], |
199
|
|
|
'Entry' => [ |
200
|
|
|
'sourceOrder' => [ |
201
|
|
|
['heading', 'Channels'], |
202
|
|
|
['key', 'source:handle'], |
203
|
|
|
], |
204
|
|
|
'sources' => [ |
205
|
|
|
'*' => [ |
206
|
|
|
'tableAttributes' => [ |
207
|
|
|
'1' => 'section', |
208
|
|
|
'2' => 'postDate', |
209
|
|
|
'3' => 'expiryDate', |
210
|
|
|
'4' => 'author', |
211
|
|
|
'5' => 'link', |
212
|
|
|
], |
213
|
|
|
], |
214
|
|
|
], |
215
|
|
|
], |
216
|
|
|
]; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* @return Mock|Sources |
221
|
|
|
*/ |
222
|
|
|
private function setMockSources() |
223
|
|
|
{ |
224
|
|
|
$mockSources = $this->getMockBuilder(Sources::class) |
225
|
|
|
->disableOriginalConstructor() |
226
|
|
|
->getMock(); |
227
|
|
|
|
228
|
|
|
$mockSources->expects($this->any()) |
229
|
|
|
->method('getSource') |
230
|
|
|
->will($this->returnCallback(array($this, 'getMockSourceCallback'))); |
231
|
|
|
|
232
|
|
|
$this->setComponent(Craft::app(), 'schematic_sources', $mockSources); |
233
|
|
|
|
234
|
|
|
return $mockSources; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* @param string $fieldType |
239
|
|
|
* @param string $source |
240
|
|
|
* @param string $fromIndex |
241
|
|
|
* @param string $toIndex |
242
|
|
|
* |
243
|
|
|
* @return string |
244
|
|
|
*/ |
245
|
|
|
public function getMockSourceCallback($fieldType, $source, $fromIndex, $toIndex) |
246
|
|
|
{ |
247
|
|
|
switch ($source) { |
248
|
|
|
case 'source:handle': |
249
|
|
|
return 'source:1'; |
250
|
|
|
case 'source:1': |
251
|
|
|
return 'source:handle'; |
252
|
|
|
case 'field:handle': |
253
|
|
|
return 'field:1'; |
254
|
|
|
case 'field:1': |
255
|
|
|
return 'field:handle'; |
256
|
|
|
default: |
257
|
|
|
return $source; |
258
|
|
|
} |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* @return Mock|CraftFieldsService |
263
|
|
|
*/ |
264
|
|
|
private function setMockFieldsService() |
265
|
|
|
{ |
266
|
|
|
$mockFieldsService = $this->getMockBuilder(FieldsService::class) |
267
|
|
|
->disableOriginalConstructor() |
268
|
|
|
->getMock(); |
269
|
|
|
|
270
|
|
|
$mockFieldsService->expects($this->any()) |
271
|
|
|
->method('getFieldById') |
272
|
|
|
->with('1') |
273
|
|
|
->willReturn(new FieldModel(['handle' => 'handle'])); |
274
|
|
|
|
275
|
|
|
$this->setComponent(Craft::app(), 'fields', $mockFieldsService); |
276
|
|
|
|
277
|
|
|
return $mockFieldsService; |
278
|
|
|
} |
279
|
|
|
} |
280
|
|
|
|