Search moodle.org's
Developer Documentation


  • Bug fixes for general core bugs in 2.8.x ended 9 November 2015 (12 months).
  • Bug fixes for security issues in 2.8.x ended 9 May 2016 (18 months).
  • minimum PHP 5.4.4 (always use latest PHP 5.4.x or 5.5.x on Windows - http://windows.php.net/download/), PHP 7 is NOT supported
  • Differences Between: [Versions 28 and 30] [Versions 28 and 31] [Versions 28 and 32] [Versions 28 and 33] [Versions 28 and 34] [Versions 28 and 35] [Versions 28 and 36] [Versions 28 and 37]

       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   * Events tests.
      19   *
      20   * @package core_tag
      21   * @category test
      22   * @copyright 2014 Mark Nelson <markn@moodle.com>
      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  
      30  require_once($CFG->dirroot . '/tag/lib.php');
      31  require_once($CFG->dirroot . '/tag/coursetagslib.php');
      32  
      33  // Used to create a wiki page to tag.
      34  require_once($CFG->dirroot . '/mod/wiki/locallib.php');
      35  
      36  class core_tag_events_testcase extends advanced_testcase {
      37  
      38      /**
      39       * Test set up.
      40       *
      41       * This is executed before running any test in this file.
      42       */
      43      public function setUp() {
      44          $this->resetAfterTest();
      45      }
      46  
      47      /**
      48       * Test the tag updated event.
      49       */
      50      public function test_tag_updated() {
      51          $this->setAdminUser();
      52  
      53          // Save the system context.
      54          $systemcontext = context_system::instance();
      55  
      56          // Create a tag we are going to update.
      57          $tag = $this->getDataGenerator()->create_tag();
      58  
      59          // Store the name before we change it.
      60          $oldname = $tag->name;
      61  
      62          // Trigger and capture the event when renaming a tag.
      63          $sink = $this->redirectEvents();
      64          tag_rename($tag->id, 'newname');
      65          // Update the tag's name since we have renamed it.
      66          $tag->name = 'newname';
      67          $events = $sink->get_events();
      68          $event = reset($events);
      69  
      70          // Check that the event data is valid.
      71          $this->assertInstanceOf('\core\event\tag_updated', $event);
      72          $this->assertEquals($systemcontext, $event->get_context());
      73          $expected = array(SITEID, 'tag', 'update', 'index.php?id=' . $tag->id, $oldname . '->'. $tag->name);
      74          $this->assertEventLegacyLogData($expected, $event);
      75  
      76          // Trigger and capture the event when setting the type of a tag.
      77          $sink = $this->redirectEvents();
      78          tag_type_set($tag->id, 'official');
      79          $events = $sink->get_events();
      80          $event = reset($events);
      81  
      82          // Check that the event data is valid.
      83          $this->assertInstanceOf('\core\event\tag_updated', $event);
      84          $this->assertEquals($systemcontext, $event->get_context());
      85          $expected = array(0, 'tag', 'update', 'index.php?id=' . $tag->id, $tag->name);
      86          $this->assertEventLegacyLogData($expected, $event);
      87  
      88          // Trigger and capture the event for setting the description of a tag.
      89          $sink = $this->redirectEvents();
      90          tag_description_set($tag->id, 'description', FORMAT_MOODLE);
      91          $events = $sink->get_events();
      92          $event = reset($events);
      93  
      94          // Check that the event data is valid.
      95          $this->assertInstanceOf('\core\event\tag_updated', $event);
      96          $this->assertEquals($systemcontext, $event->get_context());
      97          $expected = array(0, 'tag', 'update', 'index.php?id=' . $tag->id, $tag->name);
      98          $this->assertEventLegacyLogData($expected, $event);
      99      }
     100  
     101      /**
     102       * Test the tag added event.
     103       */
     104      public function test_tag_added() {
     105          global $DB;
     106  
     107          // Create a course to tag.
     108          $course = $this->getDataGenerator()->create_course();
     109  
     110          // Trigger and capture the event for tagging a course.
     111          $sink = $this->redirectEvents();
     112          tag_set('course', $course->id, array('A tag'), 'core', context_course::instance($course->id)->id);
     113          $events = $sink->get_events();
     114          $event = $events[1];
     115  
     116          // Check that the tag was added to the course and that the event data is valid.
     117          $this->assertEquals(1, $DB->count_records('tag_instance', array('component' => 'core')));
     118          $this->assertInstanceOf('\core\event\tag_added', $event);
     119          $this->assertEquals(context_course::instance($course->id), $event->get_context());
     120          $expected = array($course->id, 'coursetags', 'add', 'tag/search.php?query=A+tag', 'Course tagged');
     121          $this->assertEventLegacyLogData($expected, $event);
     122  
     123          // Create a question to tag.
     124          $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
     125          $cat = $questiongenerator->create_question_category();
     126          $question = $questiongenerator->create_question('shortanswer', null, array('category' => $cat->id));
     127  
     128          // Trigger and capture the event for tagging a question.
     129          $this->assertEquals(1, $DB->count_records('tag_instance'));
     130          $sink = $this->redirectEvents();
     131          tag_set('question', $question->id, array('A tag'), 'core_question', $cat->contextid);
     132          $events = $sink->get_events();
     133          $event = reset($events);
     134  
     135          // Check that the tag was added to the question and the event data is valid.
     136          $this->assertEquals(1, $DB->count_records('tag_instance', array('component' => 'core')));
     137          $this->assertInstanceOf('\core\event\tag_added', $event);
     138          $this->assertEquals(context_system::instance(), $event->get_context());
     139          $expected = null;
     140          $this->assertEventLegacyLogData($expected, $event);
     141      }
     142  
     143      /**
     144       * Test the tag removed event.
     145       */
     146      public function test_tag_removed() {
     147          global $DB;
     148  
     149          $this->setAdminUser();
     150  
     151          // Create a course to tag.
     152          $course = $this->getDataGenerator()->create_course();
     153  
     154          // Create a wiki page to tag.
     155          $wikigenerator = $this->getDataGenerator()->get_plugin_generator('mod_wiki');
     156          $wiki = $wikigenerator->create_instance(array('course' => $course->id));
     157          $subwikiid = wiki_add_subwiki($wiki->id, 0);
     158          $wikipageid = wiki_create_page($subwikiid, 'Title', FORMAT_HTML, '2');
     159  
     160          // Create the tag.
     161          $tag = $this->getDataGenerator()->create_tag();
     162  
     163          // Assign a tag to a course.
     164          tag_assign('course', $course->id, $tag->id, 1, 2, 'core', context_course::instance($course->id)->id);
     165  
     166          // Trigger and capture the event for untagging a course.
     167          $sink = $this->redirectEvents();
     168          coursetag_delete_keyword($tag->id, 2, $course->id);
     169          $events = $sink->get_events();
     170          $event = reset($events);
     171  
     172          // Check that the tag was removed from the course and the event data is valid.
     173          $this->assertEquals(0, $DB->count_records('tag_instance'));
     174          $this->assertInstanceOf('\core\event\tag_removed', $event);
     175          $this->assertEquals(context_course::instance($course->id), $event->get_context());
     176  
     177          // Create the tag.
     178          $tag = $this->getDataGenerator()->create_tag();
     179  
     180          // Assign a tag to a wiki this time.
     181          tag_assign('wiki_pages', $wikipageid, $tag->id, 1, 2, 'mod_wiki', context_module::instance($wiki->cmid)->id);
     182  
     183          // Trigger and capture the event for deleting this tag instance.
     184          $sink = $this->redirectEvents();
     185          tag_delete_instance('wiki_pages', $wikipageid, $tag->id);
     186          $events = $sink->get_events();
     187          $event = reset($events);
     188  
     189          // Check that tag was removed from the wiki page and the event data is valid.
     190          $this->assertEquals(0, $DB->count_records('tag_instance'));
     191          $this->assertInstanceOf('\core\event\tag_removed', $event);
     192          $this->assertEquals(context_module::instance($wiki->cmid), $event->get_context());
     193  
     194          // Create a tag again - the other would have been deleted since there were no more instances associated with it.
     195          $tag = $this->getDataGenerator()->create_tag();
     196  
     197          // Assign a tag to the wiki again.
     198          tag_assign('wiki_pages', $wikipageid, $tag->id, 1, 2, 'mod_wiki', context_module::instance($wiki->cmid)->id);
     199  
     200          // Now we want to delete this tag, and because there is only one tag instance
     201          // associated with it, it should get deleted as well.
     202          $sink = $this->redirectEvents();
     203          tag_delete($tag->id);
     204          $events = $sink->get_events();
     205          $event = reset($events);
     206  
     207          // Check that tag was removed from the wiki page and the event data is valid.
     208          $this->assertEquals(0, $DB->count_records('tag_instance'));
     209          $this->assertInstanceOf('\core\event\tag_removed', $event);
     210          $this->assertEquals(context_module::instance($wiki->cmid), $event->get_context());
     211  
     212          // Create a tag again - the other would have been deleted since there were no more instances associated with it.
     213          $tag = $this->getDataGenerator()->create_tag();
     214  
     215          // Assign a tag to the wiki again.
     216          tag_assign('wiki_pages', $wikipageid, $tag->id, 1, 2, 'mod_wiki', context_module::instance($wiki->cmid)->id);
     217  
     218          // Delete all tag instances for this wiki instance.
     219          $sink = $this->redirectEvents();
     220          tag_delete_instances('mod_wiki', context_module::instance($wiki->cmid)->id);
     221          $events = $sink->get_events();
     222          $event = reset($events);
     223  
     224          // Check that tag was removed from the wiki page and the event data is valid.
     225          $this->assertEquals(0, $DB->count_records('tag_instance'));
     226          $this->assertInstanceOf('\core\event\tag_removed', $event);
     227          $this->assertEquals(context_module::instance($wiki->cmid), $event->get_context());
     228  
     229          // Create another wiki.
     230          $wiki2 = $wikigenerator->create_instance(array('course' => $course->id));
     231          $subwikiid2 = wiki_add_subwiki($wiki2->id, 0);
     232          $wikipageid2 = wiki_create_page($subwikiid2, 'Title', FORMAT_HTML, '2');
     233  
     234          // Assign a tag to both wiki pages.
     235          tag_assign('wiki_pages', $wikipageid, $tag->id, 1, 2, 'mod_wiki', context_module::instance($wiki->cmid)->id);
     236          tag_assign('wiki_pages', $wikipageid2, $tag->id, 1, 2, 'mod_wiki', context_module::instance($wiki2->cmid)->id);
     237  
     238          // Now remove all tag_instances associated with all wikis.
     239          $sink = $this->redirectEvents();
     240          tag_delete_instances('mod_wiki');
     241          $events = $sink->get_events();
     242  
     243          // There will be two events - one for each wiki instance removed.
     244          $event1 = reset($events);
     245          $event2 = $events[1];
     246  
     247          // Check that the tags were removed from the wiki pages.
     248          $this->assertEquals(0, $DB->count_records('tag_instance'));
     249  
     250          // Check the first event data is valid.
     251          $this->assertInstanceOf('\core\event\tag_removed', $event1);
     252          $this->assertEquals(context_module::instance($wiki->cmid), $event1->get_context());
     253  
     254          // Check that the second event data is valid.
     255          $this->assertInstanceOf('\core\event\tag_removed', $event2);
     256          $this->assertEquals(context_module::instance($wiki2->cmid), $event2->get_context());
     257      }
     258  
     259      /**
     260       * Test the tag flagged event.
     261       */
     262      public function test_tag_flagged() {
     263          global $DB;
     264  
     265          $this->setAdminUser();
     266  
     267          // Create tags we are going to flag.
     268          $tag = $this->getDataGenerator()->create_tag();
     269          $tag2 = $this->getDataGenerator()->create_tag();
     270  
     271          // Trigger and capture the event for setting the flag of a tag.
     272          $sink = $this->redirectEvents();
     273          tag_set_flag($tag->id);
     274          $events = $sink->get_events();
     275          $event = reset($events);
     276  
     277          // Check that the flag was updated.
     278          $tag = $DB->get_record('tag', array('id' => $tag->id));
     279          $this->assertEquals(1, $tag->flag);
     280  
     281          // Check that the event data is valid.
     282          $this->assertInstanceOf('\core\event\tag_flagged', $event);
     283          $this->assertEquals(context_system::instance(), $event->get_context());
     284          $expected = array(SITEID, 'tag', 'flag', 'index.php?id=' . $tag->id, $tag->id, '', '2');
     285          $this->assertEventLegacyLogData($expected, $event);
     286  
     287          // Unset the flag for both (though by default tag2 should have been created with 0 already).
     288          tag_unset_flag(array($tag->id, $tag2->id));
     289  
     290          // Trigger and capture the event for setting the flag for multiple tags.
     291          $sink = $this->redirectEvents();
     292          tag_set_flag(array($tag->id, $tag2->id));
     293          $events = $sink->get_events();
     294  
     295          // Check that the flags were updated.
     296          $tag = $DB->get_record('tag', array('id' => $tag->id));
     297          $this->assertEquals(1, $tag->flag);
     298          $tag2 = $DB->get_record('tag', array('id' => $tag2->id));
     299          $this->assertEquals(1, $tag2->flag);
     300  
     301          // Confirm the events.
     302          $event = $events[0];
     303          $this->assertInstanceOf('\core\event\tag_flagged', $event);
     304          $this->assertEquals(context_system::instance(), $event->get_context());
     305          $expected = array(SITEID, 'tag', 'flag', 'index.php?id=' . $tag->id, $tag->id, '', '2');
     306          $this->assertEventLegacyLogData($expected, $event);
     307  
     308          $event = $events[1];
     309          $this->assertInstanceOf('\core\event\tag_flagged', $event);
     310          $this->assertEquals(context_system::instance(), $event->get_context());
     311          $expected = array(SITEID, 'tag', 'flag', 'index.php?id=' . $tag2->id, $tag2->id, '', '2');
     312          $this->assertEventLegacyLogData($expected, $event);
     313      }
     314  
     315      /**
     316       * Test the tag unflagged event.
     317       */
     318      public function test_tag_unflagged() {
     319          global $DB;
     320  
     321          $this->setAdminUser();
     322  
     323          // Create tags we are going to unflag.
     324          $tag = $this->getDataGenerator()->create_tag();
     325          $tag2 = $this->getDataGenerator()->create_tag();
     326  
     327          // Flag it.
     328          tag_set_flag($tag->id);
     329  
     330          // Trigger and capture the event for unsetting the flag of a tag.
     331          $sink = $this->redirectEvents();
     332          tag_unset_flag($tag->id);
     333          $events = $sink->get_events();
     334          $event = reset($events);
     335  
     336          // Check that the flag was updated.
     337          $tag = $DB->get_record('tag', array('id' => $tag->id));
     338          $this->assertEquals(0, $tag->flag);
     339  
     340          // Check that the event data is valid.
     341          $this->assertInstanceOf('\core\event\tag_unflagged', $event);
     342          $this->assertEquals(context_system::instance(), $event->get_context());
     343  
     344          // Set the flag back for both.
     345          tag_set_flag(array($tag->id, $tag2->id));
     346  
     347          // Trigger and capture the event for unsetting the flag for multiple tags.
     348          $sink = $this->redirectEvents();
     349          tag_unset_flag(array($tag->id, $tag2->id));
     350          $events = $sink->get_events();
     351  
     352          // Check that the flags were updated.
     353          $tag = $DB->get_record('tag', array('id' => $tag->id));
     354          $this->assertEquals(0, $tag->flag);
     355          $tag2 = $DB->get_record('tag', array('id' => $tag2->id));
     356          $this->assertEquals(0, $tag2->flag);
     357  
     358          // Confirm the events.
     359          $event = $events[0];
     360          $this->assertInstanceOf('\core\event\tag_unflagged', $event);
     361          $this->assertEquals(context_system::instance(), $event->get_context());
     362  
     363          $event = $events[1];
     364          $this->assertInstanceOf('\core\event\tag_unflagged', $event);
     365          $this->assertEquals(context_system::instance(), $event->get_context());
     366      }
     367  
     368      /**
     369       * Test the tag deleted event.
     370       */
     371      public function test_tag_deleted() {
     372          global $DB;
     373  
     374          $this->setAdminUser();
     375  
     376          // Create a course.
     377          $course = $this->getDataGenerator()->create_course();
     378  
     379          // Create tag we are going to delete.
     380          $tag = $this->getDataGenerator()->create_tag();
     381  
     382          // Trigger and capture the event for deleting a tag.
     383          $sink = $this->redirectEvents();
     384          tag_delete($tag->id);
     385          $events = $sink->get_events();
     386          $event = reset($events);
     387  
     388          // Check that the tag was deleted and the event data is valid.
     389          $this->assertEquals(0, $DB->count_records('tag'));
     390          $this->assertInstanceOf('\core\event\tag_deleted', $event);
     391          $this->assertEquals(context_system::instance(), $event->get_context());
     392  
     393          // Create two tags we are going to delete to ensure passing multiple tags work.
     394          $tag = $this->getDataGenerator()->create_tag();
     395          $tag2 = $this->getDataGenerator()->create_tag();
     396  
     397          // Trigger and capture the events for deleting multiple tags.
     398          $sink = $this->redirectEvents();
     399          tag_delete(array($tag->id, $tag2->id));
     400          $events = $sink->get_events();
     401  
     402          // Check that the tags were deleted and the events data is valid.
     403          $this->assertEquals(0, $DB->count_records('tag'));
     404          foreach ($events as $event) {
     405              $this->assertInstanceOf('\core\event\tag_deleted', $event);
     406              $this->assertEquals(context_system::instance(), $event->get_context());
     407          }
     408  
     409          // Create another tag to delete.
     410          $tag = $this->getDataGenerator()->create_tag();
     411  
     412          // Add a tag instance to a course.
     413          tag_assign('course', $course->id, $tag->id, 0, 2, 'course', context_course::instance($course->id)->id);
     414  
     415          // Trigger and capture the event for deleting a personal tag for a user for a course.
     416          $sink = $this->redirectEvents();
     417          coursetag_delete_keyword($tag->id, 2, $course->id);
     418          $events = $sink->get_events();
     419          $event = $events[1];
     420  
     421          // Check that the tag was deleted and the event data is valid.
     422          $this->assertEquals(0, $DB->count_records('tag'));
     423          $this->assertInstanceOf('\core\event\tag_deleted', $event);
     424          $this->assertEquals(context_system::instance(), $event->get_context());
     425  
     426          // Create a new tag we are going to delete.
     427          $tag = $this->getDataGenerator()->create_tag();
     428  
     429          // Add the tag instance to the course again as it was deleted.
     430          tag_assign('course', $course->id, $tag->id, 0, 2, 'course', context_course::instance($course->id)->id);
     431  
     432          // Trigger and capture the event for deleting all tags in a course.
     433          $sink = $this->redirectEvents();
     434          coursetag_delete_course_tags($course->id);
     435          $events = $sink->get_events();
     436          $event = $events[1];
     437  
     438          // Check that the tag was deleted and the event data is valid.
     439          $this->assertEquals(0, $DB->count_records('tag'));
     440          $this->assertInstanceOf('\core\event\tag_deleted', $event);
     441          $this->assertEquals(context_system::instance(), $event->get_context());
     442  
     443          // Create two tags we are going to delete to ensure passing multiple tags work.
     444          $tag = $this->getDataGenerator()->create_tag();
     445          $tag2 = $this->getDataGenerator()->create_tag();
     446  
     447          // Add multiple tag instances now and check that it still works.
     448          tag_assign('course', $course->id, $tag->id, 0, 2, 'course', context_course::instance($course->id)->id);
     449          tag_assign('course', $course->id, $tag2->id, 0, 2, 'course', context_course::instance($course->id)->id);
     450  
     451          // Trigger and capture the event for deleting all tags in a course.
     452          $sink = $this->redirectEvents();
     453          coursetag_delete_course_tags($course->id);
     454          $events = $sink->get_events();
     455          $events = array($events[1], $events[3]);
     456  
     457          // Check that the tags were deleted and the events data is valid.
     458          $this->assertEquals(0, $DB->count_records('tag'));
     459          foreach ($events as $event) {
     460              $this->assertInstanceOf('\core\event\tag_deleted', $event);
     461              $this->assertEquals(context_system::instance(), $event->get_context());
     462          }
     463      }
     464  
     465      /**
     466       * Test the tag created event.
     467       */
     468      public function test_tag_created() {
     469          global $DB;
     470  
     471          // Trigger and capture the event for creating a tag.
     472          $sink = $this->redirectEvents();
     473          tag_add('A really awesome tag!');
     474          $events = $sink->get_events();
     475          $event = reset($events);
     476  
     477          // Check that the tag was created and the event data is valid.
     478          $this->assertEquals(1, $DB->count_records('tag'));
     479          $this->assertInstanceOf('\core\event\tag_created', $event);
     480          $this->assertEquals(context_system::instance(), $event->get_context());
     481      }
     482  }
    

    Search This Site: