1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\Core\Tests\Manifest; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Core\Manifest\ResourceLoader; |
6
|
|
|
use SilverStripe\View\ThemeManifest; |
7
|
|
|
use SilverStripe\Dev\SapphireTest; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Tests for the {@link TemplateLoader} class. |
11
|
|
|
*/ |
12
|
|
|
class ResourceLoaderTest extends SapphireTest |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
private $base; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var ThemeManifest |
21
|
|
|
*/ |
22
|
|
|
private $manifest; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var ResourceLoader |
26
|
|
|
*/ |
27
|
|
|
private $loader; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Set up manifest before each test |
31
|
|
|
*/ |
32
|
|
|
protected function setUp() |
33
|
|
|
{ |
34
|
|
|
parent::setUp(); |
35
|
|
|
|
36
|
|
|
// Fake project root |
37
|
|
|
$this->base = dirname(__FILE__) . '/fixtures/templatemanifest'; |
38
|
|
|
// New ThemeManifest for that root |
39
|
|
|
$this->manifest = new ThemeManifest($this->base, 'myproject'); |
40
|
|
|
$this->manifest->init(); |
41
|
|
|
// New Loader for that root |
42
|
|
|
$this->loader = new ResourceLoader($this->base); |
43
|
|
|
$this->loader->addSet('$default', $this->manifest); |
|
|
|
|
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Test that 'main' and 'Layout' templates are loaded from module |
48
|
|
|
*/ |
49
|
|
View Code Duplication |
public function testFindTemplatesInModule() |
|
|
|
|
50
|
|
|
{ |
51
|
|
|
$this->assertEquals( |
52
|
|
|
"$this->base/module/templates/Page.ss", |
53
|
|
|
$this->loader->findTemplate('Page', ['$default']) |
54
|
|
|
); |
55
|
|
|
|
56
|
|
|
$this->assertEquals( |
57
|
|
|
"$this->base/module/templates/Layout/Page.ss", |
58
|
|
|
$this->loader->findTemplate(['type' => 'Layout', 'Page'], ['$default']) |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function testFindNestedThemeTemplates() |
63
|
|
|
{ |
64
|
|
|
// Without including the theme this template cannot be found |
65
|
|
|
$this->assertEquals(null, $this->loader->findTemplate('NestedThemePage', ['$default'])); |
66
|
|
|
|
67
|
|
|
// With a nested theme available then it is available |
68
|
|
|
$this->assertEquals( |
69
|
|
|
"{$this->base}/module/themes/subtheme/templates/NestedThemePage.ss", |
70
|
|
|
$this->loader->findTemplate( |
71
|
|
|
'NestedThemePage', |
72
|
|
|
[ |
73
|
|
|
'silverstripe/module:subtheme', |
74
|
|
|
'$default' |
75
|
|
|
] |
76
|
|
|
) |
77
|
|
|
); |
78
|
|
|
|
79
|
|
|
// Can also be found if excluding $default theme |
80
|
|
|
$this->assertEquals( |
81
|
|
|
"{$this->base}/module/themes/subtheme/templates/NestedThemePage.ss", |
82
|
|
|
$this->loader->findTemplate( |
83
|
|
|
'NestedThemePage', |
84
|
|
|
[ |
85
|
|
|
'silverstripe/module:subtheme', |
86
|
|
|
] |
87
|
|
|
) |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function testFindTemplateByType() |
92
|
|
|
{ |
93
|
|
|
// Test that "type" is respected properly |
94
|
|
|
$this->assertEquals( |
95
|
|
|
"{$this->base}/module/templates/MyNamespace/Layout/MyClass.ss", |
96
|
|
|
$this->loader->findTemplate( |
97
|
|
|
[ |
98
|
|
|
[ |
99
|
|
|
'type' => 'Layout', |
100
|
|
|
'MyNamespace/NonExistantTemplate' |
101
|
|
|
], |
102
|
|
|
[ |
103
|
|
|
'type' => 'Layout', |
104
|
|
|
'MyNamespace/MyClass' |
105
|
|
|
], |
106
|
|
|
'MyNamespace/MyClass' |
107
|
|
|
], |
108
|
|
|
[ |
109
|
|
|
'silverstripe/module:subtheme', |
110
|
|
|
'theme', |
111
|
|
|
'$default', |
112
|
|
|
] |
113
|
|
|
) |
114
|
|
|
); |
115
|
|
|
|
116
|
|
|
// Non-typed template can be found even if looking for typed theme at a lower priority |
117
|
|
|
$this->assertEquals( |
118
|
|
|
"{$this->base}/module/templates/MyNamespace/MyClass.ss", |
119
|
|
|
$this->loader->findTemplate( |
120
|
|
|
[ |
121
|
|
|
[ |
122
|
|
|
'type' => 'Layout', |
123
|
|
|
'MyNamespace/NonExistantTemplate' |
124
|
|
|
], |
125
|
|
|
'MyNamespace/MyClass', |
126
|
|
|
[ |
127
|
|
|
'type' => 'Layout', |
128
|
|
|
'MyNamespace/MyClass' |
129
|
|
|
] |
130
|
|
|
], |
131
|
|
|
[ |
132
|
|
|
'silverstripe/module', |
133
|
|
|
'theme', |
134
|
|
|
'$default', |
135
|
|
|
] |
136
|
|
|
) |
137
|
|
|
); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function testFindTemplatesByPath() |
141
|
|
|
{ |
142
|
|
|
// Items given as full paths are returned directly |
143
|
|
|
$this->assertEquals( |
144
|
|
|
"$this->base/themes/theme/templates/Page.ss", |
145
|
|
|
$this->loader->findTemplate("$this->base/themes/theme/templates/Page.ss", ['theme']) |
146
|
|
|
); |
147
|
|
|
|
148
|
|
|
$this->assertEquals( |
149
|
|
|
"$this->base/themes/theme/templates/Page.ss", |
150
|
|
|
$this->loader->findTemplate( |
151
|
|
|
[ |
152
|
|
|
"$this->base/themes/theme/templates/Page.ss", |
153
|
|
|
"Page" |
154
|
|
|
], |
155
|
|
|
['theme'] |
156
|
|
|
) |
157
|
|
|
); |
158
|
|
|
|
159
|
|
|
// Ensure checks for file_exists |
160
|
|
|
$this->assertEquals( |
161
|
|
|
"$this->base/themes/theme/templates/Page.ss", |
162
|
|
|
$this->loader->findTemplate( |
163
|
|
|
[ |
164
|
|
|
"$this->base/themes/theme/templates/NotAPage.ss", |
165
|
|
|
"$this->base/themes/theme/templates/Page.ss", |
166
|
|
|
], |
167
|
|
|
['theme'] |
168
|
|
|
) |
169
|
|
|
); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Test that 'main' and 'Layout' templates are loaded from set theme |
174
|
|
|
*/ |
175
|
|
View Code Duplication |
public function testFindTemplatesInTheme() |
|
|
|
|
176
|
|
|
{ |
177
|
|
|
$this->assertEquals( |
178
|
|
|
"$this->base/themes/theme/templates/Page.ss", |
179
|
|
|
$this->loader->findTemplate('Page', ['theme']) |
180
|
|
|
); |
181
|
|
|
|
182
|
|
|
$this->assertEquals( |
183
|
|
|
"$this->base/themes/theme/templates/Layout/Page.ss", |
184
|
|
|
$this->loader->findTemplate(['type' => 'Layout', 'Page'], ['theme']) |
185
|
|
|
); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Test that 'main' and 'Layout' templates are loaded from project without a set theme |
190
|
|
|
*/ |
191
|
|
|
public function testFindTemplatesInApplication() |
192
|
|
|
{ |
193
|
|
|
// TODO: replace with one that doesn't create temporary files (so bad) |
194
|
|
|
$templates = array( |
195
|
|
|
$this->base . '/myproject/templates/Page.ss', |
196
|
|
|
$this->base . '/myproject/templates/Layout/Page.ss' |
197
|
|
|
); |
198
|
|
|
$this->createTestTemplates($templates); |
199
|
|
|
|
200
|
|
|
$this->assertEquals( |
201
|
|
|
"$this->base/myproject/templates/Page.ss", |
202
|
|
|
$this->loader->findTemplate('Page', ['$default']) |
203
|
|
|
); |
204
|
|
|
|
205
|
|
|
$this->assertEquals( |
206
|
|
|
"$this->base/myproject/templates/Layout/Page.ss", |
207
|
|
|
$this->loader->findTemplate(['type' => 'Layout', 'Page'], ['$default']) |
208
|
|
|
); |
209
|
|
|
|
210
|
|
|
$this->removeTestTemplates($templates); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Test that 'main' template is found in theme and 'Layout' is found in module |
215
|
|
|
*/ |
216
|
|
View Code Duplication |
public function testFindTemplatesMainThemeLayoutModule() |
|
|
|
|
217
|
|
|
{ |
218
|
|
|
$this->assertEquals( |
219
|
|
|
"$this->base/themes/theme/templates/CustomThemePage.ss", |
220
|
|
|
$this->loader->findTemplate('CustomThemePage', ['theme', '$default']) |
221
|
|
|
); |
222
|
|
|
|
223
|
|
|
$this->assertEquals( |
224
|
|
|
"$this->base/module/templates/Layout/CustomThemePage.ss", |
225
|
|
|
$this->loader->findTemplate(['type' => 'Layout', 'CustomThemePage'], ['theme', '$default']) |
226
|
|
|
); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
View Code Duplication |
public function testFindThemedCSS() |
|
|
|
|
230
|
|
|
{ |
231
|
|
|
$this->assertEquals( |
232
|
|
|
"myproject/css/project.css", |
233
|
|
|
$this->loader->findThemedCSS('project', ['$default', 'theme']) |
234
|
|
|
); |
235
|
|
|
$this->assertEquals( |
236
|
|
|
"themes/theme/css/project.css", |
237
|
|
|
$this->loader->findThemedCSS('project', ['theme', '$default']) |
238
|
|
|
); |
239
|
|
|
$this->assertEmpty( |
240
|
|
|
$this->loader->findThemedCSS('nofile', ['theme', '$default']) |
241
|
|
|
); |
242
|
|
|
$this->assertEquals( |
243
|
|
|
'module/css/content.css', |
244
|
|
|
$this->loader->findThemedCSS('content', ['/module', 'theme']) |
245
|
|
|
); |
246
|
|
|
$this->assertEquals( |
247
|
|
|
'module/css/content.css', |
248
|
|
|
$this->loader->findThemedCSS('content', ['/module', 'theme', '$default']) |
249
|
|
|
); |
250
|
|
|
$this->assertEquals( |
251
|
|
|
'module/css/content.css', |
252
|
|
|
$this->loader->findThemedCSS('content', ['$default', '/module', 'theme']) |
253
|
|
|
); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
View Code Duplication |
public function testFindThemedJavascript() |
|
|
|
|
257
|
|
|
{ |
258
|
|
|
$this->assertEquals( |
259
|
|
|
"myproject/javascript/project.js", |
260
|
|
|
$this->loader->findThemedJavascript('project', ['$default', 'theme']) |
261
|
|
|
); |
262
|
|
|
$this->assertEquals( |
263
|
|
|
"themes/theme/javascript/project.js", |
264
|
|
|
$this->loader->findThemedJavascript('project', ['theme', '$default']) |
265
|
|
|
); |
266
|
|
|
$this->assertEmpty( |
267
|
|
|
$this->loader->findThemedJavascript('nofile', ['theme', '$default']) |
268
|
|
|
); |
269
|
|
|
$this->assertEquals( |
270
|
|
|
'module/javascript/content.js', |
271
|
|
|
$this->loader->findThemedJavascript('content', ['/module', 'theme']) |
272
|
|
|
); |
273
|
|
|
$this->assertEquals( |
274
|
|
|
'module/javascript/content.js', |
275
|
|
|
$this->loader->findThemedJavascript('content', ['/module', 'theme', '$default']) |
276
|
|
|
); |
277
|
|
|
$this->assertEquals( |
278
|
|
|
'module/javascript/content.js', |
279
|
|
|
$this->loader->findThemedJavascript('content', ['$default', '/module', 'theme']) |
280
|
|
|
); |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
protected function createTestTemplates($templates) |
284
|
|
|
{ |
285
|
|
|
foreach ($templates as $template) { |
286
|
|
|
file_put_contents($template, ''); |
287
|
|
|
} |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
protected function removeTestTemplates($templates) |
291
|
|
|
{ |
292
|
|
|
foreach ($templates as $template) { |
293
|
|
|
unlink($template); |
294
|
|
|
} |
295
|
|
|
} |
296
|
|
|
} |
297
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: