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] [Versions 39 and 310]

   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
  19   *
  20   * @package    core_blog
  21   * @category   phpunit
  22   * @copyright  2009 Nicolas Connault
  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->dirroot . '/blog/locallib.php');
  30  require_once($CFG->dirroot . '/blog/lib.php');
  31  
  32  /**
  33   * Test functions that rely on the DB tables
  34   */
  35  class core_blog_lib_testcase extends advanced_testcase {
  36  
  37      private $courseid;
  38      private $cmid;
  39      private $groupid;
  40      private $userid;
  41      private $tagid;
  42      private $postid;
  43  
  44      protected function setUp(): void {
  45          global $DB;
  46          parent::setUp();
  47  
  48          $this->resetAfterTest();
  49  
  50          // Create default course.
  51          $course = $this->getDataGenerator()->create_course(array('category' => 1, 'shortname' => 'ANON'));
  52          $this->assertNotEmpty($course);
  53          $page = $this->getDataGenerator()->create_module('page', array('course' => $course->id));
  54          $this->assertNotEmpty($page);
  55  
  56          // Create default group.
  57          $group = new stdClass();
  58          $group->courseid = $course->id;
  59          $group->name = 'ANON';
  60          $group->id = $DB->insert_record('groups', $group);
  61  
  62          // Create default user.
  63          $user = $this->getDataGenerator()->create_user(array(
  64                  'username' => 'testuser',
  65                  'firstname' => 'Jimmy',
  66                  'lastname' => 'Kinnon'
  67          ));
  68  
  69          // Create default tag.
  70          $tag = $this->getDataGenerator()->create_tag(array('userid' => $user->id,
  71              'rawname' => 'Testtagname', 'isstandard' => 1));
  72  
  73          // Create default post.
  74          $post = new stdClass();
  75          $post->userid = $user->id;
  76          $post->groupid = $group->id;
  77          $post->content = 'test post content text';
  78          $post->module = 'blog';
  79          $post->id = $DB->insert_record('post', $post);
  80  
  81          // Grab important ids.
  82          $this->courseid = $course->id;
  83          $this->cmid = $page->cmid;
  84          $this->groupid  = $group->id;
  85          $this->userid  = $user->id;
  86          $this->tagid  = $tag->id;
  87          $this->postid = $post->id;
  88      }
  89  
  90  
  91      public function test_overrides() {
  92          global $SITE;
  93  
  94          // Try all the filters at once: Only the entry filter is active.
  95          $filters = array('site' => $SITE->id, 'course' => $this->courseid, 'module' => $this->cmid,
  96              'group' => $this->groupid, 'user' => $this->userid, 'tag' => $this->tagid, 'entry' => $this->postid);
  97          $bloglisting = new blog_listing($filters);
  98          $this->assertFalse(array_key_exists('site', $bloglisting->filters));
  99          $this->assertFalse(array_key_exists('course', $bloglisting->filters));
 100          $this->assertFalse(array_key_exists('module', $bloglisting->filters));
 101          $this->assertFalse(array_key_exists('group', $bloglisting->filters));
 102          $this->assertFalse(array_key_exists('user', $bloglisting->filters));
 103          $this->assertFalse(array_key_exists('tag', $bloglisting->filters));
 104          $this->assertTrue(array_key_exists('entry', $bloglisting->filters));
 105  
 106          // Again, but without the entry filter: This time, the tag, user and module filters are active.
 107          $filters = array('site' => $SITE->id, 'course' => $this->courseid, 'module' => $this->cmid,
 108              'group' => $this->groupid, 'user' => $this->userid, 'tag' => $this->postid);
 109          $bloglisting = new blog_listing($filters);
 110          $this->assertFalse(array_key_exists('site', $bloglisting->filters));
 111          $this->assertFalse(array_key_exists('course', $bloglisting->filters));
 112          $this->assertFalse(array_key_exists('group', $bloglisting->filters));
 113          $this->assertTrue(array_key_exists('module', $bloglisting->filters));
 114          $this->assertTrue(array_key_exists('user', $bloglisting->filters));
 115          $this->assertTrue(array_key_exists('tag', $bloglisting->filters));
 116  
 117          // We should get the same result by removing the 3 inactive filters: site, course and group.
 118          $filters = array('module' => $this->cmid, 'user' => $this->userid, 'tag' => $this->tagid);
 119          $bloglisting = new blog_listing($filters);
 120          $this->assertFalse(array_key_exists('site', $bloglisting->filters));
 121          $this->assertFalse(array_key_exists('course', $bloglisting->filters));
 122          $this->assertFalse(array_key_exists('group', $bloglisting->filters));
 123          $this->assertTrue(array_key_exists('module', $bloglisting->filters));
 124          $this->assertTrue(array_key_exists('user', $bloglisting->filters));
 125          $this->assertTrue(array_key_exists('tag', $bloglisting->filters));
 126  
 127      }
 128  
 129      // The following series of 'test_blog..' functions correspond to the blog_get_headers() function within blog/lib.php.
 130      // Some cases are omitted due to the optional_param variables used.
 131  
 132      public function test_blog_get_headers_case_1() {
 133          global $CFG, $PAGE, $OUTPUT;
 134          $blogheaders = blog_get_headers();
 135          $this->assertEquals($blogheaders['heading'], get_string('siteblogheading', 'blog'));
 136      }
 137  
 138      public function test_blog_get_headers_case_6() {
 139          global $CFG, $PAGE, $OUTPUT;
 140          $blogheaders = blog_get_headers($this->courseid, null, $this->userid);
 141          $this->assertNotEquals($blogheaders['heading'], '');
 142      }
 143  
 144      public function test_blog_get_headers_case_7() {
 145          global $CFG, $PAGE, $OUTPUT;
 146          $blogheaders = blog_get_headers(null, $this->groupid);
 147          $this->assertNotEquals($blogheaders['heading'], '');
 148      }
 149  
 150      public function test_blog_get_headers_case_10() {
 151          global $CFG, $PAGE, $OUTPUT;
 152          $blogheaders = blog_get_headers($this->courseid);
 153          $this->assertNotEquals($blogheaders['heading'], '');
 154      }
 155  
 156      /**
 157       * Tests the core_blog_myprofile_navigation() function.
 158       */
 159      public function test_core_blog_myprofile_navigation() {
 160          global $USER;
 161  
 162          // Set up the test.
 163          $tree = new \core_user\output\myprofile\tree();
 164          $this->setAdminUser();
 165          $iscurrentuser = true;
 166          $course = null;
 167  
 168          // Enable blogs.
 169          set_config('enableblogs', true);
 170  
 171          // Check the node tree is correct.
 172          core_blog_myprofile_navigation($tree, $USER, $iscurrentuser, $course);
 173          $reflector = new ReflectionObject($tree);
 174          $nodes = $reflector->getProperty('nodes');
 175          $nodes->setAccessible(true);
 176          $this->assertArrayHasKey('blogs', $nodes->getValue($tree));
 177      }
 178  
 179      /**
 180       * Tests the core_blog_myprofile_navigation() function as a guest.
 181       */
 182      public function test_core_blog_myprofile_navigation_as_guest() {
 183          global $USER;
 184  
 185          // Set up the test.
 186          $tree = new \core_user\output\myprofile\tree();
 187          $iscurrentuser = false;
 188          $course = null;
 189  
 190          // Set user as guest.
 191          $this->setGuestUser();
 192  
 193          // Check the node tree is correct.
 194          core_blog_myprofile_navigation($tree, $USER, $iscurrentuser, $course);
 195          $reflector = new ReflectionObject($tree);
 196          $nodes = $reflector->getProperty('nodes');
 197          $nodes->setAccessible(true);
 198          $this->assertArrayNotHasKey('blogs', $nodes->getValue($tree));
 199      }
 200  
 201      /**
 202       * Tests the core_blog_myprofile_navigation() function when blogs are disabled.
 203       */
 204      public function test_core_blog_myprofile_navigation_blogs_disabled() {
 205          global $USER;
 206  
 207          // Set up the test.
 208          $tree = new \core_user\output\myprofile\tree();
 209          $this->setAdminUser();
 210          $iscurrentuser = false;
 211          $course = null;
 212  
 213          // Disable blogs.
 214          set_config('enableblogs', false);
 215  
 216          // Check the node tree is correct.
 217          core_blog_myprofile_navigation($tree, $USER, $iscurrentuser, $course);
 218          $reflector = new ReflectionObject($tree);
 219          $nodes = $reflector->getProperty('nodes');
 220          $nodes->setAccessible(true);
 221          $this->assertArrayNotHasKey('blogs', $nodes->getValue($tree));
 222      }
 223  
 224      public function test_blog_get_listing_course() {
 225          $this->setAdminUser();
 226          $coursecontext = context_course::instance($this->courseid);
 227          $anothercourse = $this->getDataGenerator()->create_course();
 228  
 229          // Add blog associations with a course.
 230          $blog = new blog_entry($this->postid);
 231          $blog->add_association($coursecontext->id);
 232  
 233          // There is one entry associated with a course.
 234          $bloglisting = new blog_listing(array('course' => $this->courseid));
 235          $this->assertCount(1, $bloglisting->get_entries());
 236  
 237          // There is no entry associated with a wrong course.
 238          $bloglisting = new blog_listing(array('course' => $anothercourse->id));
 239          $this->assertCount(0, $bloglisting->get_entries());
 240  
 241          // There is no entry associated with a module.
 242          $bloglisting = new blog_listing(array('module' => $this->cmid));
 243          $this->assertCount(0, $bloglisting->get_entries());
 244  
 245          // There is one entry associated with a site (id is ignored).
 246          $bloglisting = new blog_listing(array('site' => 12345));
 247          $this->assertCount(1, $bloglisting->get_entries());
 248  
 249          // There is one entry associated with course context.
 250          $bloglisting = new blog_listing(array('context' => $coursecontext->id));
 251          $this->assertCount(1, $bloglisting->get_entries());
 252      }
 253  
 254      public function test_blog_get_listing_module() {
 255          $this->setAdminUser();
 256          $coursecontext = context_course::instance($this->courseid);
 257          $contextmodule = context_module::instance($this->cmid);
 258          $anothermodule = $this->getDataGenerator()->create_module('page', array('course' => $this->courseid));
 259  
 260          // Add blog associations with a course.
 261          $blog = new blog_entry($this->postid);
 262          $blog->add_association($contextmodule->id);
 263  
 264          // There is no entry associated with a course.
 265          $bloglisting = new blog_listing(array('course' => $this->courseid));
 266          $this->assertCount(0, $bloglisting->get_entries());
 267  
 268          // There is one entry associated with a module.
 269          $bloglisting = new blog_listing(array('module' => $this->cmid));
 270          $this->assertCount(1, $bloglisting->get_entries());
 271  
 272          // There is no entry associated with a wrong module.
 273          $bloglisting = new blog_listing(array('module' => $anothermodule->cmid));
 274          $this->assertCount(0, $bloglisting->get_entries());
 275  
 276          // There is one entry associated with a site (id is ignored).
 277          $bloglisting = new blog_listing(array('site' => 12345));
 278          $this->assertCount(1, $bloglisting->get_entries());
 279  
 280          // There is one entry associated with course context (module is a subcontext of a course).
 281          $bloglisting = new blog_listing(array('context' => $coursecontext->id));
 282          $this->assertCount(1, $bloglisting->get_entries());
 283      }
 284  }
 285