diff --git a/contentbank/contenttype/h5p/tests/behat/admin_upload_content.feature b/contentbank/contenttype/h5p/tests/behat/admin_upload_content.feature new file mode 100644 index 00000000000..8d03118b5e8 --- /dev/null +++ b/contentbank/contenttype/h5p/tests/behat/admin_upload_content.feature @@ -0,0 +1,73 @@ +@core @core_contentbank @contenttype_h5p @_file_upload @_switch_iframe @javascript +Feature: H5P file upload to content bank for admins + In order import new H5P content to content bank + As an admin + I need to be able to upload a new .h5p file to content bank + + Background: + Given 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 press "Customise this page" + And I add the "Navigation" block if not present + And I expand "Site pages" node + And I click on "Content bank" "link" + + Scenario: Admins can upload .h5p extension files to content bank + Given I should not see "filltheblanks.h5p" + 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 wait until the page is ready + Then I should see "filltheblanks.h5p" + + Scenario: Admins can see uploaded H5P contents + Given I should not see "filltheblanks.h5p" + 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 wait until the page is ready + And I click on "filltheblanks.h5p" "link" + And I switch to "h5p-player" class iframe + And I switch to "h5p-iframe" class iframe + Then I should see "Of which countries" + + Scenario: Users can't see content managed by disabled plugins + Given 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 wait until the page is ready + And I should see "filltheblanks.h5p" + And I navigate to "Plugins > Content bank > Manage content bank content types" in site administration + And I click on "Disable" "icon" in the "H5P" "table_row" + And I wait until the page is ready + When I click on "Content bank" "link" + Then I should not see "filltheblanks.h5p" + + Scenario: Contents in a context are not available from other contexts + Given the following "courses" exist: + | fullname | shortname | category | + | Course 1 | C1 | 0 | + 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 wait until the page is ready + Then I should see "filltheblanks.h5p" + And I am on "Course 1" course homepage + And I add the "Navigation" block if not present + And I expand "Site pages" node + And I click on "Content bank" "link" + And I should not see "filltheblanks.h5p" \ No newline at end of file diff --git a/contentbank/contenttype/h5p/tests/behat/teacher_upload_content.feature b/contentbank/contenttype/h5p/tests/behat/teacher_upload_content.feature new file mode 100644 index 00000000000..9c25ac96dae --- /dev/null +++ b/contentbank/contenttype/h5p/tests/behat/teacher_upload_content.feature @@ -0,0 +1,73 @@ +@core @core_contentbank @contenttype_h5p @_file_upload @_switch_iframe @javascript +Feature: H5P file upload to content bank for non admins + In order import new H5P content to content bank + As an admin + I need to be able to upload a new .h5p file to content bank + + Background: + Given the following "users" exist: + | username | firstname | lastname | email | + | teacher1 | Teacher | 1 | teacher1@example.com | + | teacher2 | Teacher | 2 | teacher2@example.com | + And the following "categories" exist: + | name | category | idnumber | + | Cat 1 | 0 | CAT1 | + And the following "courses" exist: + | fullname | shortname | category | + | Course 1 | C1 | CAT1 | + And the following "course enrolments" exist: + | user | course | role | + | teacher1 | C1 | editingteacher | + | teacher2 | C1 | editingteacher | + And I log in as "teacher1" + 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" + + Scenario: Teachers can not access system level content bank + Given I press "Customise this page" + And I add the "Navigation" block if not present + When I expand "Site pages" node + Then I should not see "Content bank" + + Scenario: Teachers can access course level content bank + Given I am on "Course 1" course homepage with editing mode on + And I add the "Navigation" block if not present + When I expand "Site pages" node + Then I should see "Content bank" + + Scenario: Teachers can upload .h5p extension files to course content bank + Given I am on "Course 1" course homepage with editing mode on + And I add the "Navigation" block if not present + When I expand "Site pages" node + And I click on "Content bank" "link" + Then I should not see "filltheblanks.h5p" + And 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 wait until the page is ready + And I should see "filltheblanks.h5p" + + Scenario: Other teachers can see uploaded H5P contents + Given I am on "Course 1" course homepage with editing mode on + And I add the "Navigation" block if not present + When I expand "Site pages" node + And I click on "Content bank" "link" + And 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 wait until the page is ready + And I should see "filltheblanks.h5p" + And I log out + When I log in as "teacher2" + And I am on "Course 1" course homepage with editing mode on + And I add the "Navigation" block if not present + And I expand "Site pages" node + And I click on "Content bank" "link" + Then I should see "filltheblanks.h5p" diff --git a/contentbank/contenttype/h5p/tests/content_h5p_test.php b/contentbank/contenttype/h5p/tests/content_h5p_test.php new file mode 100644 index 00000000000..a4082f84e07 --- /dev/null +++ b/contentbank/contenttype/h5p/tests/content_h5p_test.php @@ -0,0 +1,69 @@ +. + +/** + * Test for H5P content bank plugin. + * + * @package contenttype_h5p + * @category test + * @copyright 2020 Amaia Anabitarte + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +/** + * Test for H5P content bank plugin. + * + * @package contenttype_h5p + * @category test + * @copyright 2020 Amaia Anabitarte + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @coversDefaultClass \contenttype_h5p\content + */ +class contenttype_h5p_content_plugin_testcase extends advanced_testcase { + + /** + * Tests for uploaded file. + * + * @covers ::get_file + */ + public function test_upload_file() { + $this->resetAfterTest(); + + // Create content. + $record = new stdClass(); + $record->name = 'Test content'; + $record->configdata = ''; + $contenttype = new \contenttype_h5p\contenttype(context_system::instance()); + $content = $contenttype->create_content($record); + + // Create a dummy file. + $filename = 'content.h5p'; + $dummy = array( + 'contextid' => \context_system::instance()->id, + 'component' => 'contentbank', + 'filearea' => 'public', + 'itemid' => $content->get_id(), + 'filepath' => '/', + 'filename' => $filename + ); + $fs = get_file_storage(); + $fs->create_file_from_string($dummy, 'dummy content'); + + $file = $content->get_file(); + $this->assertInstanceOf(\stored_file::class, $file); + $this->assertEquals($filename, $file->get_filename()); + } +} diff --git a/contentbank/contenttype/h5p/tests/contenttype_h5p_test.php b/contentbank/contenttype/h5p/tests/contenttype_h5p_test.php new file mode 100644 index 00000000000..a2daa82dcaa --- /dev/null +++ b/contentbank/contenttype/h5p/tests/contenttype_h5p_test.php @@ -0,0 +1,67 @@ +. + +/** + * Test for H5P content bank plugin. + * + * @package contenttype_h5p + * @category test + * @copyright 2020 Amaia Anabitarte + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +/** + * Test for H5P content bank plugin. + * + * @package contenttype_h5p + * @category test + * @copyright 2020 Amaia Anabitarte + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @coversDefaultClass \contenttype_h5p\contenttype + */ +class contenttype_h5p_contenttype_plugin_testcase extends advanced_testcase { + + /** + * Tests can_upload behavior. + * + * @covers ::can_upload + */ + public function test_can_upload() { + $this->resetAfterTest(); + + $systemcontext = \context_system::instance(); + $systemtype = new \contenttype_h5p\contenttype($systemcontext); + + // Admins can upload. + $this->setAdminUser(); + $this->assertTrue($systemtype->can_upload()); + + // Teacher can upload in the course but not at system level. + $course = $this->getDataGenerator()->create_course(); + $teacher = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher'); + $coursecontext = \context_course::instance($course->id); + $coursetype = new \contenttype_h5p\contenttype($coursecontext); + $this->setUser($teacher); + $this->assertTrue($coursetype->can_upload()); + $this->assertFalse($systemtype->can_upload()); + + // Users can't upload. + $user = $this->getDataGenerator()->create_user(); + $this->setUser($user); + $this->assertFalse($coursetype->can_upload()); + $this->assertFalse($systemtype->can_upload()); + } +} diff --git a/contentbank/tests/behat/access_permissions.feature b/contentbank/tests/behat/access_permissions.feature new file mode 100644 index 00000000000..de1fde0ae87 --- /dev/null +++ b/contentbank/tests/behat/access_permissions.feature @@ -0,0 +1,29 @@ +@core @core_contentbank +Feature: Access permission to content Bank + In order to control access to content bank + As an admin + I need to be able to configure users' permissions + + Background: + Given the following "users" exist: + | username | firstname | lastname | email | + | teacher1 | Teacher | 1 | user1@example.com | + And the following "courses" exist: + | fullname | shortname | category | + | Course 1 | C1 | 0 | + And the following "course enrolments" exist: + | user | course | role | + | teacher1 | C1 | editingteacher | + + Scenario: Admins access content bank + Given I log in as "admin" + Then "Content bank" "link" should exist + + Scenario: Editing teachers can access content bank at course level + Given I log in as "teacher1" + And I am on "Course 1" course homepage with editing mode on + Then "Content bank" "link" should exist + + Scenario: Editing teachers can't access content bank at system level + Given I log in as "teacher1" + Then "Content bank" "link" should not exist \ No newline at end of file diff --git a/contentbank/tests/content_test.php b/contentbank/tests/content_test.php new file mode 100644 index 00000000000..04b722f90c9 --- /dev/null +++ b/contentbank/tests/content_test.php @@ -0,0 +1,107 @@ +. + +/** + * Test for content bank contenttype class. + * + * @package core_contentbank + * @category test + * @copyright 2020 Amaia Anabitarte + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace core_contentbank; + +defined('MOODLE_INTERNAL') || die(); + +global $CFG; +require_once($CFG->dirroot . '/contentbank/tests/fixtures/testable_contenttype.php'); +require_once($CFG->dirroot . '/contentbank/tests/fixtures/testable_content.php'); + +use stdClass; +use context_system; +use contenttype_testable\contenttype as contenttype; +/** + * Test for content bank contenttype class. + * + * @package core_contentbank + * @category test + * @copyright 2020 Amaia Anabitarte + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @coversDefaultClass \core_contentbank\content + * + */ +class core_contenttype_content_testcase extends \advanced_testcase { + + /** + * Tests for behaviour of get_name(). + * + * @covers ::get_name + */ + public function test_get_name() { + $this->resetAfterTest(); + + // Create content. + $record = new stdClass(); + $record->name = 'Test content'; + $record->configdata = ''; + + $contenttype = new contenttype(context_system::instance()); + $content = $contenttype->create_content($record); + $this->assertEquals($record->name, $content->get_name()); + } + + /** + * Tests for behaviour of get_content_type(). + * + * @covers ::get_content_type + */ + public function test_get_content_type() { + $this->resetAfterTest(); + + // Create content. + $record = new stdClass(); + $record->name = 'Test content'; + $record->configdata = ''; + + $contenttype = new contenttype(context_system::instance()); + $content = $contenttype->create_content($record); + $this->assertEquals('contenttype_testable', $content->get_content_type()); + } + + /** + * Tests for 'configdata' behaviour. + * + * @covers ::set_configdata + */ + public function test_configdata_changes() { + $this->resetAfterTest(); + + $configdata = "{img: 'icon.svg'}"; + + // Create content. + $record = new stdClass(); + $record->configdata = $configdata; + + $contenttype = new contenttype(context_system::instance()); + $content = $contenttype->create_content($record); + $this->assertEquals($configdata, $content->get_configdata()); + + $configdata = "{alt: 'Name'}"; + $content->set_configdata($configdata); + $this->assertEquals($configdata, $content->get_configdata()); + } +} diff --git a/contentbank/tests/contentbank_test.php b/contentbank/tests/contentbank_test.php new file mode 100644 index 00000000000..199b73a4cf4 --- /dev/null +++ b/contentbank/tests/contentbank_test.php @@ -0,0 +1,178 @@ +. + +/** + * Test for extensions manager. + * + * @package core_contentbank + * @category test + * @copyright 2020 Amaia Anabitarte + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +global $CFG; +require_once($CFG->dirroot . '/contentbank/tests/fixtures/testable_contenttype.php'); + +/** + * Test for extensions manager. + * + * @package core_contentbank + * @category test + * @copyright 2020 Amaia Anabitarte + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @coversDefaultClass \core_contentbank\contentbank + */ +class core_contentbank_testcase extends advanced_testcase { + /** + * Data provider for test_get_extension_supporter. + * + * @return array + */ + public function get_extension_provider() { + return [ + 'H5P file' => ['something.h5p', '.h5p'], + 'PDF file' => ['something.pdf', '.pdf'] + ]; + } + + /** + * Tests for get_extension() function. + * + * @dataProvider get_extension_provider + * @param string $filename The filename given + * @param string $expected The extension of the file + * + * @covers ::get_extension + */ + public function test_get_extension(string $filename, string $expected) { + $this->resetAfterTest(); + + $cb = new \core_contentbank\contentbank(); + + $extension = $cb->get_extension($filename); + $this->assertEquals($expected, $extension); + } + + /** + * Data provider for test_load_context_supported_extensions. + * + * @return array + */ + public function get_extension_supporters_provider() { + return [ + 'H5P first' => [['.h5p' => ['h5p', 'testable']], '.h5p', 'h5p'], + 'Testable first (but upload not implemented)' => [['.h5p' => ['testable', 'h5p']], '.h5p', 'h5p'], + ]; + } + + /** + * Tests for get_extension_supporter() function with admin permissions. + * + * @dataProvider get_extension_supporters_provider + * @param array $supporters The content type plugin supporters for each extension + * @param string $extension The extension of the file given + * @param string $expected The supporter contenttype of the file + * + * @covers ::load_context_supported_extensions + */ + public function test_get_extension_supporter_for_admins(array $supporters, string $extension, string $expected) { + $this->resetAfterTest(); + + $cb = new \core_contentbank\contentbank(); + $expectedsupporters = [$extension => $expected]; + + $systemcontext = context_system::instance(); + + // All contexts allowed for admins. + $this->setAdminUser(); + $contextsupporters = $cb->load_context_supported_extensions($systemcontext); + $this->assertEquals($expectedsupporters, $contextsupporters); + } + + /** + * Tests for get_extension_supporter() function with user default permissions. + * + * @dataProvider get_extension_supporters_provider + * @param array $supporters The content type plugin supporters for each extension + * @param string $extension The extension of the file given + * @param string $expected The supporter contenttype of the file + * + * @covers ::load_context_supported_extensions + */ + public function test_get_extension_supporter_for_users(array $supporters, string $extension, string $expected) { + $this->resetAfterTest(); + + $cb = new \core_contentbank\contentbank(); + $systemcontext = context_system::instance(); + + // Set a user with no permissions. + $user = $this->getDataGenerator()->create_user(); + $this->setUser($user); + + // Users with no capabilities can't upload content. + $contextsupporters = $cb->load_context_supported_extensions($systemcontext); + $this->assertEquals([], $contextsupporters); + } + + /** + * Tests for get_extension_supporter() function with teacher defaul permissions. + * + * @dataProvider get_extension_supporters_provider + * @param array $supporters The content type plugin supporters for each extension + * @param string $extension The extension of the file given + * @param string $expected The supporter contenttype of the file + * + * @covers ::load_context_supported_extensions + */ + public function test_get_extension_supporter_for_teachers(array $supporters, string $extension, string $expected) { + $this->resetAfterTest(); + + $cb = new \core_contentbank\contentbank(); + $expectedsupporters = [$extension => $expected]; + + $course = $this->getDataGenerator()->create_course(); + $teacher = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher'); + $this->setUser($teacher); + $coursecontext = context_course::instance($course->id); + + // Teachers has permission in their context to upload supported by H5P content type. + $contextsupporters = $cb->load_context_supported_extensions($coursecontext); + $this->assertEquals($expectedsupporters, $contextsupporters); + } + + /** + * Tests for get_extension_supporter() function. + * + * @dataProvider get_extension_supporters_provider + * @param array $supporters The content type plugin supporters for each extension + * @param string $extension The extension of the file given + * @param string $expected The supporter contenttype of the file + * + * @covers ::get_extension_supporter + */ + public function test_get_extension_supporter(array $supporters, string $extension, string $expected) { + $this->resetAfterTest(); + + $cb = new \core_contentbank\contentbank(); + $systemcontext = context_system::instance(); + $this->setAdminUser(); + + $supporter = $cb->get_extension_supporter($extension, $systemcontext); + $this->assertEquals($expected, $supporter); + } +} diff --git a/contentbank/tests/contenttype_test.php b/contentbank/tests/contenttype_test.php new file mode 100644 index 00000000000..ec231a0c5b6 --- /dev/null +++ b/contentbank/tests/contenttype_test.php @@ -0,0 +1,160 @@ +. + +/** + * Test for content bank contenttype class. + * + * @package core_contentbank + * @category test + * @copyright 2020 Amaia Anabitarte + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace core_contentbank; + +defined('MOODLE_INTERNAL') || die(); + +global $CFG; +require_once($CFG->dirroot . '/contentbank/tests/fixtures/testable_contenttype.php'); +require_once($CFG->dirroot . '/contentbank/tests/fixtures/testable_content.php'); + +use stdClass; +use context_system; +use contenttype_testable\contenttype as contenttype; +/** + * Test for content bank contenttype class. + * + * @package core_contentbank + * @category test + * @copyright 2020 Amaia Anabitarte + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @coversDefaultClass \core_contentbank\contenttype + * + */ +class core_contenttype_contenttype_testcase extends \advanced_testcase { + + /** + * Tests get_contenttype_name result. + * + * @covers ::get_contenttype_name + */ + public function test_get_contenttype_name() { + $this->resetAfterTest(); + + $systemcontext = \context_system::instance(); + $testable = new contenttype($systemcontext); + + $this->assertEquals('contenttype_testable', $testable->get_contenttype_name()); + } + + /** + * Tests get_plugin_name result. + * + * @covers ::get_plugin_name + */ + public function test_get_plugin_name() { + $this->resetAfterTest(); + + $systemcontext = \context_system::instance(); + $testable = new contenttype($systemcontext); + + $this->assertEquals('testable', $testable->get_plugin_name()); + } + + /** + * Tests get_icon result. + * + * @covers ::get_icon + */ + public function test_get_icon() { + $this->resetAfterTest(); + + $systemcontext = \context_system::instance(); + $testable = new contenttype($systemcontext); + $icon = $testable->get_icon('new content'); + $this->assertContains('archive', $icon); + } + + /** + * Tests is_feature_supported behavior . + * + * @covers ::is_feature_supported + */ + public function test_is_feature_supported() { + $this->resetAfterTest(); + + $systemcontext = \context_system::instance(); + $testable = new contenttype($systemcontext); + + $this->assertTrue($testable->is_feature_supported(contenttype::CAN_TEST)); + $this->assertFalse($testable->is_feature_supported(contenttype::CAN_UPLOAD)); + } + + /** + * Tests can_upload behavior with no implemented upload feature. + * + * @covers ::can_upload + */ + public function test_no_upload_feature_supported() { + $this->resetAfterTest(); + + $systemcontext = \context_system::instance(); + $testable = new contenttype($systemcontext); + + $this->setAdminUser(); + $this->assertFalse($testable->is_feature_supported(contenttype::CAN_UPLOAD)); + $this->assertFalse($testable->can_upload()); + } + + /** + * Test create_content() with empty data. + * + * @covers ::create_content + */ + public function test_create_empty_content() { + $this->resetAfterTest(); + + // Create empty content. + $record = new stdClass(); + + $contenttype = new contenttype(context_system::instance()); + $content = $contenttype->create_content($record); + + $this->assertEquals('contenttype_testable', $content->get_content_type()); + $this->assertInstanceOf('\\contenttype_testable\\content', $content); + } + + /** + * Tests for behaviour of create_content() with data. + * + * @covers ::create_content + */ + public function test_create_content() { + $this->resetAfterTest(); + + // Create content. + $record = new stdClass(); + $record->name = 'Test content'; + $record->configdata = ''; + $record->contenttype = ''; + + $contenttype = new contenttype(context_system::instance()); + $content = $contenttype->create_content($record); + + $this->assertEquals('contenttype_testable', $content->get_content_type()); + $this->assertInstanceOf('\\contenttype_testable\\content', $content); + } +} diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 952ea81c6e7..946bff4a3fa 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -197,6 +197,9 @@ lib/xapi/tests + + contentbank/tests +