/
RequirementsTest.php
183 lines (142 loc) · 7.78 KB
/
RequirementsTest.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
<?php
/**
* @package sapphire
* @subpackage tests
*
* @todo Test that order of combine_files() is correct
* @todo Figure out how to clear the modified state of Requirements class - might affect other tests.
*/
class RequirementsTest extends SapphireTest {
static $html_template = '<html><head></head><body></body></html>';
function testExternalUrls() {
$backend = new Requirements_Backend;
$backend->set_combined_files_enabled(true);
$backend->javascript('http://www.mydomain.com/test.js');
$backend->javascript('https://www.mysecuredomain.com/test.js');
$backend->css('http://www.mydomain.com/test.css');
$backend->css('https://www.mysecuredomain.com/test.css');
$html = $backend->includeInHTML(false, self::$html_template);
$this->assertTrue(
(strpos($html, 'http://www.mydomain.com/test.js') !== false),
'Load external javascript URL'
);
$this->assertTrue(
(strpos($html, 'https://www.mysecuredomain.com/test.js') !== false),
'Load external secure javascript URL'
);
$this->assertTrue(
(strpos($html, 'http://www.mydomain.com/test.css') !== false),
'Load external CSS URL'
);
$this->assertTrue(
(strpos($html, 'https://www.mysecuredomain.com/test.css') !== false),
'Load external secure CSS URL'
);
}
protected function setupCombinedRequirements($backend) {
$backend->clear();
// clearing all previously generated requirements (just in case)
$backend->clear_combined_files();
$backend->delete_combined_files('RequirementsTest_bc.js');
// require files normally (e.g. called from a FormField instance)
$backend->javascript(SAPPHIRE_DIR . '/tests/forms/RequirementsTest_a.js');
$backend->javascript(SAPPHIRE_DIR . '/tests/forms/RequirementsTest_b.js');
$backend->javascript(SAPPHIRE_DIR . '/tests/forms/RequirementsTest_c.js');
// require two of those files as combined includes
$backend->combine_files(
'RequirementsTest_bc.js',
array(
SAPPHIRE_DIR . '/tests/forms/RequirementsTest_b.js',
SAPPHIRE_DIR . '/tests/forms/RequirementsTest_c.js'
)
);
}
function testCombinedJavascript() {
$backend = new Requirements_Backend;
$backend->set_combined_files_enabled(true);
$this->setupCombinedRequirements($backend);
$combinedFilePath = Director::baseFolder() . '/' . 'RequirementsTest_bc.js';
$html = $backend->includeInHTML(false, self::$html_template);
/* COMBINED JAVASCRIPT FILE IS INCLUDED IN HTML HEADER */
$this->assertTrue((bool)preg_match('/src=".*\/RequirementsTest_bc\.js/', $html), 'combined javascript file is included in html header');
/* COMBINED JAVASCRIPT FILE EXISTS */
$this->assertTrue(file_exists($combinedFilePath), 'combined javascript file exists');
/* COMBINED JAVASCRIPT HAS CORRECT CONTENT */
$this->assertTrue((strpos(file_get_contents($combinedFilePath), "alert('b')") !== false), 'combined javascript has correct content');
$this->assertTrue((strpos(file_get_contents($combinedFilePath), "alert('c')") !== false), 'combined javascript has correct content');
/* COMBINED FILES ARE NOT INCLUDED TWICE */
$this->assertFalse((bool)preg_match('/src=".*\/RequirementsTest_b\.js/', $html), 'combined files are not included twice');
$this->assertFalse((bool)preg_match('/src=".*\/RequirementsTest_c\.js/', $html), 'combined files are not included twice');
/* NORMAL REQUIREMENTS ARE STILL INCLUDED */
$this->assertTrue((bool)preg_match('/src=".*\/RequirementsTest_a\.js/', $html), 'normal requirements are still included');
$backend->delete_combined_files('RequirementsTest_bc.js');
}
function testBlockedCombinedJavascript() {
$backend = new Requirements_Backend;
$backend->set_combined_files_enabled(true);
$combinedFilePath = Director::baseFolder() . '/' . 'RequirementsTest_bc.js';
/* BLOCKED COMBINED FILES ARE NOT INCLUDED */
$this->setupCombinedRequirements($backend);
$backend->block('RequirementsTest_bc.js');
$backend->delete_combined_files('RequirementsTest_bc.js');
clearstatcache(); // needed to get accurate file_exists() results
$html = $backend->includeInHTML(false, self::$html_template);
$this->assertFalse((bool)preg_match('/src=".*\/RequirementsTest_bc\.js/', $html), 'blocked combined files are not included ');
$backend->unblock('RequirementsTest_bc.js');
/* BLOCKED UNCOMBINED FILES ARE NOT INCLUDED */
$this->setupCombinedRequirements($backend);
$backend->block('sapphire/tests/forms/RequirementsTest_b.js');
$backend->delete_combined_files('RequirementsTest_bc.js');
clearstatcache(); // needed to get accurate file_exists() results
$html = $backend->includeInHTML(false, self::$html_template);
$this->assertFalse((strpos(file_get_contents($combinedFilePath), "alert('b')") !== false), 'blocked uncombined files are not included');
$backend->unblock('RequirementsTest_b.js');
/* A SINGLE FILE CAN'T BE INCLUDED IN TWO COMBINED FILES */
$this->setupCombinedRequirements($backend);
clearstatcache(); // needed to get accurate file_exists() results
// This throws a notice-level error, so we prefix with @
@$backend->combine_files(
'RequirementsTest_ac.js',
array(
'sapphire/tests/forms/RequirementsTest_a.js',
'sapphire/tests/forms/RequirementsTest_c.js'
)
);
$combinedFiles = $backend->get_combine_files();
$this->assertEquals(
array_keys($combinedFiles),
array('RequirementsTest_bc.js'),
"A single file can't be included in two combined files"
);
$backend->delete_combined_files('RequirementsTest_bc.js');
}
function testArgsInUrls() {
$backend = new Requirements_Backend;
$backend->set_combined_files_enabled(true);
$backend->javascript(SAPPHIRE_DIR . '/tests/forms/RequirementsTest_a.js?test=1&test=2&test=3');
$backend->css(SAPPHIRE_DIR . '/tests/forms/RequirementsTest_a.css?test=1&test=2&test=3');
Requirements::delete_combined_files('RequirementsTest_bc.js');
$html = $backend->includeInHTML(false, self::$html_template);
/* Javascript has correct path */
$this->assertTrue((bool)preg_match('/src=".*\/RequirementsTest_a\.js\?m=\d\d+&test=1&test=2&test=3/', $html), 'javascript has correct path');
/* CSS has correct path */
$this->assertTrue((bool)preg_match('/href=".*\/RequirementsTest_a\.css\?m=\d\d+&test=1&test=2&test=3/', $html), 'css has correct path');
}
function testRequirementsBackend() {
$backend = new Requirements_Backend();
$backend->javascript(SAPPHIRE_DIR . '/tests/forms/a.js');
$this->assertTrue(count($backend->get_javascript()) == 1, "There should be only 1 file included in required javascript.");
$this->assertTrue(in_array(SAPPHIRE_DIR . '/tests/forms/a.js', $backend->get_javascript()), "/test/forms/a.js should be included in required javascript.");
$backend->javascript(SAPPHIRE_DIR . '/tests/forms/b.js');
$this->assertTrue(count($backend->get_javascript()) == 2, "There should be 2 files included in required javascript.");
$backend->block(SAPPHIRE_DIR . '/tests/forms/a.js');
$this->assertTrue(count($backend->get_javascript()) == 1, "There should be only 1 file included in required javascript.");
$this->assertFalse(in_array(SAPPHIRE_DIR . '/tests/forms/a.js', $backend->get_javascript()), "/test/forms/a.js should not be included in required javascript after it has been blocked.");
$this->assertTrue(in_array(SAPPHIRE_DIR . '/tests/forms/b.js', $backend->get_javascript()), "/test/forms/b.js should be included in required javascript.");
$backend->css(SAPPHIRE_DIR . '/tests/forms/a.css');
$this->assertTrue(count($backend->get_css()) == 1, "There should be only 1 file included in required css.");
$this->assertArrayHasKey(SAPPHIRE_DIR . '/tests/forms/a.css', $backend->get_css(), "/tests/forms/a.css should be in required css.");
$backend->block(SAPPHIRE_DIR . '/tests/forms/a.css');
$this->assertTrue(count($backend->get_css()) == 0, "There should be nothing in required css after file has been blocked.");
}
}