'Resources',
'description' => 'Tests for linking to resource files',
'group' => 'Core',
);
}
/**
* @var CRM_Core_Resources
*/
protected $res;
/**
* @var CRM_Extension_Mapper
*/
protected $mapper;
function setUp() {
parent::setUp();
list ($this->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( );
}
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']
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']);
}
*/
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);
}
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);
}
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);
}
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);
}
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);
}
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);
}
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);
}
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);
}
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);
}
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);
}
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')
);
}
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);
}
/**
* @param CRM_Utils_Cache_Interface $cache
* @param null $cacheKey
* @param string $appendPathGarbage
* @return array(string $basedir, CRM_Extension_Container_Interface, CRM_Extension_Mapper)
*/
function _createMapper(CRM_Utils_Cache_Interface $cache = NULL, $cacheKey = NULL) {
$basedir = rtrim($this->createTempDir('ext-'), '/');
mkdir("$basedir/com.example.ext");
file_put_contents("$basedir/com.example.ext/info.xml", "oddball");
// not needed for now // file_put_contents("$basedir/weird/bar/oddball.php", "