Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 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
namespace core;
18
 
19
use advanced_testcase;
20
use stdClass;
21
use ReflectionMethod;
22
 
23
/**
24
 * Unit tests for emoticon manager
25
 *
26
 * @package     core
27
 * @covers      \core\emoticon_manager
28
 * @copyright   2024 Paul Holden <paulh@moodle.com>
29
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30
 */
31
final class emoticon_manager_test extends advanced_testcase {
32
 
33
    /**
34
     * Data provider for {@see test_get_emoticons}
35
     *
36
     * @return array[]
37
     */
38
    public static function get_emoticons_provider(): array {
39
        return [
40
            'Only selectable' => [true, ['wink', 'biggrin'], ['egg', 'martin']],
41
            'Including non-selectable' => [false, ['wink', 'biggrin', 'egg', 'martin']],
42
        ];
43
    }
44
 
45
    /**
46
     * Test getting enabled emoticons
47
     *
48
     * @param bool $selectable
49
     * @param string[] $expected
50
     * @param string[] $excluded
51
     *
52
     * @dataProvider get_emoticons_provider
53
     */
54
    public function test_get_emoticons(
55
        bool $selectable,
56
        array $expected,
57
        array $excluded = [],
58
    ): void {
59
        $emoticons = (new emoticon_manager())->get_emoticons($selectable);
60
        $identifiers = array_column($emoticons, 'altidentifier');
61
 
62
        // Assert the subset of expected identifiers are all present.
63
        $this->assertEquals([], array_diff($expected, $identifiers));
64
 
65
        // Assert none of the excluded identifiers are present.
66
        $this->assertEquals($excluded, array_diff($excluded, $identifiers));
67
    }
68
 
69
    /**
70
     * Test converting emoticon object into renderable
71
     */
72
    public function test_prepare_renderable_emoticon(): void {
73
        $manager = new emoticon_manager();
74
 
75
        // With language string identifier.
76
        $emoticon = $this->prepare_emoticon_object(':)', 's/smiley', 'smiley');
77
        $pixemoticon = $manager->prepare_renderable_emoticon($emoticon);
78
 
79
        $this->assertEquals('s/smiley', $pixemoticon->pix);
80
        $this->assertEquals('core', $pixemoticon->component);
81
        $this->assertEquals([
82
            'class' => 'emoticon',
83
            'alt' => 'smile',
84
            'title' => 'smile',
85
        ], $pixemoticon->attributes);
86
 
87
        // With language string identifier from another component.
88
        $emoticon = $this->prepare_emoticon_object(':)', 's/smiley', 'thanks', 'core');
89
        $pixemoticon = $manager->prepare_renderable_emoticon($emoticon);
90
 
91
        $this->assertEquals('s/smiley', $pixemoticon->pix);
92
        $this->assertEquals('core', $pixemoticon->component);
93
        $this->assertEquals([
94
            'class' => 'emoticon',
95
            'alt' => 'Thanks',
96
            'title' => 'Thanks',
97
        ], $pixemoticon->attributes);
98
 
99
        // Without language string identifier.
100
        $emoticon = $this->prepare_emoticon_object(':-O', 's/shock');
101
        $pixemoticon = $manager->prepare_renderable_emoticon($emoticon);
102
 
103
        $this->assertEquals('s/shock', $pixemoticon->pix);
104
        $this->assertEquals('core', $pixemoticon->component);
105
        $this->assertEquals([
106
            'class' => 'emoticon',
107
            'alt' => ':-O',
108
            'title' => ':-O',
109
        ], $pixemoticon->attributes);
110
    }
111
 
112
    /**
113
     * Proxy method for creating emoticon object via {@see \core\emoticon_manager::prepare_emoticon_object}
114
     *
115
     * @param mixed ...$params
116
     * @return stdClass
117
     */
118
    private function prepare_emoticon_object(...$params): stdClass {
119
        $manager = new emoticon_manager();
120
        $method = new ReflectionMethod($manager, 'prepare_emoticon_object');
121
        return $method->invoke($manager, ...$params);
122
    }
123
}