|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NerdsAndCompany\Schematic\Mappers; |
|
4
|
|
|
|
|
5
|
|
|
use Craft; |
|
6
|
|
|
use Exception; |
|
7
|
|
|
use craft\base\Model; |
|
8
|
|
|
use craft\base\Plugin; |
|
9
|
|
|
use NerdsAndCompany\Schematic\Schematic; |
|
10
|
|
|
use Codeception\Test\Unit; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class Plugin Mapper Test. |
|
14
|
|
|
* |
|
15
|
|
|
* @author Nerds & Company |
|
16
|
|
|
* @copyright Copyright (c) 2015-2018, Nerds & Company |
|
17
|
|
|
* @license MIT |
|
18
|
|
|
* |
|
19
|
|
|
* @see http://www.nerds.company |
|
20
|
|
|
*/ |
|
21
|
|
|
class PluginMapperTest extends Unit |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var PluginMapper |
|
25
|
|
|
*/ |
|
26
|
|
|
private $mapper; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Set the mapper. |
|
30
|
|
|
* |
|
31
|
|
|
* @SuppressWarnings(PHPMD.CamelCaseMethodName) |
|
32
|
|
|
* phpcs:disable PSR2.Methods.MethodDeclaration.Underscore |
|
33
|
|
|
*/ |
|
34
|
|
|
protected function _before() |
|
35
|
|
|
{ |
|
36
|
|
|
$this->mapper = new PluginMapper(); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
//============================================================================================================== |
|
40
|
|
|
//================================================= TESTS ==================================================== |
|
41
|
|
|
//============================================================================================================== |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Test default import functionality. |
|
45
|
|
|
*/ |
|
46
|
|
|
public function testImportWithInstalledPlugins() |
|
47
|
|
|
{ |
|
48
|
|
|
$definitions = $this->getPluginsDefinition(); |
|
49
|
|
|
$data = $this->getPluginsData(); |
|
50
|
|
|
|
|
51
|
|
|
$this->mockPluginsService(); |
|
52
|
|
|
|
|
53
|
|
|
$result = $this->mapper->import($definitions, $data); |
|
54
|
|
|
|
|
55
|
|
|
$this->assertCount(count($definitions), $result); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Test default import functionality. |
|
60
|
|
|
*/ |
|
61
|
|
|
public function testImportWithInstalledDisabledPlugins() |
|
62
|
|
|
{ |
|
63
|
|
|
$this->getMockplugin(); |
|
64
|
|
|
|
|
65
|
|
|
$definitions = $this->getPluginsDefinition(); |
|
66
|
|
|
$definitions['pluginHandle']['isEnabled'] = false; |
|
67
|
|
|
$data = $this->getPluginsData(); |
|
68
|
|
|
|
|
69
|
|
|
$this->mockPluginsService(); |
|
70
|
|
|
|
|
71
|
|
|
$result = $this->mapper->import($definitions, $data); |
|
72
|
|
|
|
|
73
|
|
|
$this->assertCount(count($definitions), $result); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Test default import functionality. |
|
78
|
|
|
*/ |
|
79
|
|
|
public function testImportWithMissingPlugin() |
|
80
|
|
|
{ |
|
81
|
|
|
$definitions = $this->getPluginsDefinition(); |
|
82
|
|
|
|
|
83
|
|
|
$this->mockPluginsService(false); |
|
84
|
|
|
|
|
85
|
|
|
$result = $this->mapper->import($definitions, []); |
|
86
|
|
|
|
|
87
|
|
|
$this->assertCount(0, $result); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Test default import functionality. |
|
92
|
|
|
*/ |
|
93
|
|
|
public function testImportWithInstallException() |
|
94
|
|
|
{ |
|
95
|
|
|
$definitions = $this->getPluginsDefinition(); |
|
96
|
|
|
$data = $this->getPluginsData(); |
|
97
|
|
|
|
|
98
|
|
|
$this->mockPluginsService(true, false); |
|
99
|
|
|
|
|
100
|
|
|
$this->expectException(Exception::class); |
|
101
|
|
|
|
|
102
|
|
|
$this->mapper->import($definitions, $data); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Test default import functionality. |
|
107
|
|
|
*/ |
|
108
|
|
|
public function testImportWithNotInstalledPlugin() |
|
109
|
|
|
{ |
|
110
|
|
|
$this->getMockplugin(); |
|
111
|
|
|
|
|
112
|
|
|
$definitions = $this->getPluginsDefinition(); |
|
113
|
|
|
$definitions['pluginHandle']['isInstalled'] = false; |
|
114
|
|
|
$data = $this->getPluginsData(); |
|
115
|
|
|
|
|
116
|
|
|
$this->mockPluginsService(); |
|
117
|
|
|
|
|
118
|
|
|
$result = $this->mapper->import($definitions, $data); |
|
119
|
|
|
|
|
120
|
|
|
$this->assertCount(0, $result); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Test default import functionality. |
|
125
|
|
|
*/ |
|
126
|
|
|
public function testImportWithForce() |
|
127
|
|
|
{ |
|
128
|
|
|
$data = $this->getPluginsData(); |
|
129
|
|
|
$data['pluginHandle']['isInstalled'] = true; |
|
130
|
|
|
|
|
131
|
|
|
$this->mockPluginsService(false); |
|
132
|
|
|
|
|
133
|
|
|
Craft::$app->plugins->expects($this->exactly(1)) |
|
134
|
|
|
->method('uninstallPlugin') |
|
135
|
|
|
->willReturn(true); |
|
136
|
|
|
|
|
137
|
|
|
Schematic::$force = true; |
|
138
|
|
|
$result = $this->mapper->import([], $data); |
|
139
|
|
|
|
|
140
|
|
|
$this->assertCount(0, $result); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Test export functionality. |
|
145
|
|
|
*/ |
|
146
|
|
|
public function testExport() |
|
147
|
|
|
{ |
|
148
|
|
|
$data = $this->getPluginsData(); |
|
149
|
|
|
$data['pluginHandle']['isInstalled'] = true; |
|
150
|
|
|
$data['pluginHandle']['isEnabled'] = true; |
|
151
|
|
|
|
|
152
|
|
|
$definitions = $this->getPluginsDefinition(); |
|
153
|
|
|
$this->mockPluginsService(); |
|
154
|
|
|
|
|
155
|
|
|
$result = $this->mapper->export($data); |
|
156
|
|
|
|
|
157
|
|
|
$this->assertEquals($definitions, $result); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
//============================================================================================================== |
|
161
|
|
|
//================================================ HELPERS =================================================== |
|
162
|
|
|
//============================================================================================================== |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @param bool $returnPlugin |
|
166
|
|
|
* @param bool $installPluginResponse |
|
167
|
|
|
* @param bool $enablePluginResponse |
|
168
|
|
|
* @param bool $disablePluginResponse |
|
169
|
|
|
* @param bool $saveSettingsResponse |
|
170
|
|
|
*/ |
|
171
|
|
|
private function mockPluginsService( |
|
172
|
|
|
$returnPlugin = true, |
|
173
|
|
|
$installPluginResponse = true, |
|
174
|
|
|
$enablePluginResponse = true, |
|
175
|
|
|
$disablePluginResponse = true, |
|
176
|
|
|
$saveSettingsResponse = true |
|
177
|
|
|
) { |
|
178
|
|
|
Craft::$app->plugins->expects($this->any()) |
|
179
|
|
|
->method('getPlugin') |
|
180
|
|
|
->willReturn(($returnPlugin) ? $this->getMockplugin() : null); |
|
181
|
|
|
|
|
182
|
|
|
if ($installPluginResponse) { |
|
183
|
|
|
Craft::$app->plugins->expects($this->any())->method('installPlugin')->willReturn($installPluginResponse); |
|
184
|
|
|
} else { |
|
185
|
|
|
Craft::$app->plugins->expects($this->any())->method('installPlugin')->willThrowException(new Exception()); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
Craft::$app->plugins->expects($this->any())->method('enablePlugin')->willReturn($enablePluginResponse); |
|
189
|
|
|
Craft::$app->plugins->expects($this->any())->method('disablePlugin')->willReturn($disablePluginResponse); |
|
190
|
|
|
Craft::$app->plugins->expects($this->any())->method('savePluginSettings')->willReturn($saveSettingsResponse); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* @return Mock|plugin |
|
195
|
|
|
*/ |
|
196
|
|
|
private function getMockplugin() |
|
197
|
|
|
{ |
|
198
|
|
|
$mockPlugin = $this->getMockBuilder(Plugin::class) |
|
199
|
|
|
->disableOriginalConstructor() |
|
200
|
|
|
->getMock(); |
|
201
|
|
|
|
|
202
|
|
|
$mockPlugin->expects($this->any()) |
|
203
|
|
|
->method('getSettings') |
|
204
|
|
|
->willReturn($this->getMockSettings()); |
|
205
|
|
|
|
|
206
|
|
|
return $mockPlugin; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* Get mock settings. |
|
211
|
|
|
* |
|
212
|
|
|
* @return Mock|Model |
|
213
|
|
|
*/ |
|
214
|
|
|
private function getMockSettings() |
|
215
|
|
|
{ |
|
216
|
|
|
$mockSettings = $this->getMockBuilder(Model::class)->getMock(); |
|
217
|
|
|
$mockSettings->expects($this->any()) |
|
218
|
|
|
->method('__get') |
|
219
|
|
|
->willReturnMap([ |
|
220
|
|
|
['attributes', [ |
|
221
|
|
|
'pluginName' => 'Menu', |
|
222
|
|
|
'canDoActions' => '', |
|
223
|
|
|
'quietErrors' => '', |
|
224
|
|
|
]], |
|
225
|
|
|
]); |
|
226
|
|
|
|
|
227
|
|
|
return $mockSettings; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* Returns plugins data. |
|
232
|
|
|
* |
|
233
|
|
|
* @return array |
|
234
|
|
|
*/ |
|
235
|
|
|
private function getPluginsData() |
|
236
|
|
|
{ |
|
237
|
|
|
return [ |
|
238
|
|
|
'pluginHandle' => [ |
|
239
|
|
|
'isInstalled' => false, |
|
240
|
|
|
'isEnabled' => false, |
|
241
|
|
|
'settings' => [ |
|
242
|
|
|
'pluginName' => 'Menu', |
|
243
|
|
|
'canDoActions' => '', |
|
244
|
|
|
'quietErrors' => '', |
|
245
|
|
|
], |
|
246
|
|
|
], |
|
247
|
|
|
]; |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
/** |
|
251
|
|
|
* Returns plugins data. |
|
252
|
|
|
* |
|
253
|
|
|
* @return array |
|
254
|
|
|
*/ |
|
255
|
|
|
private function getPluginsDefinition() |
|
256
|
|
|
{ |
|
257
|
|
|
return [ |
|
258
|
|
|
'pluginHandle' => [ |
|
259
|
|
|
'isInstalled' => true, |
|
260
|
|
|
'isEnabled' => true, |
|
261
|
|
|
'settings' => [ |
|
262
|
|
|
'pluginName' => 'Menu', |
|
263
|
|
|
'canDoActions' => '', |
|
264
|
|
|
'quietErrors' => '', |
|
265
|
|
|
], |
|
266
|
|
|
], |
|
267
|
|
|
]; |
|
268
|
|
|
} |
|
269
|
|
|
} |
|
270
|
|
|
|