Proyectos de Subversion Moodle

Rev

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

Rev 11 Rev 1441
Línea 17... Línea 17...
17
namespace core_tag;
17
namespace core_tag;
Línea 18... Línea 18...
18
 
18
 
19
use core_tag_area;
19
use core_tag_area;
20
use core_tag_collection;
20
use core_tag_collection;
-
 
21
use core_tag_tag;
Línea 21... Línea 22...
21
use core_tag_tag;
22
use core_tag;
22
 
23
 
23
/**
24
/**
24
 * Tag related unit tests.
25
 * Tag related unit tests.
25
 *
26
 *
26
 * @package core_tag
27
 * @package core_tag
27
 * @category test
28
 * @category test
-
 
29
 * @copyright 2014 Mark Nelson <markn@moodle.com>
28
 * @copyright 2014 Mark Nelson <markn@moodle.com>
30
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31
 * @covers \core_tag_tag
Línea 30... Línea 32...
30
 */
32
 */
31
class taglib_test extends \advanced_testcase {
33
final class taglib_test extends \advanced_testcase {
32
 
34
 
33
    /**
35
    /**
34
     * Test set up.
36
     * Test set up.
35
     *
37
     *
-
 
38
     * This is executed before running any test in this file.
36
     * This is executed before running any test in this file.
39
     */
37
     */
40
    public function setUp(): void {
Línea 38... Línea 41...
38
    public function setUp(): void {
41
        parent::setUp();
39
        $this->resetAfterTest();
-
 
40
    }
-
 
41
 
-
 
42
    /**
-
 
43
     * Test that the tag_set function throws an exception.
-
 
44
     * This function was deprecated in 3.1
-
 
45
     */
-
 
46
    public function test_tag_set_get(): void {
-
 
47
        $this->expectException('coding_exception');
-
 
48
        $this->expectExceptionMessage('tag_set() can not be used anymore. Please use ' .
-
 
49
            'core_tag_tag::set_item_tags().');
-
 
50
        tag_set();
-
 
51
    }
-
 
52
 
-
 
53
    /**
-
 
54
     * Test that tag_set_add function throws an exception.
-
 
55
     * This function was deprecated in 3.1
-
 
56
     */
-
 
57
    public function test_tag_set_add(): void {
-
 
58
        $this->expectException('coding_exception');
-
 
59
        $this->expectExceptionMessage('tag_set_add() can not be used anymore. Please use ' .
-
 
60
            'core_tag_tag::add_item_tag().');
-
 
61
        tag_set_add();
-
 
62
    }
-
 
63
 
-
 
64
    /**
-
 
65
     * Test that tag_set_delete function returns an exception.
-
 
66
     * This function was deprecated in 3.1
-
 
67
     */
-
 
68
    public function test_tag_set_delete(): void {
-
 
69
        $this->expectException('coding_exception');
-
 
70
        $this->expectExceptionMessage('tag_set_delete() can not be used anymore. Please use ' .
-
 
71
            'core_tag_tag::remove_item_tag().');
-
 
72
        tag_set_delete();
42
        $this->resetAfterTest();
73
    }
43
    }
74
 
44
 
75
    /**
45
    /**
Línea 141... Línea 111...
141
            \context_module::instance($book1->cmid), 'A random tag for a book chapter');
111
            \context_module::instance($book1->cmid), 'A random tag for a book chapter');
142
        $this->assertEquals(1, $DB->get_field('tag_instance', 'ordering', ['id' => $ti5]));
112
        $this->assertEquals(1, $DB->get_field('tag_instance', 'ordering', ['id' => $ti5]));
143
    }
113
    }
Línea 144... Línea 114...
144
 
114
 
145
    /**
-
 
146
     * Test that tag_assign function throws an exception.
-
 
147
     * This function was deprecated in 3.1
-
 
148
     */
-
 
149
    public function test_tag_assign(): void {
-
 
150
        $this->expectException(\coding_exception::class);
-
 
151
        $this->expectExceptionMessage('tag_assign() can not be used anymore. Please use core_tag_tag::set_item_tags() ' .
-
 
152
            'or core_tag_tag::add_item_tag() instead.');
-
 
153
        tag_assign();
-
 
154
    }
-
 
155
 
-
 
156
    /**
115
    /**
157
     * Test the tag cleanup function used by the cron.
116
     * Test the tag cleanup function used by the cron.
158
     */
117
     */
159
    public function test_tag_cleanup(): void {
118
    public function test_tag_cleanup(): void {
Línea 832... Línea 791...
832
        $this->assertEquals($collid2, $user1tags[0]->tagcollid);
791
        $this->assertEquals($collid2, $user1tags[0]->tagcollid);
833
        $this->assertEquals($collid2, $user2tags[2]->tagcollid);
792
        $this->assertEquals($collid2, $user2tags[2]->tagcollid);
834
    }
793
    }
Línea 835... Línea 794...
835
 
794
 
836
    /**
-
 
837
     * Tests that tag_normalize function throws an exception.
-
 
838
     * This function was deprecated in 3.1
-
 
839
     */
-
 
840
    public function test_normalize(): void {
-
 
841
        $this->expectException(\coding_exception::class);
-
 
842
        $this->expectExceptionMessage('tag_normalize() can not be used anymore. Please use ' .
-
 
843
            'core_tag_tag::normalize().');
-
 
844
        tag_normalize();
-
 
845
    }
-
 
846
 
-
 
847
    /**
795
    /**
848
     * Test functions core_tag_tag::create_if_missing() and core_tag_tag::get_by_name_bulk().
796
     * Test functions core_tag_tag::create_if_missing() and core_tag_tag::get_by_name_bulk().
849
     */
797
     */
850
    public function test_create_get(): void {
798
    public function test_create_get(): void {
Línea 1956... Línea 1904...
1956
        $this->expectException('Exception');
1904
        $this->expectException('Exception');
1957
        core_tag_tag::change_instances_context([$fooinstance1->id], $context2);
1905
        core_tag_tag::change_instances_context([$fooinstance1->id], $context2);
1958
    }
1906
    }
Línea 1959... Línea 1907...
1959
 
1907
 
-
 
1908
    /**
-
 
1909
     * Tests user pagination works correctly for filtered users.
-
 
1910
     */
-
 
1911
    public function test_user_get_tagged_users(): void {
-
 
1912
        global $DB;
-
 
1913
 
-
 
1914
        // Create some users.
-
 
1915
        $users = [];
-
 
1916
        for ($i = 0; $i < 11; $i++) {
-
 
1917
            $users[] = $this->getDataGenerator()->create_user();
-
 
1918
        }
-
 
1919
 
-
 
1920
        // Create the tag.
-
 
1921
        $tagcollid = core_tag_collection::get_default();
-
 
1922
        $tag = $this->getDataGenerator()->create_tag(['tagcollid' => $tagcollid, 'rawname' => 'bike']);
-
 
1923
 
-
 
1924
        // Add the tag to the users.
-
 
1925
        for ($i = 0; $i < count($users); $i++) {
-
 
1926
            core_tag_tag::add_item_tag('core', 'user', $users[$i]->id,
-
 
1927
                    \context_user::instance($users[$i]->id), 'bike');
-
 
1928
        }
-
 
1929
 
-
 
1930
        // The logged-in user.
-
 
1931
        $this->setUser($users[0]);
-
 
1932
 
-
 
1933
        // Get the tagged users.
-
 
1934
        $tag = core_tag_tag::get($tag->id, '*');
-
 
1935
        $taggedusers = user_get_tagged_users($tag);
-
 
1936
 
-
 
1937
        // Ensure it has content.
-
 
1938
        $this->assertEquals(1, $taggedusers->hascontent);
-
 
1939
 
-
 
1940
        // Ensure it should have 1 user and the "more" link is hidden (null).
-
 
1941
        $this->assertEquals(1, $this->count_html_elements($taggedusers->content, 'li'));
-
 
1942
        $this->assertNull($taggedusers->nextpageurl);
-
 
1943
 
-
 
1944
        // Test which users are visible to the logged-in user based on the course.
-
 
1945
        // Create a course to tag.
-
 
1946
        $course = $this->getDataGenerator()->create_course();
-
 
1947
        $studentrole = $DB->get_record('role', ['shortname' => 'student']);
-
 
1948
        for ($i = 0; $i < count($users); $i++) {
-
 
1949
            // Enrol only some users (0, 2, 4, 6, 8, 10).
-
 
1950
            if ($i % 2 === 0) {
-
 
1951
                $this->getDataGenerator()->enrol_user($users[$i]->id, $course->id, $studentrole->id);
-
 
1952
            }
-
 
1953
        }
-
 
1954
 
-
 
1955
        // First page should have 5 users and the "more" link is visible (not null).
-
 
1956
        $taggedusers = user_get_tagged_users(
-
 
1957
            tag: $tag,
-
 
1958
            page: 0,
-
 
1959
        );
-
 
1960
        $this->assertNotNull($taggedusers->nextpageurl);
-
 
1961
        $this->assertEquals(5, $this->count_html_elements($taggedusers->content, 'li'));
-
 
1962
 
-
 
1963
        // Second page should have 1 user and the "more" link is hidden (null).
-
 
1964
        $taggedusers = user_get_tagged_users(
-
 
1965
            tag: $tag,
-
 
1966
            page: 1,
-
 
1967
        );
-
 
1968
        $this->assertNull($taggedusers->nextpageurl);
-
 
1969
        $this->assertEquals(1, $this->count_html_elements($taggedusers->content, 'li'));
-
 
1970
    }
-
 
1971
 
-
 
1972
    /**
-
 
1973
     * Counts the number of specified HTML elements in a given HTML string.
-
 
1974
     *
-
 
1975
     * @param string $html The HTML string to be parsed.
-
 
1976
     * @param string $tagname The name of the HTML tag to count (e.g., 'li', 'div').
-
 
1977
     * @return int The number of elements with the specified tag name found in the HTML.
-
 
1978
     */
-
 
1979
    private function count_html_elements(string $html, string $tagname): int {
-
 
1980
        // Load the HTML into DOMDocument.
-
 
1981
        $dom = new \DOMDocument();
-
 
1982
        libxml_use_internal_errors(true); // Suppress warnings for invalid HTML.
-
 
1983
        $dom->loadHTML($html);
-
 
1984
        libxml_clear_errors();
-
 
1985
 
-
 
1986
        // Find all elements with the specified tag name.
-
 
1987
        $elements = $dom->getElementsByTagName($tagname);
-
 
1988
 
-
 
1989
        // Count the number of elements.
-
 
1990
        return $elements->length;
-
 
1991
    }
-
 
1992
 
1960
    /**
1993
    /**
1961
     * Help method to return sorted array of names of correlated tags to use for assertions
1994
     * Help method to return sorted array of names of correlated tags to use for assertions
1962
     * @param core_tag $tag
1995
     * @param core_tag $tag
1963
     * @return string
1996
     * @return string
1964
     */
1997
     */
Línea 1992... Línea 2025...
1992
        $record['ordering'] = 0;
2025
        $record['ordering'] = 0;
1993
        $record['timecreated'] = time();
2026
        $record['timecreated'] = time();
1994
        $record['id'] = $DB->insert_record('tag_instance', $record);
2027
        $record['id'] = $DB->insert_record('tag_instance', $record);
1995
        return (object) $record;
2028
        return (object) $record;
1996
    }
2029
    }
-
 
2030
 
-
 
2031
    /**
-
 
2032
     * Checks the contents of the a tagcloud
-
 
2033
     *
-
 
2034
     * @param array $tags
-
 
2035
     * @param \core_tag\output\tagcloud $tagcloud
-
 
2036
     */
-
 
2037
    protected function assert_tag_cloud_contains_tags(array $tags, \core_tag\output\tagcloud $tagcloud) {
-
 
2038
        global $PAGE;
-
 
2039
        $renderer = $PAGE->get_renderer('core', 'tag');
-
 
2040
        $result = $tagcloud->export_for_template($renderer);
-
 
2041
        $result = json_decode(json_encode($result), true);
-
 
2042
        $this->assertEqualsCanonicalizing($tags, array_values(array_column($result['tags'], 'name')));
-
 
2043
    }
-
 
2044
 
-
 
2045
    public function test_get_tag_cloud(): void {
-
 
2046
        global $DB, $PAGE;
-
 
2047
        $this->resetAfterTest();
-
 
2048
 
-
 
2049
        // Create a course and a user with tags.
-
 
2050
        $this->getDataGenerator()->create_course(['tags' => 'cats,animals']);
-
 
2051
        $this->getDataGenerator()->create_user(['interests' => 'dogs,animals']);
-
 
2052
 
-
 
2053
        // Default tag cloud contains all three tags.
-
 
2054
        $tagcloud = core_tag_collection::get_tag_cloud(0);
-
 
2055
        $this->assert_tag_cloud_contains_tags(['animals', 'cats', 'dogs'], $tagcloud);
-
 
2056
 
-
 
2057
        // Create two new tag collections, move course tags to C1 and user tags to C2.
-
 
2058
        $c1 = core_tag_collection::create((object)['name' => 'C1', 'searchable' => 1]);
-
 
2059
        $c2 = core_tag_collection::create((object)['name' => 'C2', 'searchable' => 1]);
-
 
2060
        $tagareacourse = $DB->get_record('tag_area', ['component' => 'core', 'itemtype' => 'course'], '*', MUST_EXIST);
-
 
2061
        core_tag_area::update($tagareacourse, ['tagcollid' => $c1->id]);
-
 
2062
        $tagareauser = $DB->get_record('tag_area', ['component' => 'core', 'itemtype' => 'user'], '*', MUST_EXIST);
-
 
2063
        core_tag_area::update($tagareauser, ['tagcollid' => $c2->id]);
-
 
2064
 
-
 
2065
        // Tag cloud still has all tags and you can also search by a collection. Tag 'animals' now has two different view links.
-
 
2066
        $this->assert_tag_cloud_contains_tags(['animals', 'animals', 'cats', 'dogs'], core_tag_collection::get_tag_cloud(0));
-
 
2067
        $this->assert_tag_cloud_contains_tags(['animals', 'cats'], core_tag_collection::get_tag_cloud($c1->id));
-
 
2068
        $this->assert_tag_cloud_contains_tags(['animals', 'dogs'], core_tag_collection::get_tag_cloud($c2->id));
-
 
2069
 
-
 
2070
        // Make user interest tag area not searchable.
-
 
2071
        core_tag_collection::update($c2, ['searchable' => 0]);
-
 
2072
        // Check that the user interest tags do not appear in the tagclouds.
-
 
2073
        $this->assert_tag_cloud_contains_tags(['animals', 'cats'], core_tag_collection::get_tag_cloud(0));
-
 
2074
        $this->assert_tag_cloud_contains_tags(['animals', 'cats'], core_tag_collection::get_tag_cloud($c1->id));
-
 
2075
        $this->assert_tag_cloud_contains_tags([], core_tag_collection::get_tag_cloud($c2->id));
-
 
2076
    }
1997
}
2077
}