first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-09-30 18:11:26 -04:00
commit e592ca6823
27270 changed files with 5002257 additions and 0 deletions
+146
View File
@@ -0,0 +1,146 @@
<?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/>.
namespace core_contentbank\external;
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');
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
use core_external\external_api;
/**
* Content bank's copy content external function tests.
*
* @package core_contentbank
* @copyright 2023 Daniel Neis Araujo
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @coversDefaultClass \core_contentbank\external\copy_content
*/
class copy_content_test extends \externallib_advanced_testcase {
/**
* Test the behaviour of copy_content() for users with permission.
*
* @covers ::execute
*/
public function test_copy_content_with_permission(): void {
global $CFG, $DB;
$this->resetAfterTest();
// Create users.
$roleid = $DB->get_field('role', 'id', ['shortname' => 'editingteacher']);
$teacher = $this->getDataGenerator()->create_user();
$this->getDataGenerator()->role_assign($roleid, $teacher->id);
$this->setUser($teacher);
// Add some content to the content bank as teacher.
$filename = 'filltheblanks.h5p';
$filepath = $CFG->dirroot . '/h5p/tests/fixtures/' . $filename;
$generator = $this->getDataGenerator()->get_plugin_generator('core_contentbank');
$contents = $generator->generate_contentbank_data('contenttype_h5p', 1, $teacher->id, null, true, $filepath);
$content = array_shift($contents);
$oldname = $content->get_name();
$newname = 'New name';
// Call the WS and check the content is copied as expected.
$result = copy_content::execute($content->get_id(), $newname);
$result = external_api::clean_returnvalue(copy_content::execute_returns(), $result);
$this->assertNotEmpty($result['id']);
$record = $DB->get_record('contentbank_content', ['id' => $result['id']]);
$this->assertEquals($newname, $record->name);
$record = $DB->get_record('contentbank_content', ['id' => $content->get_id()]);
$this->assertEquals($oldname, $record->name);
// Call the WS using an unexisting contentid and check an error is thrown.
$this->expectException(\invalid_response_exception::class);
$result = copy_content::execute_returns($content->get_id() + 1, $oldname);
$result = external_api::clean_returnvalue(copy_content::execute_returns(), $result);
$this->assertNotEmpty($result['warnings']);
}
/**
* Test the behaviour of copy_content() for users with and without permission.
*
* @covers ::execute
*/
public function test_copy_content(): void {
global $CFG, $DB;
$this->resetAfterTest();
// Create users.
$course = $this->getDataGenerator()->create_course();
$teacher = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
$teacher2 = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
$manager = $this->getDataGenerator()->create_and_enrol($course, 'manager');
$student = $this->getDataGenerator()->create_and_enrol($course, 'student');
// Add some content to the content bank as teacher.
$coursecontext = \context_course::instance($course->id);
$filename = 'filltheblanks.h5p';
$filepath = $CFG->dirroot . '/h5p/tests/fixtures/' . $filename;
$generator = $this->getDataGenerator()->get_plugin_generator('core_contentbank');
$contents = $generator->generate_contentbank_data('contenttype_h5p', 1, $teacher->id, $coursecontext, true, $filepath);
$content = array_shift($contents);
$oldname = $content->get_name();
$newname = 'New name';
// Call the WS and check the teacher can copy his/her own content.
$this->setUser($teacher);
$result = copy_content::execute($content->get_id(), $newname);
$result = external_api::clean_returnvalue(copy_content::execute_returns(), $result);
$this->assertEmpty($result['warnings']);
$record = $DB->get_record('contentbank_content', ['id' => $result['id']]);
$this->assertEquals($newname, $record->name);
// Call the WS and check the content has not been copied by the student.
$this->setUser($student);
$result = copy_content::execute($content->get_id(), $newname);
$result = external_api::clean_returnvalue(copy_content::execute_returns(), $result);
$this->assertNotEmpty($result['warnings']);
$record = $DB->get_record('contentbank_content', ['id' => $content->get_id()]);
$this->assertEquals($oldname, $record->name);
$this->assertNotEquals($newname, $record->name);
// Call the WS an check the content with empty name is not copied by the teacher.
$this->setUser($teacher);
$result = copy_content::execute($content->get_id(), '');
$result = external_api::clean_returnvalue(copy_content::execute_returns(), $result);
$this->assertNotEmpty($result['warnings']);
// Call the WS and check a teacher cannot copy content from another teacher by default.
$this->setUser($teacher2);
$result = copy_content::execute($content->get_id(), 'New name 2');
$result = external_api::clean_returnvalue(copy_content::execute_returns(), $result);
$this->assertNotEmpty($result['warnings']);
// Call the WS and check a manager can copy content from a teacher by default.
$this->setUser($manager);
$result = copy_content::execute($content->get_id(), $newname);
$result = external_api::clean_returnvalue(copy_content::execute_returns(), $result);
$this->assertEmpty($result['warnings']);
$record = $DB->get_record('contentbank_content', ['id' => $result['id']]);
$this->assertEquals($newname, $record->name);
}
}
+112
View File
@@ -0,0 +1,112 @@
<?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/>.
/**
* External function test for delete_content.
*
* @package core_contentbank
* @category external
* @since Moodle 3.9
* @copyright 2020 Sara Arjona <sara@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_contentbank\external;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
require_once($CFG->dirroot . '/contentbank/tests/fixtures/testable_content.php');
use dml_missing_record_exception;
use core_external\external_api;
use externallib_advanced_testcase;
/**
* External function test for delete_content.
*
* @package core_contentbank
* @copyright 2020 Sara Arjona <sara@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class delete_content_test extends externallib_advanced_testcase {
/**
* Test the behaviour of delete_content().
*/
public function test_delete_content(): void {
global $DB;
$this->resetAfterTest();
$records = [];
// Create users.
$user = $this->getDataGenerator()->create_user();
$roleid = $DB->get_field('role', 'id', array('shortname' => 'manager'));
$manager = $this->getDataGenerator()->create_user();
$this->getDataGenerator()->role_assign($roleid, $manager->id);
// Add some content to the content bank.
$generator = $this->getDataGenerator()->get_plugin_generator('core_contentbank');
$records[$manager->id] = $generator->generate_contentbank_data('contenttype_testable', 4, $manager->id, null, false);
$records[$user->id] = $generator->generate_contentbank_data('contenttype_testable', 2, $user->id, null, false);
// Check the content has been created as expected.
$this->assertEquals(6, $DB->count_records('contentbank_content'));
// Check the content is deleted as expected by the user when the content has been created by herself.
$this->setUser($user);
$userrecord = array_shift($records[$user->id]);
$result = delete_content::execute([$userrecord->id]);
$result = external_api::clean_returnvalue(delete_content::execute_returns(), $result);
$this->assertTrue($result['result']);
$this->assertCount(0, $result['warnings']);
$this->assertEquals(5, $DB->count_records('contentbank_content'));
// Check the content is not deleted if the user hasn't created it and has only permission to delete her own content.
$userrecord = array_shift($records[$user->id]);
$managerrecord1 = array_shift($records[$manager->id]);
$result = delete_content::execute([$managerrecord1->id, $userrecord->id]);
$result = external_api::clean_returnvalue(delete_content::execute_returns(), $result);
$this->assertFalse($result['result']);
$this->assertCount(1, $result['warnings']);
$warning = array_shift($result['warnings']);
$this->assertEquals('nopermissiontodelete', $warning['warningcode']);
$this->assertEquals($managerrecord1->id, $warning['item']);
$this->assertEquals(4, $DB->count_records('contentbank_content'));
// Check the content is deleted as expected by the manager.
$this->setUser($manager);
$managerrecord2 = array_shift($records[$manager->id]);
$result = delete_content::execute([$managerrecord1->id, $managerrecord2->id]);
$result = external_api::clean_returnvalue(delete_content::execute_returns(), $result);
$this->assertTrue($result['result']);
$this->assertCount(0, $result['warnings']);
$this->assertEquals(2, $DB->count_records('contentbank_content'));
// Check an exception warning is returned if an unexisting contentid is deleted.
// Check also the other content is deleted (so the process continues after the exception is thrown).
$managerrecord3 = array_shift($records[$manager->id]);
$result = delete_content::execute([$managerrecord1->id, $managerrecord3->id]);
$result = external_api::clean_returnvalue(delete_content::execute_returns(), $result);
$this->assertFalse($result['result']);
$this->assertCount(1, $result['warnings']);
$warning = array_shift($result['warnings']);
$this->assertEquals('exception', $warning['warningcode']);
$this->assertEquals($managerrecord1->id, $warning['item']);
$this->assertEquals(1, $DB->count_records('contentbank_content'));
}
}
+140
View File
@@ -0,0 +1,140 @@
<?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/>.
/**
* Core content bank external functions tests.
*
* @package core_contentbank
* @category external
* @copyright 2020 Sara Arjona <sara@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 3.9
*/
namespace core_contentbank\external;
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');
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
use core_external\external_api;
/**
* Core content bank external functions tests.
*
* @package core_contentbank
* @copyright 2020 Sara Arjona <sara@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @coversDefaultClass \core_contentbank\external\rename_content
*/
class rename_content_test extends \externallib_advanced_testcase {
/**
* Data provider for test_rename_content.
*
* @return array
*/
public function rename_content_provider() {
return [
'Standard name' => ['New name', 'New name', true],
'Name with digits' => ['Today is 17/04/2017', 'Today is 17/04/2017', true],
'Name with symbols' => ['Follow us: @moodle', 'Follow us: @moodle', true],
'Name with tags' => ['This is <b>bold</b>', 'This is bold', true],
'Long name' => [str_repeat('a', 100), str_repeat('a', 100), true],
'Too long name' => [str_repeat('a', 300), str_repeat('a', 255), true],
'Empty name' => ['', 'Test content ', false],
'Blanks only' => [' ', 'Test content ', false],
'Zero name' => ['0', '0', true],
];
}
/**
* Test the behaviour of rename_content() for users with permission.
*
* @dataProvider rename_content_provider
* @param string $newname The name to set
* @param string $expectedname The name result
* @param bool $expectedresult The bolean result expected when renaming
*
* @covers ::execute
*/
public function test_rename_content_with_permission(string $newname, string $expectedname, bool $expectedresult): void {
global $DB;
$this->resetAfterTest();
// Create users.
$roleid = $DB->get_field('role', 'id', ['shortname' => 'editingteacher']);
$teacher = $this->getDataGenerator()->create_user();
$this->getDataGenerator()->role_assign($roleid, $teacher->id);
$this->setUser($teacher);
// Add some content to the content bank as teacher.
$generator = $this->getDataGenerator()->get_plugin_generator('core_contentbank');
$contents = $generator->generate_contentbank_data('contenttype_testable', 1, $teacher->id);
$content = array_shift($contents);
$oldname = $content->get_name();
// Call the WS and check the content is renamed as expected.
$result = rename_content::execute($content->get_id(), $newname);
$result = external_api::clean_returnvalue(rename_content::execute_returns(), $result);
$this->assertEquals($expectedresult, $result['result']);
$record = $DB->get_record('contentbank_content', ['id' => $content->get_id()]);
$this->assertEquals($expectedname, $record->name);
// Call the WS using an unexisting contentid and check an error is thrown.
$this->expectException(\invalid_response_exception::class);
$result = rename_content::execute_returns($content->get_id() + 1, $oldname);
$result = external_api::clean_returnvalue(rename_content::execute_returns(), $result);
$this->assertFalse($result['result']);
}
/**
* Test the behaviour of rename_content() for users with permission.
*
* @covers ::execute
*/
public function test_rename_content_without_permission(): void {
global $DB;
$this->resetAfterTest();
// Create users.
$course = $this->getDataGenerator()->create_course();
$teacher = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
$student = $this->getDataGenerator()->create_and_enrol($course, 'student');
// Add some content to the content bank as teacher.
$generator = $this->getDataGenerator()->get_plugin_generator('core_contentbank');
$contents = $generator->generate_contentbank_data('contenttype_testable', 1, $teacher->id);
$content = array_shift($contents);
$oldname = $content->get_name();
$newname = 'New name';
// Call the WS and check the content has not been renamed by the student.
$this->setUser($student);
$result = rename_content::execute($content->get_id(), $newname);
$result = external_api::clean_returnvalue(rename_content::execute_returns(), $result);
$this->assertFalse($result['result']);
$record = $DB->get_record('contentbank_content', ['id' => $content->get_id()]);
$this->assertEquals($oldname, $record->name);
$this->assertNotEquals($newname, $record->name);
}
}