[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/mod/wiki/tests/ -> search_test.php (source)

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Wiki global search unit tests.
  19   *
  20   * @package     mod_wiki
  21   * @category    test
  22   * @copyright   2016 Eric Merrill {@link http://www.merrilldigital.com}
  23   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  global $CFG;
  29  require_once($CFG->dirroot . '/search/tests/fixtures/testable_core_search.php');
  30  
  31  /**
  32   * Provides the unit tests for wiki global search.
  33   *
  34   * @package     mod_wiki
  35   * @category    test
  36   * @copyright   2016 Eric Merrill {@link http://www.merrilldigital.com}
  37   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class mod_wiki_search_testcase extends advanced_testcase {
  40  
  41      /**
  42       * @var string Area id
  43       */
  44      protected $wikicollabpageareaid = null;
  45  
  46      public function setUp() {
  47          $this->resetAfterTest(true);
  48          $this->setAdminUser();
  49          set_config('enableglobalsearch', true);
  50  
  51          $this->wikicollabpageareaid = \core_search\manager::generate_areaid('mod_wiki', 'collaborative_page');
  52  
  53          // Set \core_search::instance to the mock_search_engine as we don't require the search engine to be working to test this.
  54          $search = testable_core_search::instance();
  55      }
  56  
  57      /**
  58       * Availability.
  59       *
  60       * @return void
  61       */
  62      public function test_search_enabled() {
  63          $searcharea = \core_search\manager::get_search_area($this->wikicollabpageareaid);
  64          list($componentname, $varname) = $searcharea->get_config_var_name();
  65  
  66          // Enabled by default once global search is enabled.
  67          $this->assertTrue($searcharea->is_enabled());
  68  
  69          set_config($varname . '_enabled', 0, $componentname);
  70          $this->assertFalse($searcharea->is_enabled());
  71  
  72          set_config($varname . '_enabled', 1, $componentname);
  73          $this->assertTrue($searcharea->is_enabled());
  74      }
  75  
  76      /**
  77       * Indexing collaborative page contents.
  78       *
  79       * @return void
  80       */
  81      public function test_collaborative_page_indexing() {
  82          global $DB;
  83  
  84          // Returns the instance as long as the area is supported.
  85          $searcharea = \core_search\manager::get_search_area($this->wikicollabpageareaid);
  86          $this->assertInstanceOf('\mod_wiki\search\collaborative_page', $searcharea);
  87  
  88          $wikigenerator = $this->getDataGenerator()->get_plugin_generator('mod_wiki');
  89          $course1 = self::getDataGenerator()->create_course();
  90  
  91          $collabwiki = $this->getDataGenerator()->create_module('wiki', array('course' => $course1->id));
  92          $cpage1 = $wikigenerator->create_first_page($collabwiki);
  93          $cpage2 = $wikigenerator->create_content($collabwiki);
  94          $cpage3 = $wikigenerator->create_content($collabwiki);
  95  
  96          $indwiki = $this->getDataGenerator()->create_module('wiki', array('course' => $course1->id, 'wikimode' => 'individual'));
  97          $ipage1 = $wikigenerator->create_first_page($indwiki);
  98          $ipage2 = $wikigenerator->create_content($indwiki);
  99          $ipage3 = $wikigenerator->create_content($indwiki);
 100  
 101          // All records.
 102          $recordset = $searcharea->get_recordset_by_timestamp(0);
 103          $this->assertTrue($recordset->valid());
 104          $nrecords = 0;
 105          foreach ($recordset as $record) {
 106              $this->assertInstanceOf('stdClass', $record);
 107              $doc = $searcharea->get_document($record);
 108              $this->assertInstanceOf('\core_search\document', $doc);
 109  
 110              // Static caches are working.
 111              $dbreads = $DB->perf_get_reads();
 112              $doc = $searcharea->get_document($record);
 113              $this->assertEquals($dbreads, $DB->perf_get_reads());
 114              $this->assertInstanceOf('\core_search\document', $doc);
 115              $nrecords++;
 116          }
 117          // If there would be an error/failure in the foreach above the recordset would be closed on shutdown.
 118          $recordset->close();
 119  
 120          // We expect 3 (not 6) pages.
 121          $this->assertEquals(3, $nrecords);
 122  
 123          // The +2 is to prevent race conditions.
 124          $recordset = $searcharea->get_recordset_by_timestamp(time() + 2);
 125  
 126          // No new records.
 127          $this->assertFalse($recordset->valid());
 128          $recordset->close();
 129      }
 130  
 131      /**
 132       * Check collaborative_page check access.
 133       *
 134       * @return void
 135       */
 136      public function test_collaborative_page_check_access() {
 137          global $DB;
 138  
 139          // Returns the instance as long as the area is supported.
 140          $searcharea = \core_search\manager::get_search_area($this->wikicollabpageareaid);
 141          $this->assertInstanceOf('\mod_wiki\search\collaborative_page', $searcharea);
 142  
 143          $user1 = self::getDataGenerator()->create_user();
 144          $course1 = self::getDataGenerator()->create_course();
 145          $this->getDataGenerator()->enrol_user($user1->id, $course1->id, 'student');
 146  
 147          $wikigenerator = $this->getDataGenerator()->get_plugin_generator('mod_wiki');
 148  
 149          $collabwiki = $this->getDataGenerator()->create_module('wiki', array('course' => $course1->id));
 150          $cpage1 = $wikigenerator->create_first_page($collabwiki);
 151  
 152          $this->setAdminUser();
 153          $this->assertEquals(\core_search\manager::ACCESS_GRANTED, $searcharea->check_access($cpage1->id));
 154  
 155          $this->setUser($user1);
 156          $this->assertEquals(\core_search\manager::ACCESS_GRANTED, $searcharea->check_access($cpage1->id));
 157  
 158          $this->assertEquals(\core_search\manager::ACCESS_DELETED, $searcharea->check_access($cpage1->id + 10));
 159      }
 160  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1