forked from timber/timber
/
test-timber-loader.php
189 lines (159 loc) · 6.54 KB
/
test-timber-loader.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
class TestTimberLoader extends Timber_UnitTestCase {
function testTwigLoaderFilter() {
$php_unit = $this;
add_filter('timber/loader/loader', function ($loader) use ($php_unit) {
$php_unit->assertInstanceOf('Twig_LoaderInterface', $loader);
return $loader;
});
$str = Timber::compile('assets/single.twig', array());
}
function testBogusTemplate() {
$str = Timber::compile('assets/darkhelmet.twig');
$this->assertFalse($str);
}
function testBogusTemplates() {
$str = Timber::compile( array('assets/barf.twig', 'assets/lonestar.twig') );
$this->assertFalse($str);
}
function testTwigPathFilter() {
$php_unit = $this;
add_filter('timber/loader/paths', function($paths) use ($php_unit) {
$paths = call_user_func_array('array_merge', $paths);
$count = count($paths);
$php_unit->assertEquals(3, count($paths));
$pos = array_search('/', $paths);
unset($paths[$pos]);
$php_unit->assertEquals(2, count($paths));
return $paths;
});
$str = Timber::compile('assets/single.twig', array());
}
function testTwigLoadsFromChildTheme(){
$this->_setupParentTheme();
$this->_setupChildTheme();
$this->assertFileExists(WP_CONTENT_DIR.'/themes/fake-child-theme/style.css');
switch_theme('fake-child-theme');
$child_theme = get_stylesheet_directory_uri();
$this->assertEquals(WP_CONTENT_URL.'/themes/fake-child-theme', $child_theme);
$context = array();
$str = Timber::compile('single.twig', $context);
$this->assertEquals('I am single.twig', trim($str));
}
static function _setupChildTheme(){
$dest_dir = WP_CONTENT_DIR.'/themes/fake-child-theme';
if (!file_exists($dest_dir)) {
mkdir($dest_dir, 0777, true);
}
if (!file_exists($dest_dir.'/views')) {
mkdir($dest_dir.'/views', 0777, true);
}
copy(__DIR__.'/assets/style.css', $dest_dir.'/style.css');
copy(__DIR__.'/assets/single.twig', $dest_dir.'/views/single.twig');
}
static function _setupParentTheme(){
$dest_dir = WP_CONTENT_DIR.'/themes/twentyfifteen';
if (!file_exists($dest_dir.'/views')) {
mkdir($dest_dir.'/views', 0777, true);
}
copy(__DIR__.'/assets/single-parent.twig', $dest_dir.'/views/single.twig');
copy(__DIR__.'/assets/single-parent.twig', $dest_dir.'/views/single-parent.twig');
}
function testTwigLoadsFromParentTheme(){
$this->_setupParentTheme();
$this->_setupChildTheme();
switch_theme('fake-child-theme');
$templates = array('single-parent.twig');
$str = Timber::compile($templates, array());
$this->assertEquals('I am single.twig in parent theme', trim($str));
}
function _setupRelativeViews(){
if (!file_exists(__DIR__.'/views')) {
mkdir(__DIR__.'/views', 0777, true);
}
copy(__DIR__.'/assets/relative.twig', __DIR__.'/views/single.twig');
}
function _teardownRelativeViews(){
if (file_exists(__DIR__.'/views/single.twig')){
unlink(__DIR__.'/views/single.twig');
}
if (file_exists(__DIR__.'/views')) {
rmdir(__DIR__.'/views');
}
}
function testTwigLoadsFromRelativeToScript(){
$this->_setupRelativeViews();
$str = Timber::compile('single.twig');
$this->assertEquals('I am in the assets directory', trim($str));
$this->_teardownRelativeViews();
}
function testTwigLoadsFromAbsolutePathOnServer(){
$str = Timber::compile(__DIR__.'/assets/image-test.twig');
$this->assertEquals('<img src="" />', trim($str));
}
function _testTwigLoadsFromAbsolutePathOnServerWithSecurityRestriction(){
$str = Timber::compile('assets/single-foo.twig');
}
function testTwigLoadsFromAlternateDirName(){
Timber::$dirname = array('foo', 'views');
if (!file_exists(get_template_directory().'/foo')) {
mkdir(get_template_directory().'/foo', 0777, true);
}
copy(__DIR__.'/assets/single-foo.twig', get_template_directory().'/foo/single-foo.twig');
$str = Timber::compile('single-foo.twig');
$this->assertEquals('I am single-foo', trim($str));
}
function testTwigLoadsFromLocation(){
Timber::$locations = __DIR__.'/assets';
$str = Timber::compile('thumb-test.twig');
$this->assertEquals('<img src="" />', trim($str));
}
function testTwigLoadsFromLocationWithNamespace(){
Timber::$locations = array( __DIR__.'/assets' => 'assets' );
$str = Timber::compile('@assets/thumb-test.twig');
$this->assertEquals('<img src="" />', trim($str));
}
function testTwigLoadsFromLocationWithNestedNamespace(){
Timber::$locations = array( __DIR__.'/namespaced' => 'namespaced' );
$str = Timber::compile('@namespaced/test-nested.twig');
$this->assertEquals('This is a namespaced template.', trim($str));
}
function testTwigLoadsFromLocationWithAndWithoutNamespaces(){
Timber::$locations = array( __DIR__.'/namespaced' => 'namespaced', __DIR__ . '/assets' );
// Namespaced location
$str = Timber::compile('@namespaced/test-namespaced.twig');
$this->assertEquals('This is a namespaced template.', trim($str));
// Non namespaced location
$str = Timber::compile('thumb-test.twig');
$this->assertEquals('<img src="" />', trim($str));
}
function testTwigLoadsFromLocationWithAndWithoutNamespacesAndDirs(){
Timber::$dirname = array('foo', 'views');
Timber::$locations = array( __DIR__.'/namespaced' => 'namespaced', __DIR__ . '/assets' );
// Namespaced location
$str = Timber::compile('@namespaced/test-namespaced.twig');
$this->assertEquals('This is a namespaced template.', trim($str));
// Non namespaced location
$str = Timber::compile('thumb-test.twig');
$this->assertEquals('<img src="" />', trim($str));
if (!file_exists(get_template_directory().'/foo')) {
mkdir(get_template_directory().'/foo', 0777, true);
}
copy(__DIR__.'/assets/single-foo.twig', get_template_directory().'/foo/single-foo.twig');
// Dir
$str = Timber::compile('single-foo.twig');
$this->assertEquals('I am single-foo', trim($str));
}
function testTwigLoadsFromMultipleLocationsWithNamespace(){
Timber::$locations = array( __DIR__.'/assets' => 'assets', __DIR__ .'/namespaced' => 'assets' );
$str = Timber::compile('@assets/thumb-test.twig');
$this->assertEquals('<img src="" />', trim($str));
$str = Timber::compile('@assets/test-namespaced.twig');
$this->assertEquals('This is a namespaced template.', trim($str));
}
function testTwigLoadsFirstTemplateWhenMultipleLocationsWithSameNamespace(){
Timber::$locations = array( __DIR__.'/assets' => 'assets', __DIR__ .'/namespaced' => 'assets' );
$str = Timber::compile('@assets/thumb-test.twig');
$this->assertEquals('<img src="" />', trim($str));
}
}