Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403]

   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   * Googledocs portfolio functional test.
  19   *
  20   * @package    portfolio_googledocs
  21   * @category   tests
  22   * @copyright  2016 Marina Glancy
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  global $CFG;
  29  require_once($CFG->libdir . '/portfoliolib.php');
  30  require_once($CFG->libdir . '/portfolio/forms.php');
  31  
  32  /**
  33   * Googledocs portfolio functional test.
  34   *
  35   * @package    portfolio_googledocs
  36   * @category   tests
  37   * @copyright  2016 Marina Glancy
  38   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class portfolio_googledocs_plugin_testcase extends advanced_testcase {
  41  
  42      /** @var string name of the portfolio plugin */
  43      protected $pluginname = 'googledocs';
  44  
  45      /**
  46       * Creates a new instance of the portfolio plugin
  47       *
  48       * @param string $name name of the instance
  49       * @param stdClass $data config data for the instance
  50       * @return portfolio_plugin_base
  51       */
  52      protected function enable_plugin($name = 'Instance name', $data = null) {
  53          $data = $data ?: new stdClass();
  54          $instance = portfolio_static_function($this->pluginname, 'create_instance', $this->pluginname, $name, $data);
  55          core_plugin_manager::reset_caches();
  56          return $instance;
  57      }
  58  
  59      /**
  60       * Test for method enable_plugin()
  61       */
  62      public function test_enable() {
  63          global $DB;
  64          $this->resetAfterTest();
  65          $instance = $this->enable_plugin();
  66          $record = $DB->get_record('portfolio_instance', ['plugin' => $this->pluginname]);
  67          $this->assertEquals($record->id, $instance->get('id'));
  68          $this->assertEquals('portfolio_plugin_'  . $this->pluginname, get_class($instance));
  69          $this->assertEquals(1, $instance->get('visible'));
  70      }
  71  
  72      /**
  73       * Test submitting a form for creating an instance
  74       */
  75      public function test_create_form() {
  76          $formdata = ['name' => 'Instance name', 'clientid' => 'CLIENT', 'secret' => 'SECRET'];
  77          portfolio_admin_form::mock_submit($formdata);
  78  
  79          $form = new portfolio_admin_form('', array('plugin' => $this->pluginname,
  80              'instance' => null, 'portfolio' => null,
  81              'action' => 'new', 'visible' => 1));
  82          $data = $form->get_data();
  83          $this->assertEquals('new', $data->action);
  84          $this->assertEquals(1, $data->visible);
  85          $this->assertEquals($this->pluginname, $data->plugin);
  86          foreach ($formdata as $key => $value) {
  87              $this->assertEquals($value, $data->$key);
  88          }
  89      }
  90  
  91      /**
  92       * Test submitting a form for editing an instance
  93       */
  94      public function test_edit_form() {
  95          $this->resetAfterTest();
  96          $instance = $this->enable_plugin();
  97  
  98          $formdata = ['name' => 'New name', 'clientid' => 'CLIENT', 'secret' => 'SECRET'];
  99          portfolio_admin_form::mock_submit($formdata);
 100  
 101          $form = new portfolio_admin_form('', array('plugin' => $this->pluginname,
 102              'instance' => $instance, 'portfolio' => $instance->get('id'),
 103              'action' => 'edit', 'visible' => $instance->get('visible')));
 104          $this->assertTrue($form->is_validated());
 105          $this->assertTrue($form->is_submitted());
 106          $data = $form->get_data();
 107          $this->assertEquals('edit', $data->action);
 108          $this->assertEquals($instance->get('visible'), $data->visible);
 109          $this->assertEquals($this->pluginname, $data->plugin);
 110          foreach ($formdata as $key => $value) {
 111              $this->assertEquals($value, $data->$key);
 112          }
 113      }
 114  }