Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 * TinyMCE HTML plugin custom behat steps definitions.
19
 *
20
 * @package    tiny_html
21
 * @category   test
22
 * @copyright  2023 Matt Porritt <matt.porritt@moodle.com>
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
26
use Behat\Mink\Exception\ExpectationException;
27
use Behat\Gherkin\Node\{PyStringNode};
28
 
29
// NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
30
require_once(__DIR__ . '/../../../../tests/behat/editor_tiny_helpers.php');
31
require_once(__DIR__ . '/../../../../../../behat/behat_base.php');
32
 
33
/**
34
 * TinyMCE HTML plugin custom behat steps definitions.
35
 *
36
 * @package    tiny_html
37
 * @category   test
38
 * @copyright  2023 Matt Porritt <matt.porritt@moodle.com>
39
 */
40
class behat_tiny_html extends behat_base {
41
    use editor_tiny_helpers;
42
 
43
    /**
44
     * Get Javascript to navigate to the shadow DOM of the editor,
45
     * and find specified sourcecode text.
46
     *
47
     * @param string $editorid The editor id to search within.
48
     * @param string $sourcecode The sourcecode to find.
49
     * @return string The Javascript to execute.
50
     */
51
    protected function get_javascript_sourcecode_search(string $editorid, string $sourcecode): string {
52
        return <<<EOF
53
            const container = document.getElementById('{$editorid}_codeMirrorContainer');
54
            const shadowRoot = container.shadowRoot;
55
            const sourceCode = shadowRoot.querySelector('.modal-codemirror-container [contenteditable="true"]').innerText
56
            const textToFind = `$sourcecode`;
57
 
58
            if (sourceCode == textToFind) {
59
              resolve(true);
60
            } else {
61
              resolve(false);
62
            }
63
        EOF;
64
    }
65
 
66
    /**
67
     * Gets the specified formatted single line source code from the editor
68
     * and compares it to what is expected.
69
     *
70
     * @When /^I should see "(?P<sourcecodelocator_string>(?:[^"]|\\")*)" source code for the "(?P<locator_string>(?:[^"]|\\")*)" TinyMCE editor$/
71
     * @throws ExpectationException
72
     * @param string $sourcecode The type of element to select (for example `p` or `span`)
73
     * @param string $locator The editor to select within
74
     */
75
    public function get_source_code(string $sourcecode, string $locator): void {
76
        $this->require_tiny_tags();
77
 
78
        $editor = $this->get_textarea_for_locator($locator);
79
        $editorid = $editor->getAttribute('id');
80
        $js = $this->get_javascript_sourcecode_search($editorid, $sourcecode);
81
 
82
        if ($this->evaluate_javascript_for_editor($editorid, $js) != 'true') {
83
            throw new ExpectationException("Source code does not match expected.", $this->getSession());
84
        }
85
    }
86
 
87
    /**
88
     * Gets the specified formatted multiline source code from the editor
89
     * and compares it to what is expected.
90
     *
91
     * @When /^I should see this multiline source code for the "(?P<locator_string>(?:[^"]|\\")*)" TinyMCE editor:$/
92
     * @throws ExpectationException
93
     * @param string $locator
94
     * @param PyStringNode $sourcecode
95
     * @return void
96
     */
97
    public function get_multiline_source_code(string $locator, PyStringNode $sourcecode): void {
98
        $this->require_tiny_tags();
99
 
100
        $editor = $this->get_textarea_for_locator($locator);
101
        $editorid = $editor->getAttribute('id');
102
        $js = $this->get_javascript_sourcecode_search($editorid, $sourcecode);
103
 
104
        if ($this->evaluate_javascript_for_editor($editorid, $js) != 'true') {
105
            throw new ExpectationException("Source code is not indented as expected.", $this->getSession());
106
        }
107
    }
108
}