Merge remote-tracking branch 'upstream/4.6' into 4.6-master-2015-07-06-14-41-49
[civicrm-core.git] / tests / phpunit / CRM / Core / ResourcesTest.php
index 177302cd0dae778ab48bd5a5e3b9a66de2ef2bd6..0ff2354700f27548f7c61a6fbffd430ea093ae87 100644 (file)
@@ -3,7 +3,7 @@
  +--------------------------------------------------------------------+
  | CiviCRM version 4.6                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2014                                |
+ | Copyright CiviCRM LLC (c) 2004-2015                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
@@ -23,7 +23,7 @@
  | GNU Affero General Public License or the licensing of CiviCRM,     |
  | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
  +--------------------------------------------------------------------+
-*/
+ */
 
 require_once 'CiviTest/CiviUnitTestCase.php';
 
@@ -59,7 +59,7 @@ class CRM_Core_ResourcesTest extends CiviUnitTestCase {
   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('com.example.ext', 'foo%20bar.js', 0, 'testAddScriptFile')// extra
       ->addScriptFile('civicrm', 'foo%20bar.js', 0, 'testAddScriptFile');
 
     $smarty = CRM_Core_Smarty::singleton();
@@ -75,19 +75,22 @@ class CRM_Core_ResourcesTest extends CiviUnitTestCase {
    *
    * 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']);
-  }
+   * 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('/whiz/foo%20bar.js', 0, 'testAddScriptURL')// extra
       ->addScriptUrl('/whizbang/foo%20bar.js', 0, 'testAddScriptURL');
 
     $smarty = CRM_Core_Smarty::singleton();
@@ -116,7 +119,17 @@ class CRM_Core_ResourcesTest extends CiviUnitTestCase {
       ->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')))),
+      array(
+        'vars' => array(
+          'food' => array(
+            'fruit' => array(
+              'yours' => 'orange',
+              'mine' => 'new apple',
+              'ours' => 'banana',
+            ),
+          ),
+        ),
+      ),
       $this->res->getSettings()
     );
   }
@@ -135,10 +148,10 @@ class CRM_Core_ResourcesTest extends CiviUnitTestCase {
   }
 
   public function testAddSettingFactory() {
-    $this->res->addSettingsFactory(function()  {
+    $this->res->addSettingsFactory(function () {
       return array('fruit' => array('yours' => 'orange'));
     });
-    $this->res->addSettingsFactory(function()  {
+    $this->res->addSettingsFactory(function () {
       return array('fruit' => array('mine' => 'apple'));
     });
 
@@ -151,7 +164,7 @@ class CRM_Core_ResourcesTest extends CiviUnitTestCase {
     $this->res->addSetting(array('fruit' => array('mine' => 'apple')));
 
     $muckableValue = array('fruit' => array('yours' => 'orange', 'theirs' => 'apricot'));
-    $this->res->addSettingsFactory(function() use (&$muckableValue) {
+    $this->res->addSettingsFactory(function () use (&$muckableValue) {
       return $muckableValue;
     });
     $actual = $this->res->getSettings();
@@ -185,7 +198,7 @@ class CRM_Core_ResourcesTest extends CiviUnitTestCase {
   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('com.example.ext', 'foo%20bar.css', 0, 'testAddStyleFile')// extra
       ->addStyleFile('civicrm', 'foo%20bar.css', 0, 'testAddStyleFile');
 
     $smarty = CRM_Core_Smarty::singleton();
@@ -199,7 +212,7 @@ class CRM_Core_ResourcesTest extends CiviUnitTestCase {
   public function testAddStyleURL() {
     $this->res
       ->addStyleUrl('/whiz/foo%20bar.css', 0, 'testAddStyleURL')
-      ->addStyleUrl('/whiz/foo%20bar.css', 0, 'testAddStyleURL') // extra
+      ->addStyleUrl('/whiz/foo%20bar.css', 0, 'testAddStyleURL')// extra
       ->addStyleUrl('/whizbang/foo%20bar.css', 0, 'testAddStyleURL');
 
     $smarty = CRM_Core_Smarty::singleton();
@@ -271,17 +284,34 @@ class CRM_Core_ResourcesTest extends CiviUnitTestCase {
     $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 null $cacheKey
+   * @param string $cacheKey
    *
-   * @internal param string $appendPathGarbage
-   * @return array(string $basedir, CRM_Extension_Container_Interface, CRM_Extension_Mapper)
+   * @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", "<extension key='com.example.ext' type='report'><file>oddball</file></extension>");
+    file_put_contents("$basedir/com.example.ext/js/example.js", "alert('Boo!');");
     // not needed for now // file_put_contents("$basedir/weird/bar/oddball.php", "<?php\n");
     $c = new CRM_Extension_Container_Basic($basedir, 'http://ext-dir', $cache, $cacheKey);
     $mapper = new CRM_Extension_Mapper($c, NULL, NULL, '/pathto/civicrm', 'http://core-app');