5 use Civi\Core\AssetBuilder
;
6 use Civi\Core\Event\GenericHookEvent
;
9 * Class AssetBuilderTest
13 class AssetBuilderTest
extends \CiviEndToEndTestCase
{
20 protected function setUp() {
23 \Civi
::service('asset_builder')->clear();
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'));
32 * @param \Civi\Core\Event\GenericHookEvent $e
33 * @see \CRM_Utils_Hook::buildAsset()
35 public function counter(GenericHookEvent
$e) {
36 $this->fired
['hook_civicrm_buildAsset']++
;
40 * @param \Civi\Core\Event\GenericHookEvent $e
41 * @see \CRM_Utils_Hook::buildAsset()
43 public function buildSquareTxt(GenericHookEvent
$e) {
44 if ($e->asset
!== 'square.txt') {
47 $this->assertTrue(in_array($e->params
['x'], array(11, 12)));
49 $e->mimeType
= 'text/plain';
50 $e->content
= "Square: " . ($e->params
['x'] * $e->params
['x']);
54 * @param \Civi\Core\Event\GenericHookEvent $e
55 * @see \CRM_Utils_Hook::buildAsset()
57 public function buildSquareJs(GenericHookEvent
$e) {
58 if ($e->asset
!== 'square.js') {
61 $this->assertTrue(in_array($e->params
['x'], array(11, 12)));
63 $e->mimeType
= 'application/javascript';
64 $e->content
= "var square=" . ($e->params
['x'] * $e->params
['x']) . ';';
68 * Get a list of example assets to build/request.
71 public function getExamples() {
76 1 => array('x' => 11),
82 1 => array('x' => 12),
88 1 => array('x' => 12),
89 2 => 'application/javascript',
90 3 => 'var square=144;',
97 * @param string $asset
99 * @param array $params
101 * @param string $expectedMimeType
103 * @param string $expectedContent
105 * @dataProvider getExamples
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']);
115 * @param string $asset
117 * @param array $params
119 * @param string $expectedMimeType
121 * @param string $expectedContent
123 * @dataProvider getExamples
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));
138 * @param string $asset
140 * @param array $params
142 * @param string $expectedMimeType
144 * @param string $expectedContent
146 * @dataProvider getExamples
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']);
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);
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']);
162 public function testInvalid() {
163 \Civi
::service('asset_builder')->setCacheEnabled(FALSE);
164 $url = \Civi
::service('asset_builder')->getUrl('invalid.json');
166 $guzzleClient = new \GuzzleHttp\
Client();
167 $guzzleResponse = $guzzleClient->request('GET', $url, array('timeout' => 1));
168 $this->fail('Expecting ClientException... but it was not thrown!');
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())));
173 $this->assertEquals('Unrecognized asset name: invalid.json', $e->getResponse()->getBody());