CRM-19827 Avoid unnecessary core_resources processing during ajax calls (#9623)
authorcolemanw <coleman@civicrm.org>
Wed, 11 Jan 2017 22:41:04 +0000 (17:41 -0500)
committerEileen McNaughton <eileen@mcnaughty.com>
Wed, 11 Jan 2017 22:41:04 +0000 (11:41 +1300)
* CRM-19827 Avoid unnecessary core_resources processing during ajax calls

* CRM-19827 - Add unit test for isAjaxMode

* CRM-19827 - Test fix

CRM/Core/Controller.php
CRM/Core/Resources.php
CRM/Utils/System.php
tests/phpunit/CRM/Core/ResourcesTest.php

index e6357605def5b1e68b2fe0226a1918806722b604..7e9d64f0ea290ba321d2c51c86db26294b7a7aa7 100644 (file)
@@ -457,8 +457,8 @@ class CRM_Core_Controller extends HTML_QuickForm_Controller {
       if ($options) {
         $$stateName->setOptions($options);
       }
-      if (property_exists($$stateName, 'urlPath') && isset($_GET[CRM_Core_Config::singleton()->userFrameworkURLVar])) {
-        $$stateName->urlPath = explode('/', $_GET[CRM_Core_Config::singleton()->userFrameworkURLVar]);
+      if (property_exists($$stateName, 'urlPath')) {
+        $$stateName->urlPath = explode('/', (string) CRM_Utils_System::getUrlPath());
       }
       $this->addPage($$stateName);
       $this->addAction($stateName, new HTML_QuickForm_Action_Direct());
index 1ca111ec44675df18a8ef24a689bea51292480da..52815a69f74cb558db1ef6812a511e2480ac2700 100644 (file)
@@ -781,11 +781,15 @@ class CRM_Core_Resources {
    *   is this page request an ajax snippet?
    */
   public static function isAjaxMode() {
-    return in_array(CRM_Utils_Array::value('snippet', $_REQUEST), array(
+    if (in_array(CRM_Utils_Array::value('snippet', $_REQUEST), array(
         CRM_Core_Smarty::PRINT_SNIPPET,
         CRM_Core_Smarty::PRINT_NOFORM,
         CRM_Core_Smarty::PRINT_JSON,
-      ));
+      ))
+    ) {
+      return TRUE;
+    }
+    return strpos(CRM_Utils_System::getUrlPath(), 'civicrm/ajax') === 0;
   }
 
   /**
index dd65eb9fb09dd990cfe0a19db3202b6d9f33cca6..94c28b4dfc8b173ba99f4b9c0e6b911fc7b37bba 100644 (file)
@@ -300,6 +300,18 @@ class CRM_Utils_System {
     return $url;
   }
 
+  /**
+   * Path of the current page e.g. 'civicrm/contact/view'
+   *
+   * @return string|null
+   */
+  public static function getUrlPath() {
+    if (isset($_GET[CRM_Core_Config::singleton()->userFrameworkURLVar])) {
+      return $_GET[CRM_Core_Config::singleton()->userFrameworkURLVar];
+    }
+    return NULL;
+  }
+
   /**
    * Get href.
    *
index abae4e87e22a134fa42191ecf0dd5dc7262b5b11..ea0d74ca010025c3a027ac0412f068bd386458ad 100644 (file)
@@ -41,6 +41,9 @@ class CRM_Core_ResourcesTest extends CiviUnitTestCase {
    */
   protected $mapper;
 
+  protected $originalRequest;
+  protected $originalGet;
+
   public function setUp() {
     parent::setUp();
 
@@ -53,6 +56,17 @@ class CRM_Core_ResourcesTest extends CiviUnitTestCase {
     // 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();
+
+    $this->originalRequest = $_REQUEST;
+    $this->originalGet = $_GET;
+  }
+
+  /**
+   * Restore globals so this test doesn't interfere with others.
+   */
+  public function tearDown() {
+    $_REQUEST = $this->originalRequest;
+    $_GET = $this->originalGet;
   }
 
   public function testAddScriptFile() {
@@ -298,6 +312,23 @@ class CRM_Core_ResourcesTest extends CiviUnitTestCase {
     );
   }
 
+  /**
+   * @dataProvider ajaxModeData
+   */
+  public function testIsAjaxMode($query, $result) {
+    $_REQUEST = $_GET = $query;
+    $this->assertEquals($result, CRM_Core_Resources::isAjaxMode());
+  }
+
+  public function ajaxModeData() {
+    return array(
+      array(array('q' => 'civicrm/ajax/foo'), TRUE),
+      array(array('q' => 'civicrm/test/page'), FALSE),
+      array(array('q' => 'civicrm/test/page', 'snippet' => 'json'), TRUE),
+      array(array('q' => 'civicrm/test/page', 'snippet' => 'foo'), FALSE),
+    );
+  }
+
   /**
    * @param CRM_Utils_Cache_Interface $cache
    * @param string $cacheKey