Proyectos de Subversion Moodle

Rev

Rev 11 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 11 Rev 1441
Línea 27... Línea 27...
27
 * @package     core_search
27
 * @package     core_search
28
 * @category    phpunit
28
 * @category    phpunit
29
 * @copyright   2015 David Monllao {@link http://www.davidmonllao.com}
29
 * @copyright   2015 David Monllao {@link http://www.davidmonllao.com}
30
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31
 */
31
 */
32
class manager_test extends \advanced_testcase {
32
final class manager_test extends \advanced_testcase {
Línea 33... Línea 33...
33
 
33
 
34
    /**
34
    /**
35
     * Forum area id.
35
     * Forum area id.
36
     *
36
     *
Línea 45... Línea 45...
45
     * @var string
45
     * @var string
46
     */
46
     */
47
    protected $coursesareaid = null;
47
    protected $coursesareaid = null;
Línea 48... Línea 48...
48
 
48
 
-
 
49
    public function setUp(): void {
49
    public function setUp(): void {
50
        parent::setUp();
50
        $this->forumpostareaid = \core_search\manager::generate_areaid('mod_forum', 'post');
51
        $this->forumpostareaid = \core_search\manager::generate_areaid('mod_forum', 'post');
51
        $this->coursesareaid = \core_search\manager::generate_areaid('core_course', 'course');
52
        $this->coursesareaid = \core_search\manager::generate_areaid('core_course', 'course');
Línea 52... Línea 53...
52
    }
53
    }
Línea 104... Línea 105...
104
    /**
105
    /**
105
     * Data for the test_course_search_url test.
106
     * Data for the test_course_search_url test.
106
     *
107
     *
107
     * @return array[]
108
     * @return array[]
108
     */
109
     */
109
    public function data_course_search_url(): array {
110
    public static function data_course_search_url(): array {
110
        return [
111
        return [
111
            'defaults' => [null, null, null, '/course/search.php'],
112
            'defaults' => [null, null, null, '/course/search.php'],
112
            'enabled' => [true, true, true, '/search/index.php'],
113
            'enabled' => [true, true, true, '/search/index.php'],
113
            'no all courses, no search area' => [true, false, false, '/course/search.php'],
114
            'no all courses, no search area' => [true, false, false, '/course/search.php'],
114
            'no search area' => [true, true, false, '/course/search.php'],
115
            'no search area' => [true, true, false, '/course/search.php'],
Línea 153... Línea 154...
153
    /**
154
    /**
154
     * Data for the test_can_replace_course_search test.
155
     * Data for the test_can_replace_course_search test.
155
     *
156
     *
156
     * @return array[]
157
     * @return array[]
157
     */
158
     */
158
    public function data_can_replace_course_search(): array {
159
    public static function data_can_replace_course_search(): array {
159
        return [
160
        return [
160
            'defaults' => [null, null, null, false],
161
            'defaults' => [null, null, null, false],
161
            'enabled' => [true, true, true, true],
162
            'enabled' => [true, true, true, true],
162
            'no all courses, no search area' => [true, false, false, false],
163
            'no all courses, no search area' => [true, false, false, false],
163
            'no search area' => [true, true, false, false],
164
            'no search area' => [true, true, false, false],
Línea 1501... Línea 1502...
1501
    /**
1502
    /**
1502
     * Test data for test_parse_areaid test fucntion.
1503
     * Test data for test_parse_areaid test fucntion.
1503
     *
1504
     *
1504
     * @return array
1505
     * @return array
1505
     */
1506
     */
1506
    public function parse_search_area_id_data_provider() {
1507
    public static function parse_search_area_id_data_provider(): array {
1507
        return [
1508
        return [
1508
            ['mod_book-chapter', ['mod_book', 'search_chapter']],
1509
            ['mod_book-chapter', ['mod_book', 'search_chapter']],
1509
            ['mod_customcert-activity', ['mod_customcert', 'search_activity']],
1510
            ['mod_customcert-activity', ['mod_customcert', 'search_activity']],
1510
            ['core_course-mycourse', ['core_search', 'core_course_mycourse']],
1511
            ['core_course-mycourse', ['core_search', 'core_course_mycourse']],
1511
        ];
1512
        ];
Línea 1622... Línea 1623...
1622
            ['context', $usercontext->id],
1623
            ['context', $usercontext->id],
1623
            ['context', $context5->id]
1624
            ['context', $context5->id]
1624
        ];
1625
        ];
1625
        $this->assertEquals($expected, $search->get_engine()->get_and_clear_deletes());
1626
        $this->assertEquals($expected, $search->get_engine()->get_and_clear_deletes());
1626
    }
1627
    }
-
 
1628
 
-
 
1629
    /**
-
 
1630
     * Tests the indexing delay (used to avoid race conditions) in {@see manager::index()}.
-
 
1631
     *
-
 
1632
     * @covers \core_search\manager::index
-
 
1633
     */
-
 
1634
    public function test_indexing_delay(): void {
-
 
1635
        global $USER, $CFG;
-
 
1636
 
-
 
1637
        $this->resetAfterTest();
-
 
1638
 
-
 
1639
        // Normally the indexing delay is turned off for test scripts because we don't want to have
-
 
1640
        // to wait 5 seconds after creating anything to index it and it's not like there will be a
-
 
1641
        // race condition (indexing doesn't run at same time as adding). This turns it on.
-
 
1642
        $CFG->searchindexingdelayfortestscript = true;
-
 
1643
 
-
 
1644
        $this->setAdminUser();
-
 
1645
 
-
 
1646
        // Create a course and a forum.
-
 
1647
        $generator = $this->getDataGenerator();
-
 
1648
        $course = $generator->create_course();
-
 
1649
        $forum = $generator->create_module('forum', ['course' => $course->id]);
-
 
1650
 
-
 
1651
        // Skip ahead 5 seconds so everything gets indexed.
-
 
1652
        $now = time();
-
 
1653
        $now += manager::INDEXING_DELAY;
-
 
1654
        $search = \testable_core_search::instance();
-
 
1655
        $search->fake_current_time($now);
-
 
1656
        $search->index();
-
 
1657
        $search->get_engine()->get_and_clear_added_documents();
-
 
1658
 
-
 
1659
        // Basic discussion data.
-
 
1660
        $basicdata = [
-
 
1661
            'course' => $course->id,
-
 
1662
            'forum' => $forum->id,
-
 
1663
            'userid' => $USER->id,
-
 
1664
        ];
-
 
1665
        // Discussion so old it's prior to indexing delay (not realistic).
-
 
1666
        $generator->get_plugin_generator('mod_forum')->create_discussion(array_merge($basicdata,
-
 
1667
            ['timemodified' => $now - manager::INDEXING_DELAY, 'name' => 'Frog']));
-
 
1668
        // Discussion just within indexing delay (simulates if it took a while to add to database).
-
 
1669
        $generator->get_plugin_generator('mod_forum')->create_discussion(array_merge($basicdata,
-
 
1670
            ['timemodified' => $now - (manager::INDEXING_DELAY - 1), 'name' => 'Toad']));
-
 
1671
        // Move time along a bit.
-
 
1672
        $now += 100;
-
 
1673
        $search->fake_current_time($now);
-
 
1674
        // Discussion that happened 5 seconds before the new now.
-
 
1675
        $generator->get_plugin_generator('mod_forum')->create_discussion(array_merge($basicdata,
-
 
1676
            ['timemodified' => $now - (manager::INDEXING_DELAY), 'name' => 'Zombie']));
-
 
1677
        // This one only happened 4 seconds before so it shouldn't be indexed yet.
-
 
1678
        $generator->get_plugin_generator('mod_forum')->create_discussion(array_merge($basicdata,
-
 
1679
            ['timemodified' => $now - (manager::INDEXING_DELAY - 1), 'name' => 'Werewolf']));
-
 
1680
 
-
 
1681
        // Reindex and check that it added the middle two discussions.
-
 
1682
        $search->index();
-
 
1683
        $added = $search->get_engine()->get_and_clear_added_documents();
-
 
1684
        $this->assertCount(2, $added);
-
 
1685
        $this->assertEquals('Toad', $added[0]->get('title'));
-
 
1686
        $this->assertEquals('Zombie', $added[1]->get('title'));
-
 
1687
 
-
 
1688
        // Move time forwards a couple of seconds and now the last one will get indexed.
-
 
1689
        $now += 2;
-
 
1690
        $search->fake_current_time($now);
-
 
1691
        $search->index();
-
 
1692
        $added = $search->get_engine()->get_and_clear_added_documents();
-
 
1693
        $this->assertCount(1, $added);
-
 
1694
        $this->assertEquals('Werewolf', $added[0]->get('title'));
-
 
1695
    }
1627
}
1696
}