Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 401 and 402] [Versions 401 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   * Unit tests for blog external API.
  19   *
  20   * @package    core_blog
  21   * @copyright  2018 Juan Leyva
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core_blog\external;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  global $CFG;
  30  require_once($CFG->dirroot . '/blog/locallib.php');
  31  require_once($CFG->dirroot . '/blog/lib.php');
  32  
  33  /**
  34   * Unit tests for blog external API.
  35   *
  36   * @package    core_blog
  37   * @copyright  2018 Juan Leyva
  38   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class external_test extends \advanced_testcase {
  41  
  42      private $courseid;
  43      private $cmid;
  44      private $userid;
  45      private $groupid;
  46      private $tagid;
  47      private $postid;
  48  
  49      protected function setUp(): void {
  50          global $DB, $CFG;
  51          parent::setUp();
  52  
  53          $this->resetAfterTest();
  54  
  55          // Create default course.
  56          $course = $this->getDataGenerator()->create_course(array('category' => 1, 'shortname' => 'ANON'));
  57          $this->assertNotEmpty($course);
  58          $page = $this->getDataGenerator()->create_module('page', array('course' => $course->id));
  59          $this->assertNotEmpty($page);
  60  
  61          // Create default user.
  62          $user = $this->getDataGenerator()->create_user(array(
  63                  'username' => 'testuser',
  64                  'firstname' => 'Jimmy',
  65                  'lastname' => 'Kinnon'
  66          ));
  67          // Enrol user.
  68          $this->getDataGenerator()->enrol_user($user->id, $course->id);
  69  
  70          $group = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
  71          $this->getDataGenerator()->create_group_member(array('groupid' => $group->id, 'userid' => $user->id));
  72  
  73          // Create default post.
  74          $post = new \stdClass();
  75          $post->userid = $user->id;
  76          $post->courseid = $course->id;
  77          $post->groupid = $group->id;
  78          $post->content = 'test post content text';
  79          $post->module = 'blog';
  80          $post->id = $DB->insert_record('post', $post);
  81  
  82          \core_tag_tag::set_item_tags('core', 'post', $post->id, \context_user::instance($user->id), array('tag1'));
  83          $tagid = $DB->get_field('tag', 'id', array('name' => 'tag1'));
  84  
  85          // Grab important ids.
  86          $this->courseid = $course->id;
  87          $this->cmid = $page->cmid;
  88          $this->userid  = $user->id;
  89          $this->groupid  = $group->id;
  90          $this->tagid  = $tagid;
  91          $this->postid = $post->id;
  92          $this->publishstate = 'site';   // To be override in tests.
  93  
  94          // Set default blog level.
  95          $CFG->bloglevel = BLOG_SITE_LEVEL;
  96      }
  97  
  98      /**
  99       * Get global public entries even for not authenticated users.
 100       * We get the entry since is public.
 101       */
 102      public function test_get_public_entries_global_level_by_non_logged_users() {
 103          global $CFG, $DB;
 104  
 105          $CFG->bloglevel = BLOG_GLOBAL_LEVEL;
 106          $CFG->forcelogin = 0;
 107          // Set current entry global.
 108          $DB->set_field('post', 'publishstate', 'public', array('id' => $this->postid));
 109  
 110          $result = \core_blog\external::get_entries();
 111          $result = \external_api::clean_returnvalue(\core_blog\external::get_entries_returns(), $result);
 112          $this->assertCount(1, $result['entries']);
 113          $this->assertCount(1, $result['entries'][0]['tags']);
 114          $this->assertEquals('tag1', $result['entries'][0]['tags'][0]['rawname']);
 115  
 116          $this->assertEquals($this->postid, $result['entries'][0]['id']);
 117      }
 118  
 119      /**
 120       * Get global public entries even for not authenticated users in closed site.
 121       */
 122      public function test_get_public_entries_global_level_by_non_logged_users_closed_site() {
 123          global $CFG, $DB;
 124  
 125          $CFG->bloglevel = BLOG_GLOBAL_LEVEL;
 126          $CFG->forcelogin = 1;
 127          // Set current entry global.
 128          $DB->set_field('post', 'publishstate', 'public', array('id' => $this->postid));
 129  
 130          $this->expectException('\moodle_exception');
 131          \core_blog\external::get_entries();
 132      }
 133  
 134      /**
 135       * Get global public entries for guest users.
 136       * We get the entry since is public.
 137       */
 138      public function test_get_public_entries_global_level_by_guest_users() {
 139          global $CFG, $DB;
 140  
 141          $CFG->bloglevel = BLOG_GLOBAL_LEVEL;
 142          // Set current entry global.
 143          $DB->set_field('post', 'publishstate', 'public', array('id' => $this->postid));
 144  
 145          $this->setGuestUser();
 146          $result = \core_blog\external::get_entries();
 147          $result = \external_api::clean_returnvalue(\core_blog\external::get_entries_returns(), $result);
 148          $this->assertCount(1, $result['entries']);
 149          $this->assertCount(1, $result['entries'][0]['tags']);
 150          $this->assertEquals('tag1', $result['entries'][0]['tags'][0]['rawname']);
 151  
 152          $this->assertEquals($this->postid, $result['entries'][0]['id']);
 153      }
 154  
 155      /**
 156       * Get global not public entries even for not authenticated users withouth being authenticated.
 157       * We don't get any because they are not public (restricted to site users).
 158       */
 159      public function test_get_not_public_entries_global_level_by_non_logged_users() {
 160          global $CFG;
 161  
 162          $CFG->bloglevel = BLOG_GLOBAL_LEVEL;
 163  
 164          $result = \core_blog\external::get_entries();
 165          $result = \external_api::clean_returnvalue(\core_blog\external::get_entries_returns(), $result);
 166          $this->assertCount(0, $result['entries']);
 167      }
 168  
 169      /**
 170       * Get global not public entries users being guest.
 171       * We don't get any because they are not public (restricted to real site users).
 172       */
 173      public function test_get_not_public_entries_global_level_by_guest_user() {
 174          global $CFG;
 175  
 176          $CFG->bloglevel = BLOG_GLOBAL_LEVEL;
 177  
 178          $this->setGuestUser();
 179          $result = \core_blog\external::get_entries();
 180          $result = \external_api::clean_returnvalue(\core_blog\external::get_entries_returns(), $result);
 181          $this->assertCount(0, $result['entries']);
 182      }
 183  
 184      /**
 185       * Get site not public entries for not authenticated users.
 186       * We don't get any because they are not public (restricted to real site users).
 187       */
 188      public function test_get_not_public_entries_site_level_by_non_logged_users() {
 189          $this->expectException('require_login_exception'); // In this case we get a security exception.
 190          $result = \core_blog\external::get_entries();
 191      }
 192  
 193      /**
 194       * Get site not public entries for guest users.
 195       * We don't get any because they are not public (restricted to real site users).
 196       */
 197      public function test_get_not_public_entries_site_level_by_guest_users() {
 198  
 199          $this->setGuestUser();
 200          $result = \core_blog\external::get_entries();
 201          $result = \external_api::clean_returnvalue(\core_blog\external::get_entries_returns(), $result);
 202          $this->assertCount(0, $result['entries']);
 203      }
 204  
 205      /**
 206       * Get site entries at site level by system users.
 207       */
 208      public function test_get_site_entries_site_level_by_normal_users() {
 209  
 210          $this->setUser($this->userid);
 211          $result = \core_blog\external::get_entries();
 212          $result = \external_api::clean_returnvalue(\core_blog\external::get_entries_returns(), $result);
 213          $this->assertCount(1, $result['entries']);
 214          $this->assertEquals($this->postid, $result['entries'][0]['id']);
 215      }
 216  
 217      /**
 218       * Get draft site entries by authors.
 219       */
 220      public function test_get_draft_entries_site_level_by_author_users() {
 221          global $DB;
 222  
 223          // Set current entry global.
 224          $DB->set_field('post', 'publishstate', 'draft', array('id' => $this->postid));
 225  
 226          $this->setUser($this->userid);
 227          $result = \core_blog\external::get_entries();
 228          $result = \external_api::clean_returnvalue(\core_blog\external::get_entries_returns(), $result);
 229          $this->assertCount(1, $result['entries']);
 230          $this->assertEquals($this->postid, $result['entries'][0]['id']);
 231      }
 232  
 233      /**
 234       * Get draft site entries by not authors.
 235       */
 236      public function test_get_draft_entries_site_level_by_not_author_users() {
 237          global $DB;
 238  
 239          // Set current entry global.
 240          $DB->set_field('post', 'publishstate', 'draft', array('id' => $this->postid));
 241          $user = $this->getDataGenerator()->create_user();
 242  
 243          $this->setUser($user);
 244          $result = \core_blog\external::get_entries();
 245          $result = \external_api::clean_returnvalue(\core_blog\external::get_entries_returns(), $result);
 246          $this->assertCount(0, $result['entries']);
 247      }
 248  
 249      /**
 250       * Get draft site entries by admin.
 251       */
 252      public function test_get_draft_entries_site_level_by_admin_users() {
 253          global $DB;
 254  
 255          // Set current entry global.
 256          $DB->set_field('post', 'publishstate', 'draft', array('id' => $this->postid));
 257          $user = $this->getDataGenerator()->create_user();
 258  
 259          $this->setAdminUser();
 260          $result = \core_blog\external::get_entries();
 261          $result = \external_api::clean_returnvalue(\core_blog\external::get_entries_returns(), $result);
 262          $this->assertCount(1, $result['entries']);
 263          $this->assertEquals($this->postid, $result['entries'][0]['id']);
 264      }
 265  
 266      /**
 267       * Get draft user entries by authors.
 268       */
 269      public function test_get_draft_entries_user_level_by_author_users() {
 270          global $CFG, $DB;
 271  
 272          $CFG->bloglevel = BLOG_USER_LEVEL;
 273          // Set current entry global.
 274          $DB->set_field('post', 'publishstate', 'draft', array('id' => $this->postid));
 275  
 276          $this->setUser($this->userid);
 277          $result = \core_blog\external::get_entries();
 278          $result = \external_api::clean_returnvalue(\core_blog\external::get_entries_returns(), $result);
 279          $this->assertCount(1, $result['entries']);
 280          $this->assertEquals($this->postid, $result['entries'][0]['id']);
 281      }
 282  
 283      /**
 284       * Get draft user entries by not authors.
 285       */
 286      public function test_get_draft_entries_user_level_by_not_author_users() {
 287          global $CFG, $DB;
 288  
 289          $CFG->bloglevel = BLOG_USER_LEVEL;
 290          // Set current entry global.
 291          $DB->set_field('post', 'publishstate', 'draft', array('id' => $this->postid));
 292          $user = $this->getDataGenerator()->create_user();
 293  
 294          $this->setUser($user);
 295          $result = \core_blog\external::get_entries();
 296          $result = \external_api::clean_returnvalue(\core_blog\external::get_entries_returns(), $result);
 297          $this->assertCount(0, $result['entries']);
 298      }
 299  
 300      /**
 301       * Get draft user entries by admin.
 302       */
 303      public function test_get_draft_entries_user_level_by_admin_users() {
 304          global $CFG, $DB;
 305  
 306          $CFG->bloglevel = BLOG_USER_LEVEL;
 307          // Set current entry global.
 308          $DB->set_field('post', 'publishstate', 'draft', array('id' => $this->postid));
 309          $user = $this->getDataGenerator()->create_user();
 310  
 311          $this->setAdminUser();
 312          $result = \core_blog\external::get_entries();
 313          $result = \external_api::clean_returnvalue(\core_blog\external::get_entries_returns(), $result);
 314          $this->assertCount(1, $result['entries']);
 315          $this->assertEquals($this->postid, $result['entries'][0]['id']);
 316      }
 317  
 318      /**
 319       * Test get all entries including testing pagination.
 320       */
 321      public function test_get_all_entries_including_pagination() {
 322          global $DB, $USER;
 323  
 324          $DB->set_field('post', 'publishstate', 'site', array('id' => $this->postid));
 325  
 326          // Create another entry.
 327          $this->setAdminUser();
 328          $newpost = new \stdClass();
 329          $newpost->userid = $USER->id;
 330          $newpost->content = 'test post content text';
 331          $newpost->module = 'blog';
 332          $newpost->publishstate = 'site';
 333          $newpost->created = time() + HOURSECS;
 334          $newpost->lastmodified = time() + HOURSECS;
 335          $newpost->id = $DB->insert_record('post', $newpost);
 336  
 337          $this->setUser($this->userid);
 338          $result = \core_blog\external::get_entries();
 339          $result = \external_api::clean_returnvalue(\core_blog\external::get_entries_returns(), $result);
 340          $this->assertCount(2, $result['entries']);
 341          $this->assertEquals(2, $result['totalentries']);
 342          $this->assertCount(0, $result['entries'][0]['tags']);
 343          $this->assertCount(1, $result['entries'][1]['tags']);
 344          $this->assertEquals('tag1', $result['entries'][1]['tags'][0]['rawname']);
 345  
 346          $result = \core_blog\external::get_entries(array(), 0, 1);
 347          $result = \external_api::clean_returnvalue(\core_blog\external::get_entries_returns(), $result);
 348          $this->assertCount(1, $result['entries']);
 349          $this->assertEquals(2, $result['totalentries']);
 350          $this->assertEquals($newpost->id, $result['entries'][0]['id']);
 351  
 352          $result = \core_blog\external::get_entries(array(), 1, 1);
 353          $result = \external_api::clean_returnvalue(\core_blog\external::get_entries_returns(), $result);
 354          $this->assertCount(1, $result['entries']);
 355          $this->assertEquals(2, $result['totalentries']);
 356          $this->assertEquals($this->postid, $result['entries'][0]['id']);
 357      }
 358  
 359      /**
 360       * Test get entries filtering by course.
 361       */
 362      public function test_get_entries_filtering_by_course() {
 363          global $CFG, $DB;
 364  
 365          $DB->set_field('post', 'publishstate', 'site', array('id' => $this->postid));
 366  
 367          $this->setAdminUser();
 368          $coursecontext = \context_course::instance($this->courseid);
 369          $anothercourse = $this->getDataGenerator()->create_course();
 370  
 371          // Add blog associations with a course.
 372          $blog = new \blog_entry($this->postid);
 373          $blog->add_association($coursecontext->id);
 374  
 375          // There is one entry associated with a course.
 376          $result = \core_blog\external::get_entries(array(array('name' => 'courseid', 'value' => $this->courseid)));
 377          $result = \external_api::clean_returnvalue(\core_blog\external::get_entries_returns(), $result);
 378          $this->assertCount(1, $result['entries']);
 379          $this->assertCount(1, $result['entries'][0]['tags']);
 380          $this->assertEquals('tag1', $result['entries'][0]['tags'][0]['rawname']);
 381  
 382          // There is no entry associated with a wrong course.
 383          $result = \core_blog\external::get_entries(array(array('name' => 'courseid', 'value' => $anothercourse->id)));
 384          $result = \external_api::clean_returnvalue(\core_blog\external::get_entries_returns(), $result);
 385          $this->assertCount(0, $result['entries']);
 386  
 387          // There is no entry associated with a module.
 388          $result = \core_blog\external::get_entries(array(array('name' => 'cmid', 'value' => $this->cmid)));
 389          $result = \external_api::clean_returnvalue(\core_blog\external::get_entries_returns(), $result);
 390          $this->assertCount(0, $result['entries']);
 391      }
 392  
 393      /**
 394       * Test get entries filtering by module.
 395       */
 396      public function test_get_entries_filtering_by_module() {
 397          global $CFG, $DB;
 398  
 399          $DB->set_field('post', 'publishstate', 'site', array('id' => $this->postid));
 400  
 401          $this->setAdminUser();
 402          $coursecontext = \context_course::instance($this->courseid);
 403          $contextmodule = \context_module::instance($this->cmid);
 404          $anothermodule = $this->getDataGenerator()->create_module('page', array('course' => $this->courseid));
 405  
 406          // Add blog associations with a module.
 407          $blog = new \blog_entry($this->postid);
 408          $blog->add_association($contextmodule->id);
 409  
 410          // There is no entry associated with a course.
 411          $result = \core_blog\external::get_entries(array(array('name' => 'courseid', 'value' => $this->courseid)));
 412          $result = \external_api::clean_returnvalue(\core_blog\external::get_entries_returns(), $result);
 413          $this->assertCount(0, $result['entries']);
 414  
 415          // There is one entry associated with a module.
 416          $result = \core_blog\external::get_entries(array(array('name' => 'cmid', 'value' => $this->cmid)));
 417          $result = \external_api::clean_returnvalue(\core_blog\external::get_entries_returns(), $result);
 418          $this->assertCount(1, $result['entries']);
 419  
 420          // There is no entry associated with a wrong module.
 421          $result = \core_blog\external::get_entries(array(array('name' => 'cmid', 'value' => $anothermodule->cmid)));
 422          $result = \external_api::clean_returnvalue(\core_blog\external::get_entries_returns(), $result);
 423          $this->assertCount(0, $result['entries']);
 424      }
 425  
 426      /**
 427       * Test get entries filtering by author.
 428       */
 429      public function test_get_entries_filtering_by_author() {
 430          $this->setAdminUser();
 431          // Filter by author.
 432          $result = \core_blog\external::get_entries(array(array('name' => 'userid', 'value' => $this->userid)));
 433          $result = \external_api::clean_returnvalue(\core_blog\external::get_entries_returns(), $result);
 434          $this->assertCount(1, $result['entries']);
 435          // No author.
 436          $anotheruser = $this->getDataGenerator()->create_user();
 437          $result = \core_blog\external::get_entries(array(array('name' => 'userid', 'value' => $anotheruser->id)));
 438          $result = \external_api::clean_returnvalue(\core_blog\external::get_entries_returns(), $result);
 439          $this->assertCount(0, $result['entries']);
 440      }
 441  
 442      /**
 443       * Test get entries filtering by entry.
 444       */
 445      public function test_get_entries_filtering_by_entry() {
 446          $this->setAdminUser();
 447          // Filter by correct entry.
 448          $result = \core_blog\external::get_entries(array(array('name' => 'entryid', 'value' => $this->postid)));
 449          $result = \external_api::clean_returnvalue(\core_blog\external::get_entries_returns(), $result);
 450          $this->assertCount(1, $result['entries']);
 451          // Non-existent entry.
 452          $this->expectException('\moodle_exception');
 453          $result = \core_blog\external::get_entries(array(array('name' => 'entryid', 'value' => -1)));
 454      }
 455  
 456      /**
 457       * Test get entries filtering by search.
 458       */
 459      public function test_get_entries_filtering_by_search() {
 460          $this->setAdminUser();
 461          // Filter by correct search.
 462          $result = \core_blog\external::get_entries(array(array('name' => 'search', 'value' => 'test')));
 463          $result = \external_api::clean_returnvalue(\core_blog\external::get_entries_returns(), $result);
 464          $this->assertCount(1, $result['entries']);
 465          // Non-existent search.
 466          $result = \core_blog\external::get_entries(array(array('name' => 'search', 'value' => 'abc')));
 467          $result = \external_api::clean_returnvalue(\core_blog\external::get_entries_returns(), $result);
 468          $this->assertCount(0, $result['entries']);
 469      }
 470  
 471      /**
 472       * Test get entries filtering by tag.
 473       */
 474      public function test_get_entries_filtering_by_tag() {
 475          $this->setAdminUser();
 476          // Filter by correct tag.
 477          $result = \core_blog\external::get_entries(array(array('name' => 'tag', 'value' => 'tag1')));
 478          $result = \external_api::clean_returnvalue(\core_blog\external::get_entries_returns(), $result);
 479          $this->assertCount(1, $result['entries']);
 480          // Create tag.
 481          $tag = $this->getDataGenerator()->create_tag(array('userid' => $this->userid, 'name' => 'tag2',
 482              'isstandard' => 1));
 483  
 484          $result = \core_blog\external::get_entries(array(array('name' => 'tag', 'value' => 'tag2')));
 485          $result = \external_api::clean_returnvalue(\core_blog\external::get_entries_returns(), $result);
 486          $this->assertCount(0, $result['entries']);
 487      }
 488  
 489      /**
 490       * Test get entries filtering by tag id.
 491       */
 492      public function test_get_entries_filtering_by_tagid() {
 493          $this->setAdminUser();
 494          // Filter by correct tag.
 495          $result = \core_blog\external::get_entries(array(array('name' => 'tagid', 'value' => $this->tagid)));
 496          $result = \external_api::clean_returnvalue(\core_blog\external::get_entries_returns(), $result);
 497          $this->assertCount(1, $result['entries']);
 498          // Non-existent tag.
 499  
 500          // Create tag.
 501          $tag = $this->getDataGenerator()->create_tag(array('userid' => $this->userid, 'name' => 'tag2',
 502              'isstandard' => 1));
 503  
 504          $result = \core_blog\external::get_entries(array(array('name' => 'tagid', 'value' => $tag->id)));
 505          $result = \external_api::clean_returnvalue(\core_blog\external::get_entries_returns(), $result);
 506          $this->assertCount(0, $result['entries']);
 507      }
 508  
 509      /**
 510       * Test get entries filtering by group.
 511       */
 512      public function test_get_entries_filtering_by_group() {
 513          $this->setAdminUser();
 514          // Add blog associations with a course.
 515          $coursecontext = \context_course::instance($this->courseid);
 516          $blog = new \blog_entry($this->postid);
 517          $blog->add_association($coursecontext->id);
 518  
 519          // Filter by correct group.
 520          $result = \core_blog\external::get_entries(array(array('name' => 'groupid', 'value' => $this->groupid)));
 521          $result = \external_api::clean_returnvalue(\core_blog\external::get_entries_returns(), $result);
 522          $this->assertCount(1, $result['entries']);
 523          // Non-existent group.
 524          $anotheruser = $this->getDataGenerator()->create_user();
 525          $this->expectException('\moodle_exception');
 526          \core_blog\external::get_entries(array(array('name' => 'groupid', 'value' => -1)));
 527      }
 528  
 529      /**
 530       * Test get entries multiple filter.
 531       */
 532      public function test_get_entries_multiple_filter() {
 533          $this->setAdminUser();
 534          // Add blog associations with a course.
 535          $coursecontext = \context_course::instance($this->courseid);
 536          $blog = new \blog_entry($this->postid);
 537          $blog->add_association($coursecontext->id);
 538  
 539          $result = \core_blog\external::get_entries(array(
 540              array('name' => 'tagid', 'value' => $this->tagid),
 541              array('name' => 'userid', 'value' => $this->userid),
 542          ));
 543          $result = \external_api::clean_returnvalue(\core_blog\external::get_entries_returns(), $result);
 544          $this->assertCount(1, $result['entries']);
 545  
 546          // Non-existent multiple filter.
 547          $result = \core_blog\external::get_entries(array(
 548              array('name' => 'search', 'value' => 'www'),
 549              array('name' => 'userid', 'value' => $this->userid),
 550          ));
 551          $result = \external_api::clean_returnvalue(\core_blog\external::get_entries_returns(), $result);
 552          $this->assertCount(0, $result['entries']);
 553      }
 554  
 555      /**
 556       * Test get entries filtering by invalid_filter.
 557       */
 558      public function test_get_entries_filtering_by_invalid_filter() {
 559          $this->setAdminUser();
 560          // Filter by incorrect filter.
 561          $this->expectException('\moodle_exception');
 562          $result = \core_blog\external::get_entries(array(array('name' => 'zzZZzz', 'value' => 'wwWWww')));
 563      }
 564  
 565      /**
 566       * Test get entries when blog is disabled.
 567       */
 568      public function test_get_entries_blog_disabled() {
 569          global $CFG;
 570  
 571          $this->setAdminUser();
 572          $CFG->enableblogs = 0;
 573          // Filter by incorrect filter.
 574          $this->expectException('\moodle_exception');
 575          $result = \core_blog\external::get_entries(array(array('name' => 'zzZZzz', 'value' => 'wwWWww')));
 576      }
 577  
 578      /**
 579       * Test view_blog_entries without filter.
 580       */
 581      public function test_view_blog_entries_without_filtering() {
 582          // Test user with full capabilities.
 583          $this->setUser($this->userid);
 584          // Trigger and capture the event.
 585          $sink = $this->redirectEvents();
 586          $result = \core_blog\external::view_entries();
 587          $result = \external_api::clean_returnvalue(\core_blog\external::view_entries_returns(), $result);
 588  
 589          $events = $sink->get_events();
 590          $this->assertCount(1, $events);
 591          $event = array_shift($events);
 592          // Checking that the event contains the expected values (empty, no filtering done).
 593          $this->assertInstanceOf('\core\event\blog_entries_viewed', $event);
 594          $this->assertEmpty($event->get_data()['relateduserid']);
 595          $this->assertEmpty($event->get_data()['other']['entryid']);
 596          $this->assertEmpty($event->get_data()['other']['tagid']);
 597          $this->assertEmpty($event->get_data()['other']['userid']);
 598          $this->assertEmpty($event->get_data()['other']['modid']);
 599          $this->assertEmpty($event->get_data()['other']['groupid']);
 600          $this->assertEmpty($event->get_data()['other']['search']);
 601          $this->assertEmpty($event->get_data()['other']['courseid']);
 602          $this->assertEventContextNotUsed($event);
 603          $this->assertNotEmpty($event->get_name());
 604      }
 605  
 606      /**
 607       * Test view_blog_entries doing filtering.
 608       */
 609      public function test_view_blog_entries_with_filtering() {
 610          // Test user with full capabilities.
 611          $this->setUser($this->userid);
 612          // Trigger and capture the event.
 613          $sink = $this->redirectEvents();
 614          $result = \core_blog\external::view_entries(array(
 615              array('name' => 'tagid', 'value' => $this->tagid),
 616              array('name' => 'userid', 'value' => $this->userid),
 617          ));
 618          $result = \external_api::clean_returnvalue(\core_blog\external::view_entries_returns(), $result);
 619  
 620          $events = $sink->get_events();
 621          $this->assertCount(1, $events);
 622          $event = array_shift($events);
 623          // Checking that the event contains the expected values (filter by user and tag).
 624          $this->assertInstanceOf('\core\event\blog_entries_viewed', $event);
 625          $this->assertEquals($this->userid, $event->get_data()['relateduserid']);
 626          $this->assertEmpty($event->get_data()['other']['entryid']);
 627          $this->assertEquals($this->tagid, $event->get_data()['other']['tagid']);
 628          $this->assertEquals($this->userid, $event->get_data()['other']['userid']);
 629          $this->assertEmpty($event->get_data()['other']['modid']);
 630          $this->assertEmpty($event->get_data()['other']['groupid']);
 631          $this->assertEmpty($event->get_data()['other']['search']);
 632          $this->assertEmpty($event->get_data()['other']['courseid']);
 633          $this->assertEventContextNotUsed($event);
 634          $this->assertNotEmpty($event->get_name());
 635      }
 636  }