Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

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

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