[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/comment/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 comment functions unit tests
  19   *
  20   * @package    core_comment
  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 2.9
  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 comment functions unit tests
  35   *
  36   * @package    core_comment
  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 2.9
  41   */
  42  class core_comment_externallib_testcase extends externallib_advanced_testcase {
  43  
  44      /**
  45       * Tests set up
  46       */
  47      protected function setUp() {
  48          global $CFG;
  49  
  50          require_once($CFG->dirroot . '/comment/lib.php');
  51      }
  52  
  53      /**
  54       * Test get_comments
  55       */
  56      public function test_get_comments() {
  57          global $DB, $CFG;
  58  
  59          $this->resetAfterTest(true);
  60  
  61          $CFG->usecomments = true;
  62  
  63          $user = $this->getDataGenerator()->create_user();
  64          $course = $this->getDataGenerator()->create_course(array('enablecomment' => 1));
  65          $studentrole = $DB->get_record('role', array('shortname' => 'student'));
  66          $this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id);
  67  
  68          $record = new stdClass();
  69          $record->course = $course->id;
  70          $record->name = "Mod data  test";
  71          $record->intro = "Some intro of some sort";
  72          $record->comments = 1;
  73  
  74          $module = $this->getDataGenerator()->create_module('data', $record);
  75          $field = data_get_field_new('text', $module);
  76  
  77          $fielddetail = new stdClass();
  78          $fielddetail->name = 'Name';
  79          $fielddetail->description = 'Some name';
  80  
  81          $field->define_field($fielddetail);
  82          $field->insert_field();
  83          $recordid = data_add_record($module);
  84  
  85          $datacontent = array();
  86          $datacontent['fieldid'] = $field->field->id;
  87          $datacontent['recordid'] = $recordid;
  88          $datacontent['content'] = 'Asterix';
  89  
  90          $contentid = $DB->insert_record('data_content', $datacontent);
  91          $cm = get_coursemodule_from_instance('data', $module->id, $course->id);
  92  
  93          $context = context_module::instance($module->cmid);
  94  
  95          $this->setUser($user);
  96  
  97          // We need to add the comments manually, the comment API uses the global OUTPUT and this is going to make the WS to fail.
  98          $newcmt = new stdClass;
  99          $newcmt->contextid    = $context->id;
 100          $newcmt->commentarea  = 'database_entry';
 101          $newcmt->itemid       = $recordid;
 102          $newcmt->content      = 'New comment';
 103          $newcmt->format       = 0;
 104          $newcmt->userid       = $user->id;
 105          $newcmt->timecreated  = time();
 106          $cmtid1 = $DB->insert_record('comments', $newcmt);
 107  
 108          $newcmt->content  = 'New comment 2';
 109          $newcmt->timecreated  = time() + 1;
 110          $cmtid2 = $DB->insert_record('comments', $newcmt);
 111  
 112          $contextlevel = 'module';
 113          $instanceid = $cm->id;
 114          $component = 'mod_data';
 115          $itemid = $recordid;
 116          $area = 'database_entry';
 117          $page = 0;
 118  
 119          $result = core_comment_external::get_comments($contextlevel, $instanceid, $component, $itemid, $area, $page);
 120          // We need to execute the return values cleaning process to simulate the web service server.
 121          $result = external_api::clean_returnvalue(
 122              core_comment_external::get_comments_returns(), $result);
 123  
 124          $this->assertCount(0, $result['warnings']);
 125          $this->assertCount(2, $result['comments']);
 126  
 127          $this->assertEquals($user->id, $result['comments'][0]['userid']);
 128          $this->assertEquals($user->id, $result['comments'][1]['userid']);
 129  
 130          $this->assertEquals($cmtid2, $result['comments'][0]['id']);
 131          $this->assertEquals($cmtid1, $result['comments'][1]['id']);
 132      }
 133  }


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