Move to using CRM_Utils_Rule and CRM_Utils_Type::validate as per Sean's comments
authorSeamus Lee <seamuslee001@gmail.com>
Tue, 19 Sep 2017 22:45:33 +0000 (08:45 +1000)
committerSeamus Lee <seamuslee001@gmail.com>
Tue, 19 Sep 2017 22:47:51 +0000 (08:47 +1000)
CRM/Admin/Form/Extensions.php
CRM/Utils/Rule.php
CRM/Utils/Type.php
tests/phpunit/CRM/Admin/Form/ExtensionTest.php [deleted file]
tests/phpunit/CRM/Utils/RuleTest.php

index ffb07c1dc581fea8d682a6020977a2cb21796dfe..7f273cb1632f576270c669df6304a3b405d7bea4 100644 (file)
  */
 class CRM_Admin_Form_Extensions extends CRM_Admin_Form {
 
-  /**
-   * @param string $key Extension Key to check
-   * @return bool
-   */
-  public static function checkExtesnionKeyIsValid($key = NULL) {
-    if (!empty($key) && !preg_match('/^[0-9a-zA-Z._-]+$/', $key)) {
-      return FALSE;
-    }
-    return TRUE;
-  }
-
   /**
    * Form pre-processing.
    */
@@ -56,7 +45,7 @@ class CRM_Admin_Form_Extensions extends CRM_Admin_Form {
     $this->_key = CRM_Utils_Request::retrieve('key', 'String',
       $this, FALSE, 0
     );
-    if (!self::checkExtesnionKeyIsValid($this->_key)) {
+    if (!CRM_Utils_Type::validate($this->_key, 'ExtensionKey')) {
       throw new CRM_Core_Exception('Extension Key does not match expected standard');
     }
     $session = CRM_Core_Session::singleton();
index 897aa5dbbd306cdc8f9e16cb087b8cb62411f01d..c9a92fa96791f322945b35c3a2e7c7f8ddc40410 100644 (file)
@@ -911,4 +911,15 @@ class CRM_Utils_Rule {
     }
   }
 
+  /**
+   * @param string $key Extension Key to check
+   * @return bool
+   */
+  public static function checkExtesnionKeyIsValid($key = NULL) {
+    if (!empty($key) && !preg_match('/^[0-9a-zA-Z._-]+$/', $key)) {
+      return FALSE;
+    }
+    return TRUE;
+  }
+
 }
index 43b920f8be4bf595e42e4014d01f3e774074d87f..fbe1c934e7922fbc7091c76422031754e3d2974d 100644 (file)
@@ -466,6 +466,12 @@ class CRM_Utils_Type {
         }
         break;
 
+      case 'ExtensionKey':
+        if (CRM_Utils_Rule::checkExtesnionKeyIsValid($data)) {
+          return $data;
+        }
+        break;
+
       default:
         CRM_Core_Error::fatal("Cannot recognize $type for $data");
         break;
diff --git a/tests/phpunit/CRM/Admin/Form/ExtensionTest.php b/tests/phpunit/CRM/Admin/Form/ExtensionTest.php
deleted file mode 100644 (file)
index 469c3c4..0000000
+++ /dev/null
@@ -1,33 +0,0 @@
-<?php
-
-/**
- * Class CRM_Admin_Form_ExtensionTest
- * @group headless
- */
-class CRM_Admin_Form_ExtensionTest extends CiviUnitTestCase {
-
-  /**
-   * @return array
-   */
-  public function extenionKeyTests() {
-    $keys = array();
-    $keys[] = array('org.civicrm.multisite');
-    $keys[] = array('au.org.contribute2016');
-    $keys[] = array('%3Csvg%20onload=alert(0)%3E');
-    return $keys;
-  }
-
-  /**
-   * @param $key
-   * @dataProvider extenionKeyTests
-   */
-  public function testExtenionKeyValid($key) {
-    if ($key == '%3Csvg%20onload=alert(0)%3E') {
-      $this->assertFalse(CRM_Admin_Form_Extensions::checkExtesnionKeyIsValid($key));
-    }
-    else {
-      $this->assertTrue(CRM_Admin_Form_Extensions::checkExtesnionKeyIsValid($key));
-    }
-  }
-
-}
index b6abcfa22f6e47f7f9baac15259c869bd176816f..9db05dbaa356cceac7aa59e095eef6be446868e4 100644 (file)
@@ -112,4 +112,24 @@ class CRM_Utils_RuleTest extends CiviUnitTestCase {
     );
   }
 
+  /**
+   * @return array
+   */
+  public function extenionKeyTests() {
+    $keys = array();
+    $keys[] = array('org.civicrm.multisite', TRUE);
+    $keys[] = array('au.org.contribute2016', TRUE);
+    $keys[] = array('%3Csvg%20onload=alert(0)%3E', FALSE);
+    return $keys;
+  }
+
+  /**
+   * @param $key
+   * @param $expectedResult
+   * @dataProvider extenionKeyTests
+   */
+  public function testExtenionKeyValid($key, $expectedResult) {
+    $this->assertEquals($expectedResult, CRM_Utils_Rule::checkExtesnionKeyIsValid($key));
+  }
+
 }