MDL-67786 core_contentbank: Unit and behat tests

This commit is contained in:
Amaia Anabitarte 2020-04-02 18:05:09 +02:00
parent 70819075c6
commit bd4e0a7664
9 changed files with 759 additions and 0 deletions

View File

@ -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"

View File

@ -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"

View File

@ -0,0 +1,69 @@
<?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/>.
/**
* Test for H5P content bank plugin.
*
* @package contenttype_h5p
* @category test
* @copyright 2020 Amaia Anabitarte <amaia@moodle.com>
* @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 <amaia@moodle.com>
* @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());
}
}

View File

@ -0,0 +1,67 @@
<?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/>.
/**
* Test for H5P content bank plugin.
*
* @package contenttype_h5p
* @category test
* @copyright 2020 Amaia Anabitarte <amaia@moodle.com>
* @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 <amaia@moodle.com>
* @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());
}
}

View File

@ -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

View File

@ -0,0 +1,107 @@
<?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/>.
/**
* Test for content bank contenttype class.
*
* @package core_contentbank
* @category test
* @copyright 2020 Amaia Anabitarte <amaia@moodle.com>
* @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 <amaia@moodle.com>
* @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());
}
}

View File

@ -0,0 +1,178 @@
<?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/>.
/**
* Test for extensions manager.
*
* @package core_contentbank
* @category test
* @copyright 2020 Amaia Anabitarte <amaia@moodle.com>
* @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 <amaia@moodle.com>
* @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);
}
}

View File

@ -0,0 +1,160 @@
<?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/>.
/**
* Test for content bank contenttype class.
*
* @package core_contentbank
* @category test
* @copyright 2020 Amaia Anabitarte <amaia@moodle.com>
* @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 <amaia@moodle.com>
* @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);
}
}

View File

@ -197,6 +197,9 @@
<testsuite name="core_xapi_testsuite">
<directory suffix="_test.php">lib/xapi/tests</directory>
</testsuite>
<testsuite name="core_contentbank_testsuite">
<directory suffix="_test.php">contentbank/tests</directory>
</testsuite>
<!--Plugin suites: use admin/tool/phpunit/cli/util.php to build phpunit.xml from phpunit.xml.dist with up-to-date list of plugins in current install-->
<!--@plugin_suites_start@-->