Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 39 and 311]

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