Proyectos de Subversion Moodle

Rev

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

Rev 11 Rev 1441
Línea 742... Línea 742...
742
     *   - medium: 1366x768
742
     *   - medium: 1366x768
743
     *   - large: 2560x1600
743
     *   - large: 2560x1600
744
     *
744
     *
745
     * @param string $windowsize size of window.
745
     * @param string $windowsize size of window.
746
     * @param bool $viewport If true, changes viewport rather than window size
746
     * @param bool $viewport If true, changes viewport rather than window size
-
 
747
     * @param bool $scalesize Whether to scale the size by the WINDOWSCALE environment variable
747
     * @throws ExpectationException
748
     * @throws ExpectationException
748
     */
749
     */
749
    protected function resize_window($windowsize, $viewport = false) {
750
    protected function resize_window(
-
 
751
        string $windowsize,
-
 
752
        bool $viewport = false,
-
 
753
        bool $scalesize = true,
-
 
754
    ): void {
750
        global $CFG;
755
        global $CFG;
Línea 751... Línea 756...
751
 
756
 
752
        // Non JS don't support resize window.
757
        // Non JS don't support resize window.
753
        if (!$this->running_javascript()) {
758
        if (!$this->running_javascript()) {
Línea 788... Línea 793...
788
        if (isset($CFG->behat_window_size_modifier) && is_numeric($CFG->behat_window_size_modifier)) {
793
        if (isset($CFG->behat_window_size_modifier) && is_numeric($CFG->behat_window_size_modifier)) {
789
            $width *= $CFG->behat_window_size_modifier;
794
            $width *= $CFG->behat_window_size_modifier;
790
            $height *= $CFG->behat_window_size_modifier;
795
            $height *= $CFG->behat_window_size_modifier;
791
        }
796
        }
Línea -... Línea 797...
-
 
797
 
-
 
798
        if ($scalesize) {
-
 
799
            // Scale the window size by the WINDOWSCALE environment variable.
-
 
800
            // This is intended to be used for Behat reruns to negate the impact of browser window size issues.
-
 
801
            // This allows a per-run, runtime configuration of the scaling, unlike behat_window_size_modifier which
-
 
802
            // typically applies to all runs.
-
 
803
            $scalefactor = getenv('WINDOWSCALE') ? floatval(getenv('WINDOWSCALE')) : 1;
-
 
804
            $width *= $scalefactor;
-
 
805
            $height *= $scalefactor;
-
 
806
        }
792
 
807
 
793
        if ($viewport) {
808
        if ($viewport) {
794
            // When setting viewport size, we set it so that the document width will be exactly
809
            // When setting viewport size, we set it so that the document width will be exactly
795
            // as specified, assuming that there is a vertical scrollbar. (In cases where there is
810
            // as specified, assuming that there is a vertical scrollbar. (In cases where there is
796
            // no scrollbar it will be slightly wider. We presume this is rare and predictable.)
811
            // no scrollbar it will be slightly wider. We presume this is rare and predictable.)
Línea 1021... Línea 1036...
1021
 
1036
 
1022
        if (!$this->running_javascript()) {
1037
        if (!$this->running_javascript()) {
1023
            return;
1038
            return;
Línea 1024... Línea 1039...
1024
        }
1039
        }
-
 
1040
 
-
 
1041
        // Look for DOM elements with deprecated message in before pseudo-element.
-
 
1042
        $js = <<<EOF
-
 
1043
            [...document.querySelectorAll('*')].flatMap(el => {
-
 
1044
                const beforeContent = window.getComputedStyle(el, ':before').content;
-
 
1045
                if (beforeContent.startsWith('"Deprecated style in use')) {
-
 
1046
                    const deprecatedClass = beforeContent.match(/\(([^)]+)\)/)?.[1] ?? 'unknown';
-
 
1047
                    return [deprecatedClass + ' (found in: ' + el.classList + ')'];
-
 
1048
                }
-
 
1049
                return [];
-
 
1050
            });
-
 
1051
        EOF;
-
 
1052
 
-
 
1053
        $deprecations = $this->evaluate_script($js);
-
 
1054
        if ($deprecations) {
-
 
1055
            $deprecationdata = "Deprecated styles found:\n";
-
 
1056
            foreach ($deprecations as $deprecation) {
-
 
1057
                $deprecationdata .= "  {$deprecation}\n";
-
 
1058
            }
-
 
1059
            throw new \Exception(html_entity_decode($deprecationdata, ENT_COMPAT));
-
 
1060
        }
-
 
1061
    }
-
 
1062
 
-
 
1063
 
-
 
1064
    /**
-
 
1065
     * Internal step definition to find deprecated icons.
-
 
1066
     *
-
 
1067
     * Part of behat_hooks class as is part of the testing framework, is auto-executed
-
 
1068
     * after each step so no features will splicitly use it.
-
 
1069
     *
-
 
1070
     * @throws Exception Unknown type, depending on what we caught in the hook or basic \Exception.
-
 
1071
     * @see Moodle\BehatExtension\Tester\MoodleStepTester
-
 
1072
     */
-
 
1073
    public function look_for_deprecated_icons() {
-
 
1074
        if (behat_config_manager::get_behat_run_config_value('no-icon-deprecations')) {
-
 
1075
            return;
-
 
1076
        }
-
 
1077
 
-
 
1078
        if (!$this->running_javascript()) {
-
 
1079
            return;
-
 
1080
        }
1025
 
1081
 
1026
        // Look for any DOM element with deprecated message in before pseudo-element.
1082
        // Look for any DOM element with deprecated icon.
1027
        $js = <<<EOF
1083
        $js = <<<EOF
1028
            [...document.querySelectorAll('*')].some(
1084
            [...document.querySelectorAll('.icon.deprecated')].some(
1029
                el => window.getComputedStyle(el, ':before').content === '"Deprecated style in use"'
1085
                deprecatedicon => true
1030
            );
1086
            );
1031
        EOF;
1087
        EOF;
-
 
1088
        if ($this->evaluate_script($js)) {
-
 
1089
            throw new \Exception(html_entity_decode(
-
 
1090
                "Deprecated icon in use. Enable \$CFG->debugdisplay for detailed debugging information in the console",
1032
        if ($this->evaluate_script($js)) {
1091
                ENT_COMPAT,
1033
            throw new \Exception(html_entity_decode("Deprecated style in use", ENT_COMPAT));
1092
            ));
Línea 1034... Línea 1093...
1034
        }
1093
        }
1035
    }
1094
    }
Línea 1048... Línea 1107...
1048
    }
1107
    }
Línea 1049... Línea 1108...
1049
 
1108
 
1050
    /**
1109
    /**
1051
     * Helper function to execute api in a given context.
1110
     * Helper function to execute api in a given context.
-
 
1111
     *
-
 
1112
     * Note: The contextapi does not support a callback.
1052
     *
1113
     *
1053
     * @param string $contextapi context in which api is defined.
1114
     * @param string|array $contextapi context in which api is defined.
1054
     * @param array|mixed $params list of params to pass or a single parameter
1115
     * @param array|mixed $params list of params to pass or a single parameter
-
 
1116
     * @throws Exception
1055
     * @throws Exception
1117
     * @throws DriverException
1056
     */
1118
     */
-
 
1119
    protected function execute(
-
 
1120
        string|array $contextapi,
-
 
1121
        mixed $params = [],
1057
    protected function execute($contextapi, $params = array()) {
1122
    ): void {
1058
        if (!is_array($params)) {
1123
        if (!is_array($params)) {
-
 
1124
            $params = [$params];
-
 
1125
        }
-
 
1126
 
-
 
1127
        if (is_string($contextapi)) {
-
 
1128
            $contextapi = explode('::', $contextapi);
-
 
1129
        }
-
 
1130
 
-
 
1131
        if (count($contextapi) !== 2) {
1059
            $params = array($params);
1132
            throw new DriverException('Invalid contextapi format, expected "context::api" or ["context", "api"]');
Línea 1060... Línea 1133...
1060
        }
1133
        }
1061
 
1134
 
-
 
1135
        // Get required context and execute the api.
-
 
1136
        [$classname, $method] = $contextapi;
-
 
1137
        if (!is_string($classname) || !is_string($method)) {
-
 
1138
            throw new DriverException('Invalid contextapi format, expected "context::api" or ["context", "api"]');
1062
        // Get required context and execute the api.
1139
        }
1063
        $contextapi = explode("::", $contextapi);
1140
 
Línea 1064... Línea 1141...
1064
        $context = behat_context_helper::get($contextapi[0]);
1141
        $context = behat_context_helper::get($classname);
1065
        call_user_func_array(array($context, $contextapi[1]), $params);
1142
        call_user_func_array([$context, $method], $params);
Línea 1066... Línea 1143...
1066
 
1143
 
Línea 1073... Línea 1150...
1073
        // Look for exceptions.
1150
        // Look for exceptions.
1074
        $this->look_for_exceptions();
1151
        $this->look_for_exceptions();
Línea 1075... Línea 1152...
1075
 
1152
 
1076
        // Look for deprecated styles.
1153
        // Look for deprecated styles.
-
 
1154
        $this->look_for_deprecated_styles();
-
 
1155
 
-
 
1156
        // Look for deprecated icons.
1077
        $this->look_for_deprecated_styles();
1157
        $this->look_for_deprecated_icons();
Línea 1078... Línea 1158...
1078
    }
1158
    }
1079
 
1159
 
1080
    /**
1160
    /**
Línea 1110... Línea 1190...
1110
 
1190
 
1111
        $sid = $this->getSession()->getCookie('MoodleSession');
1191
        $sid = $this->getSession()->getCookie('MoodleSession');
1112
        if (empty($sid)) {
1192
        if (empty($sid)) {
1113
            throw new coding_exception('failed to get moodle session');
1193
            throw new coding_exception('failed to get moodle session');
1114
        }
1194
        }
1115
        $userid = $DB->get_field('sessions', 'userid', ['sid' => $sid]);
1195
        $session = \core\session\manager::get_session_by_sid($sid);
1116
        if (empty($userid)) {
1196
        if (empty($session->userid)) {
1117
            throw new coding_exception('failed to get user from seession id '.$sid);
1197
            throw new coding_exception('failed to get user from session id: '.$sid);
1118
        }
1198
        }
1119
        return $DB->get_record('user', ['id' => $userid]);
1199
        return $DB->get_record('user', ['id' => $session->userid]);
Línea 1120... Línea 1200...
1120
    }
1200
    }
1121
 
1201
 
1122
    /**
1202
    /**
Línea 1625... Línea 1705...
1625
     * An example usage may be:
1705
     * An example usage may be:
1626
     *
1706
     *
1627
     *    // Note: phpDoc beforeStep attribution not shown.
1707
     *    // Note: phpDoc beforeStep attribution not shown.
1628
     *    public function before_step(StepScope $scope) {
1708
     *    public function before_step(StepScope $scope) {
1629
     *        $callback = function (string $tag): bool {
1709
     *        $callback = function (string $tag): bool {
1630
     *            return $tag === 'editor_atto' || substr($tag, 0, 5) === 'atto_';
1710
     *            return $tag === 'editor_tiny' || substr($tag, 0, 5) === 'tiny_';
1631
     *        };
1711
     *        };
1632
     *
1712
     *
1633
     *        if (!self::scope_tags_match($scope, $callback)) {
1713
     *        if (!self::scope_tags_match($scope, $callback)) {
1634
     *            return;
1714
     *            return;
1635
     *        }
1715
     *        }
Línea 1667... Línea 1747...
1667
 
1747
 
1668
        return !empty($matches);
1748
        return !empty($matches);
Línea 1669... Línea 1749...
1669
    }
1749
    }
-
 
1750
 
-
 
1751
    /**
-
 
1752
     * Get the user object from an identifier.
-
 
1753
     *
-
 
1754
     * The user username and email fields are checked.
-
 
1755
     *
-
 
1756
     * @param string $identifier The user's username or email.
-
 
1757
     * @return stdClass|null The user id or null if not found.
-
 
1758
     */
-
 
1759
    protected function get_user_by_identifier(string $identifier): ?stdClass {
-
 
1760
        global $DB;
-
 
1761
 
-
 
1762
        $sql = <<<EOF
-
 
1763
            SELECT *
-
 
1764
              FROM {user}
-
 
1765
             WHERE username = :username
-
 
1766
                OR email = :email
-
 
1767
        EOF;
-
 
1768
 
-
 
1769
        $result = $DB->get_record_sql($sql, [
-
 
1770
            'username' => $identifier,
-
 
1771
            'email' => $identifier,
-
 
1772
        ]);
-
 
1773
 
-
 
1774
        return $result ?: null;
-
 
1775
    }
1670
 
1776
 
1671
    /**
1777
    /**
1672
     * Get the user id from an identifier.
1778
     * Get the user id from an identifier.
1673
     *
1779
     *
1674
     * The user username and email fields are checked.
1780
     * The user username and email fields are checked.
Línea 1691... Línea 1797...
1691
            'email' => $identifier,
1797
            'email' => $identifier,
1692
        ]);
1798
        ]);
Línea 1693... Línea 1799...
1693
 
1799
 
1694
        return $result ?: null;
1800
        return $result ?: null;
-
 
1801
    }
-
 
1802
 
-
 
1803
    /**
-
 
1804
     * Prepare an xpath for insertion into Selenium JavaScript.
-
 
1805
     *
-
 
1806
     * @param string $xpath
-
 
1807
     * @return string
-
 
1808
     */
-
 
1809
    protected function prepare_xpath_for_javascript(string $xpath): string {
-
 
1810
        $newlines = [
-
 
1811
            "\r\n",
-
 
1812
            "\r",
-
 
1813
            "\n",
-
 
1814
        ];
-
 
1815
        return str_replace($newlines, ' ', $xpath);
1695
    }
1816
    }