Commit | Line | Data |
---|---|---|
87e3fe24 TO |
1 | <?php |
2 | ||
3 | namespace E2E\Core; | |
4 | ||
5 | use Civi\Core\AssetBuilder; | |
6 | use Civi\Core\Event\GenericHookEvent; | |
7 | ||
87e3fe24 TO |
8 | /** |
9 | * Class AssetBuilderTest | |
10 | * @package E2E\Core | |
11 | * @group e2e | |
12 | */ | |
13 | class AssetBuilderTest extends \CiviEndToEndTestCase { | |
14 | ||
15 | protected $fired; | |
16 | ||
17 | /** | |
18 | * @inheritDoc | |
19 | */ | |
20 | protected function setUp() { | |
21 | parent::setUp(); | |
22 | ||
23 | \Civi::service('asset_builder')->clear(); | |
24 | ||
25 | $this->fired['hook_civicrm_buildAsset'] = 0; | |
26 | \Civi::dispatcher()->addListener('hook_civicrm_buildAsset', array($this, 'counter')); | |
27 | \Civi::dispatcher()->addListener('hook_civicrm_buildAsset', array($this, 'buildSquareTxt')); | |
28 | \Civi::dispatcher()->addListener('hook_civicrm_buildAsset', array($this, 'buildSquareJs')); | |
29 | } | |
30 | ||
31 | /** | |
32 | * @param \Civi\Core\Event\GenericHookEvent $e | |
33 | * @see \CRM_Utils_Hook::buildAsset() | |
34 | */ | |
35 | public function counter(GenericHookEvent $e) { | |
36 | $this->fired['hook_civicrm_buildAsset']++; | |
37 | } | |
38 | ||
39 | /** | |
40 | * @param \Civi\Core\Event\GenericHookEvent $e | |
41 | * @see \CRM_Utils_Hook::buildAsset() | |
42 | */ | |
43 | public function buildSquareTxt(GenericHookEvent $e) { | |
44 | if ($e->asset !== 'square.txt') { | |
45 | return; | |
46 | } | |
47 | $this->assertTrue(in_array($e->params['x'], array(11, 12))); | |
48 | ||
49 | $e->mimeType = 'text/plain'; | |
50 | $e->content = "Square: " . ($e->params['x'] * $e->params['x']); | |
51 | } | |
52 | ||
53 | /** | |
54 | * @param \Civi\Core\Event\GenericHookEvent $e | |
55 | * @see \CRM_Utils_Hook::buildAsset() | |
56 | */ | |
57 | public function buildSquareJs(GenericHookEvent $e) { | |
58 | if ($e->asset !== 'square.js') { | |
59 | return; | |
60 | } | |
61 | $this->assertTrue(in_array($e->params['x'], array(11, 12))); | |
62 | ||
63 | $e->mimeType = 'application/javascript'; | |
64 | $e->content = "var square=" . ($e->params['x'] * $e->params['x']) . ';'; | |
65 | } | |
66 | ||
67 | /** | |
68 | * Get a list of example assets to build/request. | |
69 | * @return array | |
70 | */ | |
71 | public function getExamples() { | |
affcc9d2 | 72 | $examples = []; |
87e3fe24 TO |
73 | |
74 | $examples[] = array( | |
75 | 0 => 'square.txt', | |
76 | 1 => array('x' => 11), | |
77 | 2 => 'text/plain', | |
78 | 3 => 'Square: 121', | |
79 | ); | |
80 | $examples[] = array( | |
81 | 0 => 'square.txt', | |
82 | 1 => array('x' => 12), | |
83 | 2 => 'text/plain', | |
84 | 3 => 'Square: 144', | |
85 | ); | |
86 | $examples[] = array( | |
87 | 0 => 'square.js', | |
88 | 1 => array('x' => 12), | |
89 | 2 => 'application/javascript', | |
90 | 3 => 'var square=144;', | |
91 | ); | |
92 | ||
93 | return $examples; | |
94 | } | |
95 | ||
96 | /** | |
97 | * @param string $asset | |
98 | * Ex: 'square.txt'. | |
99 | * @param array $params | |
100 | * Ex: [x=>12]. | |
101 | * @param string $expectedMimeType | |
102 | * Ex: 'text/plain'. | |
103 | * @param string $expectedContent | |
104 | * Ex: 'Square: 144'. | |
105 | * @dataProvider getExamples | |
106 | */ | |
107 | public function testRender($asset, $params, $expectedMimeType, $expectedContent) { | |
108 | $asset = \Civi::service('asset_builder')->render($asset, $params); | |
109 | $this->assertEquals(1, $this->fired['hook_civicrm_buildAsset']); | |
110 | $this->assertEquals($expectedMimeType, $asset['mimeType']); | |
111 | $this->assertEquals($expectedContent, $asset['content']); | |
112 | } | |
113 | ||
114 | /** | |
115 | * @param string $asset | |
116 | * Ex: 'square.txt'. | |
117 | * @param array $params | |
118 | * Ex: [x=>12]. | |
119 | * @param string $expectedMimeType | |
120 | * Ex: 'text/plain'. | |
121 | * @param string $expectedContent | |
122 | * Ex: 'Square: 144'. | |
123 | * @dataProvider getExamples | |
124 | */ | |
125 | public function testGetUrl_cached($asset, $params, $expectedMimeType, $expectedContent) { | |
126 | \Civi::service('asset_builder')->setCacheEnabled(TRUE); | |
127 | $url = \Civi::service('asset_builder')->getUrl($asset, $params); | |
128 | $this->assertEquals(1, $this->fired['hook_civicrm_buildAsset']); | |
129 | $this->assertRegExp(';^https?:.*dyn/square.[0-9a-f]+.(txt|js)$;', $url); | |
130 | $this->assertEquals($expectedContent, file_get_contents($url)); | |
131 | // Note: This actually relies on httpd to determine MIME type. | |
132 | // That could be ambiguous for javascript. | |
133 | $this->assertContains("Content-Type: $expectedMimeType", $http_response_header); | |
134 | $this->assertNotEmpty(preg_grep(';HTTP/1.1 200;', $http_response_header)); | |
135 | } | |
136 | ||
137 | /** | |
138 | * @param string $asset | |
139 | * Ex: 'square.txt'. | |
140 | * @param array $params | |
141 | * Ex: [x=>12]. | |
142 | * @param string $expectedMimeType | |
143 | * Ex: 'text/plain'. | |
144 | * @param string $expectedContent | |
145 | * Ex: 'Square: 144'. | |
146 | * @dataProvider getExamples | |
147 | */ | |
148 | public function testGetUrl_uncached($asset, $params, $expectedMimeType, $expectedContent) { | |
149 | \Civi::service('asset_builder')->setCacheEnabled(FALSE); | |
150 | $url = \Civi::service('asset_builder')->getUrl($asset, $params); | |
151 | $this->assertEquals(0, $this->fired['hook_civicrm_buildAsset']); | |
ee9ff517 TO |
152 | // Ex: Traditional URLs on D7 have "/". Traditional URLs on WP have "%2F". |
153 | $this->assertRegExp(';^https?:.*civicrm(/|%2F)asset(/|%2F)builder.*square.(txt|js);', $url); | |
87e3fe24 TO |
154 | |
155 | // Simulate a request. Our fake hook won't fire in a real request. | |
156 | parse_str(parse_url($url, PHP_URL_QUERY), $get); | |
157 | $asset = AssetBuilder::pageRender($get); | |
158 | $this->assertEquals($expectedMimeType, $asset['mimeType']); | |
159 | $this->assertEquals($expectedContent, $asset['content']); | |
160 | } | |
161 | ||
162 | public function testInvalid() { | |
163 | \Civi::service('asset_builder')->setCacheEnabled(FALSE); | |
164 | $url = \Civi::service('asset_builder')->getUrl('invalid.json'); | |
bfbd33be SL |
165 | try { |
166 | $guzzleClient = new \GuzzleHttp\Client(); | |
167 | $guzzleResponse = $guzzleClient->request('GET', $url, array('timeout' => 1)); | |
40a78f45 | 168 | $this->fail('Expecting ClientException... but it was not thrown!'); |
bfbd33be SL |
169 | } |
170 | catch (\GuzzleHttp\Exception\ClientException $e) { | |
171 | $this->assertNotEmpty(preg_match(';404;', $e->getMessage()), | |
172 | 'Expect to find HTTP 404. Found: ' . json_encode(preg_match(';^HTTP;', $e->getMessage()))); | |
40a78f45 | 173 | $this->assertEquals('Unrecognized asset name: invalid.json', $e->getResponse()->getBody()); |
bfbd33be | 174 | } |
87e3fe24 TO |
175 | } |
176 | ||
177 | } |