| 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 | 
            * Steps definitions related with administration.
  | 
        
        
            | 
            | 
           19 | 
            *
  | 
        
        
            | 
            | 
           20 | 
            * @package   core_admin
  | 
        
        
            | 
            | 
           21 | 
            * @category  test
  | 
        
        
            | 
            | 
           22 | 
            * @copyright 2013 David Monllaó
  | 
        
        
            | 
            | 
           23 | 
            * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           24 | 
            */
  | 
        
        
            | 
            | 
           25 | 
              | 
        
        
            | 
            | 
           26 | 
           // NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
  | 
        
        
            | 
            | 
           27 | 
              | 
        
        
            | 
            | 
           28 | 
           require_once(__DIR__ . '/../../../lib/behat/behat_base.php');
  | 
        
        
            | 
            | 
           29 | 
           require_once(__DIR__ . '/../../../lib/behat/behat_field_manager.php');
  | 
        
        
            | 
            | 
           30 | 
              | 
        
        
            | 
            | 
           31 | 
           use Behat\Gherkin\Node\TableNode as TableNode,
  | 
        
        
            | 
            | 
           32 | 
               Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException;
  | 
        
        
            | 
            | 
           33 | 
              | 
        
        
            | 
            | 
           34 | 
           /**
  | 
        
        
            | 
            | 
           35 | 
            * Site administration level steps definitions.
  | 
        
        
            | 
            | 
           36 | 
            *
  | 
        
        
            | 
            | 
           37 | 
            * @package    core_admin
  | 
        
        
            | 
            | 
           38 | 
            * @category   test
  | 
        
        
            | 
            | 
           39 | 
            * @copyright  2013 David Monllaó
  | 
        
        
            | 
            | 
           40 | 
            * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           41 | 
            */
  | 
        
        
            | 
            | 
           42 | 
           class behat_admin extends behat_base {
  | 
        
        
            | 
            | 
           43 | 
              | 
        
        
            | 
            | 
           44 | 
               /**
  | 
        
        
            | 
            | 
           45 | 
                * Sets the specified site settings. A table with | Setting label | value | is expected.
  | 
        
        
            | 
            | 
           46 | 
                *
  | 
        
        
            | 
            | 
           47 | 
                * @Given /^I set the following administration settings values:$/
  | 
        
        
            | 
            | 
           48 | 
                * @param TableNode $table
  | 
        
        
            | 
            | 
           49 | 
                */
  | 
        
        
            | 
            | 
           50 | 
               public function i_set_the_following_administration_settings_values(TableNode $table) {
  | 
        
        
            | 
            | 
           51 | 
                   if (!$data = $table->getRowsHash()) {
  | 
        
        
            | 
            | 
           52 | 
                       return;
  | 
        
        
            | 
            | 
           53 | 
                   }
  | 
        
        
            | 
            | 
           54 | 
              | 
        
        
            | 
            | 
           55 | 
                   foreach ($data as $label => $value) {
  | 
        
        
            | 
            | 
           56 | 
                       // Navigate straight to the search results fo rthis label.
  | 
        
        
            | 
            | 
           57 | 
                       $this->execute('behat_general::i_visit', ["/admin/search.php?query=" . urlencode($label)]);
  | 
        
        
            | 
            | 
           58 | 
              | 
        
        
            | 
            | 
           59 | 
                       // Admin settings does not use the same DOM structure than other moodle forms
  | 
        
        
            | 
            | 
           60 | 
                       // but we also need to use lib/behat/form_field/* to deal with the different moodle form elements.
  | 
        
        
            | 
            | 
           61 | 
                       $exception = new ElementNotFoundException($this->getSession(), '"' . $label . '" administration setting ');
  | 
        
        
            | 
            | 
           62 | 
              | 
        
        
            | 
            | 
           63 | 
                       // The argument should be converted to an xpath literal.
  | 
        
        
            | 
            | 
           64 | 
                       $label = behat_context_helper::escape($label);
  | 
        
        
            | 
            | 
           65 | 
              | 
        
        
            | 
            | 
           66 | 
                       // Single element settings.
  | 
        
        
            | 
            | 
           67 | 
                       try {
  | 
        
        
            | 
            | 
           68 | 
                           $fieldxpath = "//*[self::input | self::textarea | self::select]" .
  | 
        
        
            | 
            | 
           69 | 
                                   "[not(./@type = 'submit' or ./@type = 'image' or ./@type = 'hidden')]" .
  | 
        
        
            | 
            | 
           70 | 
                                   "[@id=//label[contains(normalize-space(.), $label)]/@for or " .
  | 
        
        
            | 
            | 
           71 | 
                                   "@id=//span[contains(normalize-space(.), $label)]/preceding-sibling::label[1]/@for]";
  | 
        
        
            | 
            | 
           72 | 
                           $fieldnode = $this->find('xpath', $fieldxpath, $exception);
  | 
        
        
            | 
            | 
           73 | 
              | 
        
        
            | 
            | 
           74 | 
                       } catch (ElementNotFoundException $e) {
  | 
        
        
            | 
            | 
           75 | 
                           // Multi element settings, interacting only the first one.
  | 
        
        
            | 
            | 
           76 | 
                           $fieldxpath = "//*[label[contains(., $label)]|span[contains(., $label)]]" .
  | 
        
        
            | 
            | 
           77 | 
                                   "/ancestor::div[contains(concat(' ', normalize-space(@class), ' '), ' form-item ')]" .
  | 
        
        
            | 
            | 
           78 | 
                                   "/descendant::*[self::input | self::textarea | self::select]" .
  | 
        
        
            | 
            | 
           79 | 
                                   "[not(./@type = 'submit' or ./@type = 'image' or ./@type = 'hidden')]";
  | 
        
        
            | 
            | 
           80 | 
                       }
  | 
        
        
            | 
            | 
           81 | 
              | 
        
        
            | 
            | 
           82 | 
                       $this->execute('behat_forms::i_set_the_field_with_xpath_to', [$fieldxpath, $value]);
  | 
        
        
            | 
            | 
           83 | 
                       $this->execute("behat_general::i_click_on", [get_string('savechanges'), 'button']);
  | 
        
        
           | 1441 | 
           ariadna | 
           84 | 
                       // Wait for the page to be redirected.
  | 
        
        
            | 
            | 
           85 | 
                       $this->execute("behat_general::i_wait_to_be_redirected");
  | 
        
        
           | 1 | 
           efrain | 
           86 | 
                   }
  | 
        
        
            | 
            | 
           87 | 
               }
  | 
        
        
            | 
            | 
           88 | 
              | 
        
        
            | 
            | 
           89 | 
               /**
  | 
        
        
            | 
            | 
           90 | 
                * Sets the specified site settings. A table with | config | value | (optional)plugin | (optional)encrypted | is expected.
  | 
        
        
            | 
            | 
           91 | 
                *
  | 
        
        
            | 
            | 
           92 | 
                * @Given /^the following config values are set as admin:$/
  | 
        
        
            | 
            | 
           93 | 
                * @param TableNode $table
  | 
        
        
            | 
            | 
           94 | 
                */
  | 
        
        
           | 1441 | 
           ariadna | 
           95 | 
               #[\core\attribute\example('And the following config values are set as admin:
  | 
        
        
            | 
            | 
           96 | 
                   | sendcoursewelcomemessage | 0 | enrol_manual |')]
  | 
        
        
           | 1 | 
           efrain | 
           97 | 
               public function the_following_config_values_are_set_as_admin(TableNode $table) {
  | 
        
        
            | 
            | 
           98 | 
              | 
        
        
            | 
            | 
           99 | 
                   if (!$data = $table->getRowsHash()) {
  | 
        
        
            | 
            | 
           100 | 
                       return;
  | 
        
        
            | 
            | 
           101 | 
                   }
  | 
        
        
            | 
            | 
           102 | 
              | 
        
        
            | 
            | 
           103 | 
                   foreach ($data as $config => $value) {
  | 
        
        
            | 
            | 
           104 | 
                       // Default plugin value is null.
  | 
        
        
            | 
            | 
           105 | 
                       $plugin = null;
  | 
        
        
            | 
            | 
           106 | 
                       $encrypted = false;
  | 
        
        
            | 
            | 
           107 | 
              | 
        
        
            | 
            | 
           108 | 
                       if (is_array($value)) {
  | 
        
        
            | 
            | 
           109 | 
                           $plugin = $value[1];
  | 
        
        
            | 
            | 
           110 | 
                           if (array_key_exists(2, $value)) {
  | 
        
        
            | 
            | 
           111 | 
                               $encrypted = $value[2] === 'encrypted';
  | 
        
        
            | 
            | 
           112 | 
                           }
  | 
        
        
            | 
            | 
           113 | 
                           $value = $value[0];
  | 
        
        
            | 
            | 
           114 | 
                       }
  | 
        
        
            | 
            | 
           115 | 
              | 
        
        
            | 
            | 
           116 | 
                       if ($encrypted) {
  | 
        
        
            | 
            | 
           117 | 
                           $value = \core\encryption::encrypt($value);
  | 
        
        
            | 
            | 
           118 | 
                       }
  | 
        
        
            | 
            | 
           119 | 
              | 
        
        
            | 
            | 
           120 | 
                       set_config($config, $value, $plugin);
  | 
        
        
            | 
            | 
           121 | 
                   }
  | 
        
        
            | 
            | 
           122 | 
               }
  | 
        
        
            | 
            | 
           123 | 
              | 
        
        
            | 
            | 
           124 | 
               /**
  | 
        
        
            | 
            | 
           125 | 
                * Waits with the provided params if we are running a JS session.
  | 
        
        
            | 
            | 
           126 | 
                *
  | 
        
        
            | 
            | 
           127 | 
                * @param int $timeout
  | 
        
        
            | 
            | 
           128 | 
                * @param string $javascript
  | 
        
        
            | 
            | 
           129 | 
                * @return void
  | 
        
        
            | 
            | 
           130 | 
                */
  | 
        
        
            | 
            | 
           131 | 
               protected function wait($timeout, $javascript = false) {
  | 
        
        
            | 
            | 
           132 | 
                   if ($this->running_javascript()) {
  | 
        
        
            | 
            | 
           133 | 
                       $this->getSession()->wait($timeout, $javascript);
  | 
        
        
            | 
            | 
           134 | 
                   }
  | 
        
        
            | 
            | 
           135 | 
               }
  | 
        
        
            | 
            | 
           136 | 
           }
  |