MDL-67800 core_contentbank: Testing contentbank events
authorAmaia Anabitarte <amaia@moodle.com>
Fri, 1 May 2020 17:47:27 +0000 (19:47 +0200)
committerAmaia Anabitarte <amaia@moodle.com>
Tue, 5 May 2020 08:20:20 +0000 (10:20 +0200)
contentbank/tests/behat/events.feature [new file with mode: 0644]
lib/tests/event/contentbank_content_created_test.php [new file with mode: 0644]
lib/tests/event/contentbank_content_deleted_test.php [new file with mode: 0644]
lib/tests/event/contentbank_content_updated_test.php [new file with mode: 0644]
lib/tests/event/contentbank_content_uploaded_test.php [new file with mode: 0644]
lib/tests/event/contentbank_content_viewed_test.php [new file with mode: 0644]

diff --git a/contentbank/tests/behat/events.feature b/contentbank/tests/behat/events.feature
new file mode 100644 (file)
index 0000000..c520199
--- /dev/null
@@ -0,0 +1,73 @@
+@core @core_contentbank @_file_upload @_switch_iframe @javascript
+Feature: Confirm content bank events are triggered
+  In order to log content bank actions
+  As an admin
+  I need to be able to check triggered events
+
+  Background:
+    And the following "courses" exist:
+      | fullname | shortname | category |
+      | Course 1 | C1        | 0        |
+    And the following "contentbank content" exist:
+      | course | contenttype     | user  | contentname |
+      | C1     | contenttype_h5p | admin | Existing    |
+    And I log in as "admin"
+    And I follow "Manage private files..."
+    And I upload "h5p/tests/fixtures/filltheblanks.h5p" file to "Files" filemanager
+    And I click on "Save changes" "button"
+    And I am on "Course 1" course homepage with editing mode on
+    And I add the "Navigation" block if not present
+
+  Scenario: Content created and uploaded events when uploading a content file
+    Given I navigate to "Reports > Live logs" in site administration
+    And I should not see "Content uploaded"
+    And I should not see "Content created"
+    And I am on "Course 1" course homepage
+    And I expand "Site pages" node
+    And I click on "Content bank" "link"
+    When I click on "Upload" "link"
+    And I click on "Choose a file..." "button"
+    And I click on "Private files" "link" in the ".fp-repo-area" "css_element"
+    And I click on "filltheblanks.h5p" "link"
+    And I click on "Select this file" "button"
+    And I click on "Save changes" "button"
+    And I navigate to "Reports > Live logs" in site administration
+    Then I should see "Content uploaded"
+    And I should see "Content created"
+
+  Scenario: Content viewed event
+    Given I navigate to "Reports > Live logs" in site administration
+    And I should not see "Content viewed"
+    And I am on "Course 1" course homepage
+    And I expand "Site pages" node
+    And I click on "Content bank" "link"
+    When I click on "Existing" "link"
+    And I navigate to "Reports > Live logs" in site administration
+    Then I should see "Content viewed"
+
+  Scenario: Content deleted event
+    Given I navigate to "Reports > Live logs" in site administration
+    And I should not see "Content deleted"
+    And I am on "Course 1" course homepage
+    And I expand "Site pages" node
+    And I click on "Content bank" "link"
+    And I click on "Existing" "link"
+    And I open the action menu in "region-main-settings-menu" "region"
+    When I choose "Delete" in the open action menu
+    And I click on "Delete" "button" in the "Delete content" "dialogue"
+    And I navigate to "Reports > Live logs" in site administration
+    Then I should see "Content deleted"
+
+  Scenario: Content updated event when renaming
+    Given I navigate to "Reports > Live logs" in site administration
+    And I should not see "Content updated"
+    And I am on "Course 1" course homepage
+    And I expand "Site pages" node
+    And I click on "Content bank" "link"
+    And I click on "Existing" "link"
+    And I open the action menu in "region-main-settings-menu" "region"
+    When I choose "Rename" in the open action menu
+    And I set the field "Content name" to "New name"
+    And I click on "Rename" "button"
+    And I navigate to "Reports > Live logs" in site administration
+    Then I should see "Content updated"
\ No newline at end of file
diff --git a/lib/tests/event/contentbank_content_created_test.php b/lib/tests/event/contentbank_content_created_test.php
new file mode 100644 (file)
index 0000000..0e381b9
--- /dev/null
@@ -0,0 +1,77 @@
+<?php
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * Content bank created event tests.
+ *
+ * @package core
+ * @category test
+ * @copyright 2020 Amaia Anabitarte <amaia@moodle.com>
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace core\event;
+
+/**
+ * Test for content bank created event.
+ *
+ * @package    core
+ * @category   test
+ * @copyright  2020 Amaia Anabitarte <amaia@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @coversDefaultClass \core\event\contentbank_content_created
+ */
+class contentbank_content_created_testcase extends \advanced_testcase {
+
+    /**
+     * Setup to ensure that fixtures are loaded.
+     */
+    public static function setUpBeforeClass() {
+        global $CFG;
+
+        require_once($CFG->dirroot . '/contentbank/tests/fixtures/testable_contenttype.php');
+        require_once($CFG->dirroot . '/contentbank/tests/fixtures/testable_content.php');
+    }
+
+    /**
+     * Test the content created event.
+     *
+     * @covers ::create_from_record
+     */
+    public function test_content_created() {
+
+        $this->resetAfterTest();
+        $this->setAdminUser();
+
+        // Save the system context.
+        $systemcontext = \context_system::instance();
+
+        // Trigger and capture the event when creating a content.
+        $sink = $this->redirectEvents();
+
+        // Create a content bank content.
+        $generator = $this->getDataGenerator()->get_plugin_generator('core_contentbank');
+        $contents = $generator->generate_contentbank_data('contenttype_testable', 1);
+        $content = array_shift($contents);
+
+        $events = $sink->get_events();
+        $event = reset($events);
+
+        // Check that the event data is valid.
+        $this->assertInstanceOf('\core\event\contentbank_content_created', $event);
+        $this->assertEquals($systemcontext, $event->get_context());
+    }
+}
diff --git a/lib/tests/event/contentbank_content_deleted_test.php b/lib/tests/event/contentbank_content_deleted_test.php
new file mode 100644 (file)
index 0000000..20c7517
--- /dev/null
@@ -0,0 +1,83 @@
+<?php
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * Content bank deleted event tests.
+ *
+ * @package core
+ * @category test
+ * @copyright 2020 Amaia Anabitarte <amaia@moodle.com>
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace core\event;
+
+/**
+ * Test for content bank deleted event.
+ *
+ * @package    core
+ * @category   test
+ * @copyright  2020 Amaia Anabitarte <amaia@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @coversDefaultClass \core\event\contentbank_content_deleted
+ */
+class contentbank_content_deleted_testcase extends \advanced_testcase {
+
+    /**
+     * Setup to ensure that fixtures are loaded.
+     */
+    public static function setUpBeforeClass() {
+        global $CFG;
+
+        require_once($CFG->dirroot . '/contentbank/tests/fixtures/testable_contenttype.php');
+        require_once($CFG->dirroot . '/contentbank/tests/fixtures/testable_content.php');
+    }
+
+    /**
+     * Test the content deleted event
+     *
+     * @covers ::create_from_record
+     */
+    public function test_content_deleted() {
+        global $DB;
+
+        $this->resetAfterTest();
+        $this->setAdminUser();
+
+        // Save the system context.
+        $systemcontext = \context_system::instance();
+
+        // Create a content bank content.
+        $generator = $this->getDataGenerator()->get_plugin_generator('core_contentbank');
+        $contents = $generator->generate_contentbank_data('contenttype_testable', 3);
+        $content = array_shift($contents);
+        $this->assertEquals(3, $DB->count_records('contentbank_content'));
+
+        $classname = '\\contenttype_testable\\contenttype';
+        $contentype = new $classname($systemcontext);
+
+        // Trigger and capture the event for deleting a content.
+        $sink = $this->redirectEvents();
+        $contentype->delete_content($content);
+        $events = $sink->get_events();
+        $event = reset($events);
+
+        // Check that the content was deleted and the event data is valid.
+        $this->assertEquals(2, $DB->count_records('contentbank_content'));
+        $this->assertInstanceOf('\core\event\contentbank_content_deleted', $event);
+        $this->assertEquals(\context_system::instance(), $event->get_context());
+    }
+}
diff --git a/lib/tests/event/contentbank_content_updated_test.php b/lib/tests/event/contentbank_content_updated_test.php
new file mode 100644 (file)
index 0000000..b6aeae7
--- /dev/null
@@ -0,0 +1,82 @@
+<?php
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * Content bank updated event tests.
+ *
+ * @package core
+ * @category test
+ * @copyright 2020 Amaia Anabitarte <amaia@moodle.com>
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace core\event;
+
+/**
+ * Test for content bank updated event.
+ *
+ * @package    core
+ * @category   test
+ * @copyright  2020 Amaia Anabitarte <amaia@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @coversDefaultClass \core\event\contentbank_content_updated
+ */
+class contentbank_content_updated_testcase extends \advanced_testcase {
+
+    /**
+     * Setup to ensure that fixtures are loaded.
+     */
+    public static function setUpBeforeClass() {
+        global $CFG;
+
+        require_once($CFG->dirroot . '/contentbank/tests/fixtures/testable_contenttype.php');
+        require_once($CFG->dirroot . '/contentbank/tests/fixtures/testable_content.php');
+    }
+
+    /**
+     * Test the content updated event.
+     *
+     * @covers ::create_from_record
+     */
+    public function test_content_updated() {
+
+        $this->resetAfterTest();
+        $this->setAdminUser();
+
+        // Save the system context.
+        $systemcontext = \context_system::instance();
+
+        // Create a content bank content.
+        $generator = $this->getDataGenerator()->get_plugin_generator('core_contentbank');
+        $contents = $generator->generate_contentbank_data('contenttype_testable', 1);
+        $content = array_shift($contents);
+
+        // Store the name before we change it.
+        $oldname = $content->get_name();
+
+        // Trigger and capture the event when renaming a content.
+        $sink = $this->redirectEvents();
+
+        $newname = "New name";
+        $content->set_name($newname);
+        $events = $sink->get_events();
+        $event = reset($events);
+
+        // Check that the event data is valid.
+        $this->assertInstanceOf('\core\event\contentbank_content_updated', $event);
+        $this->assertEquals($systemcontext, $event->get_context());
+    }
+}
diff --git a/lib/tests/event/contentbank_content_uploaded_test.php b/lib/tests/event/contentbank_content_uploaded_test.php
new file mode 100644 (file)
index 0000000..93e6d7d
--- /dev/null
@@ -0,0 +1,94 @@
+<?php
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * Content bank uploaded event tests.
+ *
+ * @package core
+ * @category test
+ * @copyright 2020 Amaia Anabitarte <amaia@moodle.com>
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace core\event;
+
+use core_contentbank\contentbank;
+
+/**
+ * Test for content bank uploaded event.
+ *
+ * @package    core
+ * @category   test
+ * @copyright  2020 Amaia Anabitarte <amaia@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @coversDefaultClass \core\event\contentbank_content_uploaded
+ */
+class contentbank_content_uploaded_testcase extends \advanced_testcase {
+
+    /**
+     * Setup to ensure that fixtures are loaded.
+     */
+    public static function setUpBeforeClass() {
+        global $CFG;
+
+        require_once($CFG->dirroot . '/contentbank/tests/fixtures/testable_contenttype.php');
+        require_once($CFG->dirroot . '/contentbank/tests/fixtures/testable_content.php');
+    }
+
+    /**
+     * Test the content created event.
+     *
+     * @covers ::create_from_record
+     */
+    public function test_content_created() {
+        global $USER;
+
+        $this->resetAfterTest();
+        $this->setAdminUser();
+        $systemcontext = \context_system::instance();
+
+        // Create a dummy H5P file.
+        $dummyh5p = array(
+            'contextid' => $systemcontext->id,
+            'component' => 'contentbank',
+            'filearea' => 'public',
+            'itemid' => 1,
+            'filepath' => '/',
+            'filename' => 'dummy_h5p.h5p'
+        );
+        $fs = get_file_storage();
+        $dummyh5pfile = $fs->create_file_from_string($dummyh5p, 'Dummy H5Pcontent');
+
+        // Trigger and capture the event when creating content from a file.
+        $sink = $this->redirectEvents();
+        $cb = new contentbank();
+        $cb->create_content_from_file($systemcontext, $USER->id, $dummyh5pfile);
+
+        // Both uploaded and created events are raised.
+        $events = $sink->get_events();
+        $this->assertCount(2, $events);
+
+        // First the created content event has been raised.
+        $event = array_shift($events);
+        $this->assertInstanceOf('\core\event\contentbank_content_created', $event);
+        $this->assertEquals($systemcontext, $event->get_context());
+
+        // Second the uploaded content event has been raised.
+        $event = array_pop($events);
+        $this->assertInstanceOf('\core\event\contentbank_content_uploaded', $event);
+        $this->assertEquals($systemcontext, $event->get_context());
+    }
+}
diff --git a/lib/tests/event/contentbank_content_viewed_test.php b/lib/tests/event/contentbank_content_viewed_test.php
new file mode 100644 (file)
index 0000000..acc7e53
--- /dev/null
@@ -0,0 +1,79 @@
+<?php
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * Content bank viewed event tests.
+ *
+ * @package core
+ * @category test
+ * @copyright 2020 Amaia Anabitarte <amaia@moodle.com>
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace core\event;
+
+/**
+ * Test for content bank viewed event.
+ *
+ * @package    core
+ * @category   test
+ * @copyright  2020 Amaia Anabitarte <amaia@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @coversDefaultClass \core\event\contentbank_content_viewed
+ */
+class contentbank_content_viewed_testcase extends \advanced_testcase {
+
+    /**
+     * Setup to ensure that fixtures are loaded.
+     */
+    public static function setUpBeforeClass() {
+        global $CFG;
+
+        require_once($CFG->dirroot . '/contentbank/tests/fixtures/testable_contenttype.php');
+        require_once($CFG->dirroot . '/contentbank/tests/fixtures/testable_content.php');
+    }
+
+    /**
+     * Test the content updated event.
+     *
+     * @covers ::create_from_record
+     */
+    public function test_content_updated() {
+
+        $this->resetAfterTest();
+        $this->setAdminUser();
+
+        // Save the system context.
+        $systemcontext = \context_system::instance();
+
+        // Create a content bank content.
+        $generator = $this->getDataGenerator()->get_plugin_generator('core_contentbank');
+        $contents = $generator->generate_contentbank_data('contenttype_testable', 1);
+        $content = array_shift($contents);
+
+        // Trigger and capture the content viewed event.
+        $sink = $this->redirectEvents();
+        $eventtotrigger = \core\event\contentbank_content_viewed::create_from_record($content->get_content());
+        $eventtotrigger->trigger();
+
+        $events = $sink->get_events();
+        $event = reset($events);
+
+        // Check that the event data is valid.
+        $this->assertInstanceOf('\core\event\contentbank_content_viewed', $event);
+        $this->assertEquals($systemcontext, $event->get_context());
+    }
+}