Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | 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
declare(strict_types=1);
18
 
19
require_once(__DIR__  . '/behat_form_field.php');
20
 
21
use \Behat\Mink\Element\NodeElement;
22
 
23
/**
24
 * Custom interaction with select_menu elements
25
 *
26
 * @package   core_form
27
 * @copyright 2022 Shamim Rezaie <shamim@moodle.com>
28
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29
 */
30
class behat_form_select_menu extends behat_form_field {
31
 
32
    /**
33
     * Sets the value of the select menu field.
34
     *
35
     * @param string $value The string that is used to identify an option within the select menu. If the string
36
     *                      has two items separated by '>' (ex. "Group > Option"), the first item ("Group") will be
37
     *                      used to identify a particular group within the select menu, while the second ("Option")
38
     *                      will be used to identify an option within that group. Otherwise, a string with a single
39
     *                      item (ex. "Option") will be used to identify an option within the select menu regardless
40
     *                      of any existing groups.
41
     */
42
    public function set_value($value) {
43
        self::require_javascript();
44
 
45
        $this->field->click();
46
        $option = $this->find_option($value);
47
        $option->click();
48
    }
49
 
50
    public function get_value() {
51
        $rootnode = $this->field->getParent();
52
        $input = $rootnode->find('css', 'input');
53
        return $input->getValue();
54
    }
55
 
1441 ariadna 56
    public function matches($expectedvalue) {
57
        $actualvalue = $this->get_value();
58
        $selectedcontainer = $this->field->find('css', '[data-selected-option]');
59
        $actualtext = $selectedcontainer ? $selectedcontainer->getText() : $this->field->getText();
60
 
61
        return ($expectedvalue == $actualvalue || $expectedvalue == $actualtext);
62
    }
63
 
1 efrain 64
    /**
65
     * Checks whether a given option exists in the select menu field.
66
     *
67
     * @param string $option The string that is used to identify an option within the select menu. If the string
68
     *                       has two items separated by '>' (ex. "Group > Option"), the first item ("Group") will be
69
     *                       used to identify a particular group within the select menu, while the second ("Option")
70
     *                       will be used to identify an option within that group. Otherwise, a string with a single
71
     *                       item (ex. "Option") will be used to identify an option within the select menu regardless
72
     *                       of any existing groups.
73
     * @return bool Whether the option exists in the select menu field or not.
74
     */
75
    public function has_option(string $option): bool {
76
        if ($this->find_option($option)) {
77
            return true;
78
        }
79
        return false;
80
    }
81
 
82
    /**
83
     * Finds and returns a given option from the select menu field.
84
     *
85
     * @param string $option The string that is used to identify an option within the select menu. If the string
86
     *                       has two items separated by '>' (ex. "Group > Option"), the first item ("Group") will be
87
     *                       used to identify a particular group within the select menu, while the second ("Option")
88
     *                       will be used to identify an option within that group. Otherwise, a string with a single
89
     *                       item (ex. "Option") will be used to identify an option within the select menu regardless
90
     *                       of any existing groups.
91
     * @return NodeElement|null The option element or null if it cannot be found.
92
     */
93
    private function find_option(string $option): ?NodeElement {
94
        // Split the value string by ">" to determine whether a group has been specified.
95
        $path = preg_split('/\s*>\s*/', trim($option));
96
 
97
        if (count($path) > 1) { // Group has been specified.
98
            $optionxpath = '//li[contains(@role, "presentation") and normalize-space(text()) = "' .
99
                $this->escape($path[0]) . '"]' .
100
                '/following-sibling::li[contains(@role, "option") and normalize-space(text()) = "' .
101
                $this->escape($path[1]) . '"]';
102
        } else { // Group has not been specified.
103
            $optionxpath = '//li[contains(@role, "option") and normalize-space(text()) = "' .
104
                $this->escape($path[0]) . '"]';
105
        }
106
 
107
        return $this->field->getParent()->find('xpath', $optionxpath);
108
    }
109
}