MDL-55390 search: is_search_area unit tests
authorDavid Monllao <davidm@moodle.com>
Tue, 9 Aug 2016 07:51:09 +0000 (15:51 +0800)
committerDavid Monllao <davidm@moodle.com>
Tue, 9 Aug 2016 07:51:30 +0000 (15:51 +0800)
Added extra checking for search manager get_search_area.

search/classes/manager.php
search/tests/fixtures/testable_core_search.php
search/tests/manager_test.php

index d68dc04..662a264 100644 (file)
@@ -207,7 +207,8 @@ class manager {
         }
 
         $classname = static::get_area_classname($areaid);
-        if (class_exists($classname)) {
+
+        if (class_exists($classname) && static::is_search_area($classname)) {
             return new $classname();
         }
 
@@ -730,12 +731,12 @@ class manager {
     /**
      * Checks whether a classname is of an actual search area.
      *
-     * @param string $searchareaname
+     * @param string $classname
      * @return bool
      */
-    protected static function is_search_area($searchareaname) {
-        if (is_subclass_of($searchareaname, 'core_search\base')) {
-            return (new \ReflectionClass($searchareaname))->isInstantiable();
+    protected static function is_search_area($classname) {
+        if (is_subclass_of($classname, 'core_search\base')) {
+            return (new \ReflectionClass($classname))->isInstantiable();
         }
 
         return false;
index 9b3b5ea..9414531 100644 (file)
@@ -97,4 +97,15 @@ class testable_core_search extends \core_search\manager {
         self::get_search_areas_list(false);
         self::get_search_areas_list(true);
     }
+
+    /**
+     * Changes visibility.
+     *
+     * @param string $classname
+     * @return bool
+     */
+    public static function is_search_area($classname) {
+        return parent::is_search_area($classname);
+    }
+
 }
index 879d838..a0b60b6 100644 (file)
@@ -253,4 +253,19 @@ class search_manager_testcase extends advanced_testcase {
         $this->assertEquals($allcontexts, $contexts[$this->forumpostareaid]);
         $this->assertEquals(array($course1ctx->id => $course1ctx->id), $contexts[$this->mycoursesareaid]);
     }
+
+    /**
+     * test_is_search_area
+     *
+     * @return void
+     */
+    public function test_is_search_area() {
+
+        $this->assertFalse(testable_core_search::is_search_area('\asd\asd'));
+        $this->assertFalse(testable_core_search::is_search_area('\mod_forum\search\posta'));
+        $this->assertFalse(testable_core_search::is_search_area('\core_search\base_mod'));
+        $this->assertTrue(testable_core_search::is_search_area('\mod_forum\search\post'));
+        $this->assertTrue(testable_core_search::is_search_area('\\mod_forum\\search\\post'));
+        $this->assertTrue(testable_core_search::is_search_area('mod_forum\\search\\post'));
+    }
 }