[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/mod/url/tests/ -> externallib_test.php (source)

   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   * External mod_url functions unit tests
  19   *
  20   * @package    mod_url
  21   * @category   external
  22   * @copyright  2015 Juan Leyva <juan@moodle.com>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   * @since      Moodle 3.0
  25   */
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  global $CFG;
  30  
  31  require_once($CFG->dirroot . '/webservice/tests/helpers.php');
  32  
  33  /**
  34   * External mod_url functions unit tests
  35   *
  36   * @package    mod_url
  37   * @category   external
  38   * @copyright  2015 Juan Leyva <juan@moodle.com>
  39   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   * @since      Moodle 3.0
  41   */
  42  class mod_url_external_testcase extends externallib_advanced_testcase {
  43  
  44      /**
  45       * Test view_url
  46       */
  47      public function test_view_url() {
  48          global $DB;
  49  
  50          $this->resetAfterTest(true);
  51  
  52          // Setup test data.
  53          $course = $this->getDataGenerator()->create_course();
  54          $url = $this->getDataGenerator()->create_module('url', array('course' => $course->id));
  55          $context = context_module::instance($url->cmid);
  56          $cm = get_coursemodule_from_instance('url', $url->id);
  57  
  58          // Test invalid instance id.
  59          try {
  60              mod_url_external::view_url(0);
  61              $this->fail('Exception expected due to invalid mod_url instance id.');
  62          } catch (moodle_exception $e) {
  63              $this->assertEquals('invalidrecord', $e->errorcode);
  64          }
  65  
  66          // Test not-enrolled user.
  67          $user = self::getDataGenerator()->create_user();
  68          $this->setUser($user);
  69          try {
  70              mod_url_external::view_url($url->id);
  71              $this->fail('Exception expected due to not enrolled user.');
  72          } catch (moodle_exception $e) {
  73              $this->assertEquals('requireloginerror', $e->errorcode);
  74          }
  75  
  76          // Test user with full capabilities.
  77          $studentrole = $DB->get_record('role', array('shortname' => 'student'));
  78          $this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id);
  79  
  80          // Trigger and capture the event.
  81          $sink = $this->redirectEvents();
  82  
  83          $result = mod_url_external::view_url($url->id);
  84          $result = external_api::clean_returnvalue(mod_url_external::view_url_returns(), $result);
  85  
  86          $events = $sink->get_events();
  87          $this->assertCount(1, $events);
  88          $event = array_shift($events);
  89  
  90          // Checking that the event contains the expected values.
  91          $this->assertInstanceOf('\mod_url\event\course_module_viewed', $event);
  92          $this->assertEquals($context, $event->get_context());
  93          $moodleurl = new \moodle_url('/mod/url/view.php', array('id' => $cm->id));
  94          $this->assertEquals($moodleurl, $event->get_url());
  95          $this->assertEventContextNotUsed($event);
  96          $this->assertNotEmpty($event->get_name());
  97  
  98          // Test user with no capabilities.
  99          // We need a explicit prohibit since this capability is only defined in authenticated user and guest roles.
 100          assign_capability('mod/url:view', CAP_PROHIBIT, $studentrole->id, $context->id);
 101          // Empty all the caches that may be affected by this change.
 102          accesslib_clear_all_caches_for_unit_testing();
 103          course_modinfo::clear_instance_cache();
 104  
 105          try {
 106              mod_url_external::view_url($url->id);
 107              $this->fail('Exception expected due to missing capability.');
 108          } catch (moodle_exception $e) {
 109              $this->assertEquals('requireloginerror', $e->errorcode);
 110          }
 111  
 112      }
 113  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1