basedir, $this->container, $this->mapper) = $this->_createMapper(); $cache = new CRM_Utils_Cache_Arraycache(array()); $this->res = new CRM_Core_Resources($this->mapper, $cache, NULL); $this->res->setCacheCode('resTest'); CRM_Core_Resources::singleton($this->res); // Templates injected into regions should normally be file names, but for unit-testing it's handy to use "string:" notation require_once 'CRM/Core/Smarty/resources/String.php'; civicrm_smarty_register_string_resource(); } public function testAddScriptFile() { $this->res ->addScriptFile('com.example.ext', 'foo%20bar.js', 0, 'testAddScriptFile') ->addScriptFile('com.example.ext', 'foo%20bar.js', 0, 'testAddScriptFile')// extra ->addScriptFile('civicrm', 'foo%20bar.js', 0, 'testAddScriptFile'); $smarty = CRM_Core_Smarty::singleton(); $actual = $smarty->fetch('string:{crmRegion name=testAddScriptFile}{/crmRegion}'); $expected = "" // stable ordering: alphabetical by (snippet.weight,snippet.name) . "\n" . "\n"; $this->assertEquals($expected, $actual); } /** * When adding a script file, any ts() expressions should be translated and added to the 'strings' * * FIXME: This can't work because the tests run in English and CRM_Core_Resources optimizes * away the English data from $settings['strings'] * public function testAddScriptFile_strings() { * file_put_contents($this->mapper->keyToBasePath('com.example.ext') . '/hello.js', 'alert(ts("Hello world"));'); * $this->res->addScriptFile('com.example.ext', 'hello.js', 0, 'testAddScriptFile_strings'); * $settings = $this->res->getSettings(); * $expected = array('Hello world'); * $this->assertEquals($expected, $settings['strings']); * } */ /** * Ensure that adding a script URL creates expected markup. */ public function testAddScriptURL() { $this->res ->addScriptUrl('/whiz/foo%20bar.js', 0, 'testAddScriptURL') ->addScriptUrl('/whiz/foo%20bar.js', 0, 'testAddScriptURL')// extra ->addScriptUrl('/whizbang/foo%20bar.js', 0, 'testAddScriptURL'); $smarty = CRM_Core_Smarty::singleton(); $actual = $smarty->fetch('string:{crmRegion name=testAddScriptURL}{/crmRegion}'); $expected = "" // stable ordering: alphabetical by (snippet.weight,snippet.name) . "\n" . "\n"; $this->assertEquals($expected, $actual); } public function testAddScript() { $this->res ->addScript('alert("hi");', 0, 'testAddScript') ->addScript('alert("there");', 0, 'testAddScript'); $smarty = CRM_Core_Smarty::singleton(); $actual = $smarty->fetch('string:{crmRegion name=testAddScript}{/crmRegion}'); $expected = "" . "\n" . "\n"; $this->assertEquals($expected, $actual); } public function testAddVars() { $this->res ->addVars('food', array('fruit' => array('mine' => 'apple', 'ours' => 'banana'))) ->addVars('food', array('fruit' => array('mine' => 'new apple', 'yours' => 'orange'))); $this->assertTreeEquals( array( 'vars' => array( 'food' => array( 'fruit' => array( 'yours' => 'orange', 'mine' => 'new apple', 'ours' => 'banana', ), ), ), ), $this->res->getSettings() ); } public function testAddSetting() { $this->res ->addSetting(array('fruit' => array('mine' => 'apple'))) ->addSetting(array('fruit' => array('yours' => 'orange'))); $this->assertTreeEquals( array('fruit' => array('yours' => 'orange', 'mine' => 'apple')), $this->res->getSettings() ); $actual = $this->res->renderSetting(); $expected = json_encode(array('fruit' => array('yours' => 'orange', 'mine' => 'apple'))); $this->assertTrue(strpos($actual, $expected) !== FALSE); } public function testAddSettingFactory() { $this->res->addSettingsFactory(function () { return array('fruit' => array('yours' => 'orange')); }); $this->res->addSettingsFactory(function () { return array('fruit' => array('mine' => 'apple')); }); $actual = $this->res->getSettings(); $expected = array('fruit' => array('yours' => 'orange', 'mine' => 'apple')); $this->assertTreeEquals($expected, $actual); } public function testAddSettingAndSettingFactory() { $this->res->addSetting(array('fruit' => array('mine' => 'apple'))); $muckableValue = array('fruit' => array('yours' => 'orange', 'theirs' => 'apricot')); $this->res->addSettingsFactory(function () use (&$muckableValue) { return $muckableValue; }); $actual = $this->res->getSettings(); $expected = array('fruit' => array('mine' => 'apple', 'yours' => 'orange', 'theirs' => 'apricot')); $this->assertTreeEquals($expected, $actual); // note: the setting is not fixed based on what the factory returns when registered; it's based // on what the factory returns when getSettings is called $muckableValue = array('fruit' => array('yours' => 'banana')); $actual = $this->res->getSettings(); $expected = array('fruit' => array('mine' => 'apple', 'yours' => 'banana')); $this->assertTreeEquals($expected, $actual); } public function testCrmJS() { $smarty = CRM_Core_Smarty::singleton(); $actual = $smarty->fetch('string:{crmScript ext=com.example.ext file=foo%20bar.js region=testCrmJS}'); $this->assertEquals('', $actual); $actual = $smarty->fetch('string:{crmScript url=/whiz/foo%20bar.js region=testCrmJS weight=1}'); $this->assertEquals('', $actual); $actual = $smarty->fetch('string:{crmRegion name=testCrmJS}{/crmRegion}'); $expected = "" // stable ordering: alphabetical by (snippet.weight,snippet.name) . "\n" . "\n"; $this->assertEquals($expected, $actual); } public function testAddStyleFile() { $this->res ->addStyleFile('com.example.ext', 'foo%20bar.css', 0, 'testAddStyleFile') ->addStyleFile('com.example.ext', 'foo%20bar.css', 0, 'testAddStyleFile')// extra ->addStyleFile('civicrm', 'foo%20bar.css', 0, 'testAddStyleFile'); $smarty = CRM_Core_Smarty::singleton(); $actual = $smarty->fetch('string:{crmRegion name=testAddStyleFile}{/crmRegion}'); $expected = "" // stable ordering: alphabetical by (snippet.weight,snippet.name) . "\n" . "\n"; $this->assertEquals($expected, $actual); } public function testAddStyleURL() { $this->res ->addStyleUrl('/whiz/foo%20bar.css', 0, 'testAddStyleURL') ->addStyleUrl('/whiz/foo%20bar.css', 0, 'testAddStyleURL')// extra ->addStyleUrl('/whizbang/foo%20bar.css', 0, 'testAddStyleURL'); $smarty = CRM_Core_Smarty::singleton(); $actual = $smarty->fetch('string:{crmRegion name=testAddStyleURL}{/crmRegion}'); $expected = "" // stable ordering: alphabetical by (snippet.weight,snippet.name) . "\n" . "\n"; $this->assertEquals($expected, $actual); } public function testAddStyle() { $this->res ->addStyle('body { background: black; }', 0, 'testAddStyle') ->addStyle('body { text-color: black; }', 0, 'testAddStyle'); $smarty = CRM_Core_Smarty::singleton(); $actual = $smarty->fetch('string:{crmRegion name=testAddStyle}{/crmRegion}'); $expected = "" . "\n" . "\n"; $this->assertEquals($expected, $actual); } public function testCrmCSS() { $smarty = CRM_Core_Smarty::singleton(); $actual = $smarty->fetch('string:{crmStyle ext=com.example.ext file=foo%20bar.css region=testCrmCSS}'); $this->assertEquals('', $actual); $actual = $smarty->fetch('string:{crmStyle url=/whiz/foo%20bar.css region=testCrmCSS weight=1}'); $this->assertEquals('', $actual); $actual = $smarty->fetch('string:{crmRegion name=testCrmCSS}{/crmRegion}'); $expected = "" // stable ordering: alphabetical by (snippet.weight,snippet.name) . "\n" . "\n"; $this->assertEquals($expected, $actual); } public function testGetURL() { $this->assertEquals( 'http://core-app/dir/file%20name.txt', $this->res->getURL('civicrm', 'dir/file%20name.txt') ); $this->assertEquals( 'http://ext-dir/com.example.ext/dir/file%20name.txt', $this->res->getURL('com.example.ext', 'dir/file%20name.txt') ); $this->assertEquals( 'http://core-app/', $this->res->getURL('civicrm') ); $this->assertEquals( 'http://ext-dir/com.example.ext/', $this->res->getURL('com.example.ext') ); } public function testCrmResURL() { $smarty = CRM_Core_Smarty::singleton(); $actual = $smarty->fetch('string:{crmResURL ext=com.example.ext file=foo%20bar.png}'); $this->assertEquals('http://ext-dir/com.example.ext/foo%20bar.png', $actual); $actual = $smarty->fetch('string:{crmResURL ext=com.example.ext file=foo%20bar.png addCacheCode=1}'); $this->assertEquals('http://ext-dir/com.example.ext/foo%20bar.png?r=resTest', $actual); $actual = $smarty->fetch('string:{crmResURL ext=com.example.ext}'); $this->assertEquals('http://ext-dir/com.example.ext/', $actual); } public function testGlob() { $this->assertEquals( array('info.xml'), $this->res->glob('com.example.ext', 'info.xml') ); $this->assertEquals( array('js/example.js'), $this->res->glob('com.example.ext', 'js/*.js') ); $this->assertEquals( array('js/example.js'), $this->res->glob('com.example.ext', array('js/*.js')) ); } /** * @param CRM_Utils_Cache_Interface $cache * @param string $cacheKey * * @return array * [string $basedir, CRM_Extension_Container_Interface, CRM_Extension_Mapper] */ public function _createMapper(CRM_Utils_Cache_Interface $cache = NULL, $cacheKey = NULL) { $basedir = rtrim($this->createTempDir('ext-'), '/'); mkdir("$basedir/com.example.ext"); mkdir("$basedir/com.example.ext/js"); file_put_contents("$basedir/com.example.ext/info.xml", "oddball"); file_put_contents("$basedir/com.example.ext/js/example.js", "alert('Boo!');"); // not needed for now // file_put_contents("$basedir/weird/bar/oddball.php", "