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
+269
View File
@@ -0,0 +1,269 @@
<?php
// This file is part of Moodle - https://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 <https://www.gnu.org/licenses/>.
namespace core\context;
use core\context, core\context_helper;
/**
* Unit tests for block context class.
*
* NOTE: more tests are in lib/tests/accesslib_test.php
*
* @package core
* @copyright Petr Skoda
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @coversDefaultClass \core\context\block
*/
class block_test extends \advanced_testcase {
/**
* Tests legacy class name.
* @covers \context_block
*/
public function test_legacy_classname(): void {
$this->resetAfterTest();
$block = $this->getDataGenerator()->create_block('online_users');
$context = block::instance($block->id);
$this->assertInstanceOf(block::class, $context);
$this->assertInstanceOf(\context_block::class, $context);
}
/**
* Tests covered methods.
* @covers ::instance
* @covers \core\context::instance_by_id
*/
public function test_factory_methods(): void {
$this->resetAfterTest();
$block = $this->getDataGenerator()->create_block('online_users');
$context = block::instance($block->id);
$this->assertInstanceOf(block::class, $context);
$this->assertSame((string)$block->id, $context->instanceid);
$context = context::instance_by_id($context->id);
$this->assertInstanceOf(block::class, $context);
$this->assertSame((string)$block->id, $context->instanceid);
}
/**
* Tests covered method.
* @covers ::get_short_name
*/
public function test_get_short_name(): void {
$this->assertSame('block', block::get_short_name());
}
/**
* Tests levels.
* @coversNothing
*/
public function test_level(): void {
$this->assertSame(80, block::LEVEL);
$this->assertSame(CONTEXT_BLOCK, block::LEVEL);
}
/**
* Tests covered method.
* @covers ::get_level_name
*/
public function test_get_level_name(): void {
$this->assertSame('Block', block::get_level_name());
}
/**
* Tests covered method.
* @covers ::get_context_name
*/
public function test_get_context_name(): void {
$this->resetAfterTest();
$block = $this->getDataGenerator()->create_block('online_users');
$context = block::instance($block->id);
$this->assertSame('Block: Online users', $context->get_context_name());
$this->assertSame('Block: Online users', $context->get_context_name(true));
$this->assertSame('Online users', $context->get_context_name(false));
$this->assertSame('Online users', $context->get_context_name(false, true));
$this->assertSame('Block: Online users', $context->get_context_name(true, true, false));
}
/**
* Tests covered method.
* @covers ::get_url
*/
public function test_get_url(): void {
$this->resetAfterTest();
$block = $this->getDataGenerator()->create_block('online_users');
$context = block::instance($block->id);
$expected = new \moodle_url('/');
$url = $context->get_url();
$this->assertInstanceOf(\moodle_url::class, $url);
$this->assertSame($expected->out(), $url->out());
}
/**
* Tests covered method.
* @covers ::get_compatible_role_archetypes
*/
public function test_get_compatible_role_archetypes(): void {
global $DB;
$allarchetypes = $DB->get_fieldset_select('role', 'DISTINCT archetype', 'archetype IS NOT NULL');
foreach ($allarchetypes as $allarchetype) {
$levels = context_helper::get_compatible_levels($allarchetype);
$this->assertNotContains(block::LEVEL, $levels, "$allarchetype is not expected to be compatible with context");
}
}
/**
* Tests covered method.
* @covers ::get_possible_parent_levels
*/
public function test_get_possible_parent_levels(): void {
$result = block::get_possible_parent_levels();
// All except itself.
$this->assertContains(system::LEVEL, $result);
$this->assertContains(user::LEVEL, $result);
$this->assertContains(coursecat::LEVEL, $result);
$this->assertContains(course::LEVEL, $result);
$this->assertContains(module::LEVEL, $result);
$this->assertNotContains(block::LEVEL, $result);
// Make sure plugin contexts are covered too.
$all = \core\context_helper::get_all_levels();
$this->assertCount(count($all) - 1, $result);
}
/**
* Tests covered method.
* @covers ::get_capabilities
*/
public function test_get_capabilities(): void {
$this->resetAfterTest();
$block = $this->getDataGenerator()->create_block('online_users');
$context = block::instance($block->id);
$capabilities = $context->get_capabilities();
$capabilities = convert_to_array($capabilities);
$capabilities = array_column($capabilities, 'name');
$this->assertNotContains('moodle/site:config', $capabilities);
$this->assertNotContains('moodle/course:view', $capabilities);
$this->assertNotContains('moodle/category:manage', $capabilities);
$this->assertNotContains('moodle/user:viewalldetails', $capabilities);
$this->assertNotContains('mod/page:view', $capabilities);
$this->assertNotContains('mod/url:view', $capabilities);
}
/**
* Tests covered method.
* @covers ::create_level_instances
*/
public function test_create_level_instances(): void {
global $DB;
$this->resetAfterTest();
$block = $this->getDataGenerator()->create_block('online_users');
$context = block::instance($block->id);
$DB->delete_records('context', ['id' => $context->id]);
context_helper::create_instances(block::LEVEL);
$record = $DB->get_record('context', ['contextlevel' => block::LEVEL, 'instanceid' => $block->id], '*', MUST_EXIST);
}
/**
* Tests covered method.
* @covers ::get_child_contexts
*/
public function test_get_child_contexts(): void {
$this->resetAfterTest();
$block = $this->getDataGenerator()->create_block('online_users');
$context = block::instance($block->id);
$children = $context->get_child_contexts();
$this->assertCount(0, $children);
}
/**
* Tests covered method.
* @covers ::get_cleanup_sql
*/
public function test_get_cleanup_sql(): void {
global $DB;
$this->resetAfterTest();
$block = $this->getDataGenerator()->create_block('online_users');
$context = block::instance($block->id);
$DB->delete_records('block_instances', ['id' => $block->id]);
context_helper::cleanup_instances();
$this->assertFalse($DB->record_exists('context', ['contextlevel' => block::LEVEL, 'instanceid' => $block->id]));
}
/**
* Tests covered method.
* @covers ::build_paths
*/
public function test_build_paths(): void {
global $DB;
$this->resetAfterTest();
$syscontext = system::instance();
$block = $this->getDataGenerator()->create_block('online_users');
$context = block::instance($block->id);
$DB->set_field('context', 'depth', 1, ['id' => $context->id]);
$DB->set_field('context', 'path', '/0', ['id' => $context->id]);
context_helper::build_all_paths(true);
$record = $DB->get_record('context', ['id' => $context->id]);
$this->assertSame('2', $record->depth);
$this->assertSame('/' . $syscontext->id . '/' . $record->id, $record->path);
}
/**
* Tests covered method.
* @covers ::set_locked
*/
public function test_set_locked(): void {
global $DB;
$this->resetAfterTest();
$block = $this->getDataGenerator()->create_block('online_users');
$context = block::instance($block->id);
$context->set_locked(true);
$context = block::instance($block->id);
$this->assertTrue($context->locked);
$record = $DB->get_record('context', ['id' => $context->id]);
$this->assertSame('1', $record->locked);
$context->set_locked(false);
$context = block::instance($block->id);
$this->assertFalse($context->locked);
$record = $DB->get_record('context', ['id' => $context->id]);
$this->assertSame('0', $record->locked);
}
}
+291
View File
@@ -0,0 +1,291 @@
<?php
// This file is part of Moodle - https://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 <https://www.gnu.org/licenses/>.
namespace core\context;
use core\context, core\context_helper;
/**
* Unit tests for course context class.
*
* NOTE: more tests are in lib/tests/accesslib_test.php
*
* @package core
* @copyright Petr Skoda
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @coversDefaultClass \core\context\course
*/
class course_test extends \advanced_testcase {
/**
* Tests legacy class.
* @coversNothing
*/
public function test_legacy_classname(): void {
global $SITE;
$course = $SITE;
$context = \context_course::instance($course->id);
$this->assertInstanceOf(course::class, $context);
$this->assertInstanceOf(\context_course::class, $context);
}
/**
* Tests covered methods.
* @covers ::instance
* @covers \core\context::instance_by_id
*/
public function test_factory_methods(): void {
global $SITE;
$course = $SITE;
$context = course::instance($course->id);
$this->assertInstanceOf(course::class, $context);
$this->assertSame($course->id, $context->instanceid);
$context = context::instance_by_id($context->id);
$this->assertInstanceOf(course::class, $context);
$this->assertSame($course->id, $context->instanceid);
}
/**
* Tests covered method.
* @covers ::get_short_name
*/
public function test_get_short_name(): void {
$this->assertSame('course', course::get_short_name());
}
/**
* Tests levels.
* @coversNothing
*/
public function test_level(): void {
$this->assertSame(50, course::LEVEL);
$this->assertSame(CONTEXT_COURSE, course::LEVEL);
}
/**
* Tests covered method.
* @covers ::get_level_name
*/
public function test_get_level_name(): void {
$this->assertSame('Course', course::get_level_name());
}
/**
* Tests covered method.
* @covers ::get_context_name
*/
public function test_get_context_name(): void {
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course(['fullname' => 'Test course', 'shortname' => 'TST']);
$context = course::instance($course->id);
$this->assertSame('Course: Test course', $context->get_context_name());
$this->assertSame('Course: Test course', $context->get_context_name(true));
$this->assertSame('Test course', $context->get_context_name(false));
$this->assertSame('TST', $context->get_context_name(false, true));
$this->assertSame('Course: TST', $context->get_context_name(true, true, false));
}
/**
* Tests covered method.
* @covers ::get_url
*/
public function test_get_url(): void {
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course();
$context = course::instance($course->id);
$expected = new \moodle_url('/course/view.php', ['id' => $course->id]);
$url = $context->get_url();
$this->assertInstanceOf(\moodle_url::class, $url);
$this->assertSame($expected->out(), $url->out());
}
/**
* Tests covered methods.
* @covers ::get_instance_table()
* @covers ::get_behat_reference_columns()
* @covers \core\context_helper::resolve_behat_reference
*/
public function test_resolve_behat_reference(): void {
$this->resetAfterTest();
$instance = $this->getDataGenerator()->create_course(['shortname' => 'xyz']);
$context = context\course::instance($instance->id);
$result = context_helper::resolve_behat_reference('Course', $instance->shortname);
$this->assertSame($context->id, $result->id);
$result = context_helper::resolve_behat_reference('course', $instance->shortname);
$this->assertSame($context->id, $result->id);
$result = context_helper::resolve_behat_reference('50', $instance->shortname);
$this->assertSame($context->id, $result->id);
$result = context_helper::resolve_behat_reference('Course', 'dshjkdshjkhjsadjhdsa');
$this->assertNull($result);
$result = context_helper::resolve_behat_reference('Course', '');
$this->assertNull($result);
}
/**
* Tests covered method.
* @covers ::get_compatible_role_archetypes
*/
public function test_get_compatible_role_archetypes(): void {
global $DB;
$allarchetypes = $DB->get_fieldset_select('role', 'DISTINCT archetype', 'archetype IS NOT NULL');
foreach ($allarchetypes as $allarchetype) {
$levels = context_helper::get_compatible_levels($allarchetype);
if ($allarchetype === 'editingteacher' || $allarchetype === 'teacher'
|| $allarchetype === 'student' || $allarchetype === 'manager') {
$this->assertContains(course::LEVEL, $levels, "$allarchetype is expected to be compatible with context");
} else {
$this->assertNotContains(course::LEVEL, $levels, "$allarchetype is not expected to be compatible with context");
}
}
}
/**
* Tests covered method.
* @covers ::get_possible_parent_levels
*/
public function test_get_possible_parent_levels(): void {
$this->assertSame([coursecat::LEVEL], course::get_possible_parent_levels());
}
/**
* Tests covered method.
* @covers ::get_capabilities
*/
public function test_get_capabilities(): void {
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course();
$context = course::instance($course->id);
$capabilities = $context->get_capabilities();
$capabilities = convert_to_array($capabilities);
$capabilities = array_column($capabilities, 'name');
$this->assertContains('moodle/course:view', $capabilities);
$this->assertContains('mod/page:view', $capabilities);
$this->assertContains('mod/url:view', $capabilities);
$this->assertNotContains('moodle/category:manage', $capabilities);
$this->assertNotContains('moodle/user:viewalldetails', $capabilities);
}
/**
* Tests covered method.
* @covers ::create_level_instances
*/
public function test_create_level_instances(): void {
global $DB;
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course();
$coursecontext = course::instance($course->id);
$DB->delete_records('context', ['id' => $coursecontext->id]);
context_helper::create_instances(course::LEVEL);
$record = $DB->get_record('context', ['contextlevel' => course::LEVEL, 'instanceid' => $course->id], '*', MUST_EXIST);
}
/**
* Tests covered method.
* @covers ::get_child_contexts
*/
public function test_get_child_contexts(): void {
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course();
$page = $this->getDataGenerator()->create_module('page', ['course' => $course->id, 'name' => 'Pokus']);
$context = course::instance($course->id);
$children = $context->get_child_contexts();
$this->assertCount(1, $children);
$childcontext = reset($children);
$this->assertInstanceOf(module::class, $childcontext);
$this->assertEquals($page->cmid, $childcontext->instanceid);
}
/**
* Tests covered method.
* @covers ::get_cleanup_sql
*/
public function test_get_cleanup_sql(): void {
global $DB;
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course();
$coursecontext = course::instance($course->id);
$DB->delete_records('course', ['id' => $course->id]);
context_helper::cleanup_instances();
$this->assertFalse($DB->record_exists('context', ['contextlevel' => course::LEVEL, 'instanceid' => $course->id]));
}
/**
* Tests covered method.
* @covers ::build_paths
*/
public function test_build_paths(): void {
global $DB;
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course();
$coursecontext = course::instance($course->id);
$syscontext = system::instance();
$DB->set_field('context', 'depth', 1, ['id' => $coursecontext->id]);
$DB->set_field('context', 'path', '/0', ['id' => $coursecontext->id]);
context_helper::build_all_paths(true);
$record = $DB->get_record('context', ['id' => $coursecontext->id]);
$categorycontext = coursecat::instance($course->category);
$this->assertSame('3', $record->depth);
$this->assertSame('/' . $syscontext->id . '/' . $categorycontext->id . '/' . $record->id, $record->path);
}
/**
* Tests covered method.
* @covers ::set_locked
*/
public function test_set_locked(): void {
global $DB;
$this->resetAfterTest();
$course1 = $this->getDataGenerator()->create_course();
$context1 = course::instance($course1->id);
$context1->set_locked(true);
$context1 = course::instance($course1->id);
$this->assertTrue($context1->locked);
$record = $DB->get_record('context', ['id' => $context1->id]);
$this->assertSame('1', $record->locked);
$context1->set_locked(false);
$context1 = course::instance($course1->id);
$this->assertFalse($context1->locked);
$record = $DB->get_record('context', ['id' => $context1->id]);
$this->assertSame('0', $record->locked);
}
}
+289
View File
@@ -0,0 +1,289 @@
<?php
// This file is part of Moodle - https://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 <https://www.gnu.org/licenses/>.
namespace core\context;
use core\context, core\context_helper;
/**
* Unit tests for coursecat context class.
*
* NOTE: more tests are in lib/tests/accesslib_test.php
*
* @package core
* @copyright Petr Skoda
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @coversDefaultClass \core\context\coursecat
*/
class coursecat_test extends \advanced_testcase {
/**
* Tests legacy class name.
* @coversNothing
*/
public function test_legacy_classname(): void {
$category = \core_course_category::get_default();
$context = \context_coursecat::instance($category->id);
$this->assertInstanceOf(coursecat::class, $context);
$this->assertInstanceOf(\context_coursecat::class, $context);
}
/**
* Tests covered methods.
* @covers ::instance
* @covers \core\context::instance_by_id
*/
public function test_factory_methods(): void {
$category = \core_course_category::get_default();
$context = coursecat::instance($category->id);
$this->assertInstanceOf(coursecat::class, $context);
$this->assertSame($category->id, $context->instanceid);
$context = context::instance_by_id($context->id);
$this->assertInstanceOf(coursecat::class, $context);
$this->assertSame($category->id, $context->instanceid);
}
/**
* Tests covered method.
* @covers ::get_short_name
*/
public function test_get_short_name(): void {
$this->assertSame('coursecat', coursecat::get_short_name());
}
/**
* Tests levels.
* @coversNothing
*/
public function test_level(): void {
$this->assertSame(40, coursecat::LEVEL);
$this->assertSame(CONTEXT_COURSECAT, coursecat::LEVEL);
}
/**
* Tests covered method.
* @covers ::get_level_name
*/
public function test_get_level_name(): void {
$this->assertSame('Category', coursecat::get_level_name());
}
/**
* Tests covered method.
* @covers ::get_context_name
*/
public function test_get_context_name(): void {
$category = \core_course_category::get_default();
$context = coursecat::instance($category->id);
$this->assertSame('Category: Category 1', $context->get_context_name());
$this->assertSame('Category: Category 1', $context->get_context_name(true));
$this->assertSame('Category 1', $context->get_context_name(false));
$this->assertSame('Category 1', $context->get_context_name(false, true));
$this->assertSame('Category: Category 1', $context->get_context_name(true, true, false));
}
/**
* Tests covered method.
* @covers ::get_url
*/
public function test_get_url(): void {
$category = \core_course_category::get_default();
$context = coursecat::instance($category->id);
$expected = new \moodle_url('/course/index.php', ['categoryid' => $category->id]);
$url = $context->get_url();
$this->assertInstanceOf(\moodle_url::class, $url);
$this->assertSame($expected->out(), $url->out());
}
/**
* Tests covered methods.
* @covers ::get_instance_table()
* @covers ::get_behat_reference_columns()
* @covers \core\context_helper::resolve_behat_reference
*/
public function test_resolve_behat_reference(): void {
$this->resetAfterTest();
$instance = $this->getDataGenerator()->create_category(['idnumber' => 'xyz']);
$context = context\coursecat::instance($instance->id);
$result = context_helper::resolve_behat_reference('Category', $instance->idnumber);
$this->assertSame($context->id, $result->id);
$result = context_helper::resolve_behat_reference('coursecat', $instance->idnumber);
$this->assertSame($context->id, $result->id);
$result = context_helper::resolve_behat_reference('40', $instance->idnumber);
$this->assertSame($context->id, $result->id);
$result = context_helper::resolve_behat_reference('Category', 'dshjkdshjkhjsadjhdsa');
$this->assertNull($result);
$result = context_helper::resolve_behat_reference('Category', '');
$this->assertNull($result);
}
/**
* Tests covered method.
* @covers ::get_compatible_role_archetypes
*/
public function test_get_compatible_role_archetypes(): void {
global $DB;
$allarchetypes = $DB->get_fieldset_select('role', 'DISTINCT archetype', 'archetype IS NOT NULL');
foreach ($allarchetypes as $allarchetype) {
$levels = context_helper::get_compatible_levels($allarchetype);
if ($allarchetype === 'manager' || $allarchetype === 'coursecreator') {
$this->assertContains(coursecat::LEVEL, $levels, "$allarchetype is expected to be compatible with context");
} else {
$this->assertNotContains(coursecat::LEVEL, $levels, "$allarchetype is not expected to be compatible with context");
}
}
}
/**
* Tests covered method.
* @covers ::get_possible_parent_levels
*/
public function test_get_possible_parent_levels(): void {
$this->assertSame([system::LEVEL, coursecat::LEVEL], coursecat::get_possible_parent_levels());
}
/**
* Tests covered method.
* @covers ::get_capabilities
*/
public function test_get_capabilities(): void {
$category = \core_course_category::get_default();
$context = coursecat::instance($category->id);
$capabilities = $context->get_capabilities();
$capabilities = convert_to_array($capabilities);
$capabilities = array_column($capabilities, 'name');
$this->assertContains('moodle/category:manage', $capabilities);
$this->assertContains('moodle/course:view', $capabilities);
$this->assertNotContains('moodle/user:viewalldetails', $capabilities);
}
/**
* Tests covered method.
* @covers ::create_level_instances
*/
public function test_create_level_instances(): void {
global $DB;
$this->resetAfterTest();
$coursecat = $this->getDataGenerator()->create_category();
$coursecatcontext = coursecat::instance($coursecat->id);
$DB->delete_records('context', ['id' => $coursecatcontext->id]);
context_helper::create_instances(coursecat::LEVEL);
$record = $DB->get_record('context', ['contextlevel' => coursecat::LEVEL, 'instanceid' => $coursecat->id], '*', MUST_EXIST);
}
/**
* Tests covered method.
* @covers ::get_child_contexts
*/
public function test_get_child_contexts(): void {
$this->resetAfterTest();
$category = \core_course_category::get_default();
$context = coursecat::instance($category->id);
$children = $context->get_child_contexts();
$this->assertCount(0, $children);
$course = $this->getDataGenerator()->create_course(['categoryid' => $category->id]);
// This may fail if some plugin auto-creates activities.
$children = $context->get_child_contexts();
$this->assertCount(1, $children);
}
/**
* Tests covered method.
* @covers ::get_cleanup_sql
*/
public function test_get_cleanup_sql(): void {
global $DB;
$this->resetAfterTest();
$coursecat = $this->getDataGenerator()->create_category();
$coursecatcontext = coursecat::instance($coursecat->id);
$DB->delete_records('course_categories', ['id' => $coursecat->id]);
context_helper::cleanup_instances();
$this->assertFalse($DB->record_exists('context', ['contextlevel' => coursecat::LEVEL, 'instanceid' => $coursecat->id]));
}
/**
* Tests covered method.
* @covers ::build_paths
*/
public function test_build_paths(): void {
global $DB;
$this->resetAfterTest();
$coursecat = $this->getDataGenerator()->create_category();
$coursecatcontext = coursecat::instance($coursecat->id);
$syscontext = system::instance();
$DB->set_field('context', 'depth', 1, ['id' => $coursecatcontext->id]);
$DB->set_field('context', 'path', '/0', ['id' => $coursecatcontext->id]);
context_helper::build_all_paths(true);
$record = $DB->get_record('context', ['id' => $coursecatcontext->id]);
$this->assertSame('2', $record->depth);
$this->assertSame('/' . $syscontext->id . '/' . $record->id, $record->path);
}
/**
* Tests covered method.
* @covers ::set_locked
*/
public function test_set_locked(): void {
global $DB;
$this->resetAfterTest();
$category1 = $this->getDataGenerator()->create_category();
$category2 = $this->getDataGenerator()->create_category(['parent' => $category1->id]);
$context1 = coursecat::instance($category1->id);
$context2 = coursecat::instance($category2->id);
$context1->set_locked(true);
$context1 = coursecat::instance($category1->id);
$context2 = coursecat::instance($category2->id);
$this->assertTrue($context1->locked);
$this->assertTrue($context2->locked);
$record = $DB->get_record('context', ['id' => $context1->id]);
$this->assertSame('1', $record->locked);
$record = $DB->get_record('context', ['id' => $context2->id]);
$this->assertSame('0', $record->locked);
$context1->set_locked(false);
$context1 = coursecat::instance($category1->id);
$context2 = coursecat::instance($category2->id);
$this->assertFalse($context1->locked);
$this->assertFalse($context2->locked);
$record = $DB->get_record('context', ['id' => $context1->id]);
$this->assertSame('0', $record->locked);
$record = $DB->get_record('context', ['id' => $context2->id]);
$this->assertSame('0', $record->locked);
}
}
+306
View File
@@ -0,0 +1,306 @@
<?php
// This file is part of Moodle - https://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 <https://www.gnu.org/licenses/>.
namespace core\context;
use core\context, core\context_helper;
/**
* Unit tests for module context class.
*
* NOTE: more tests are in lib/tests/accesslib_test.php
*
* @package core
* @copyright Petr Skoda
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @coversDefaultClass \core\context\module
*/
class module_test extends \advanced_testcase {
/**
* Tests legacy class.
* @coversNothing
*/
public function test_legacy_classname(): void {
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course();
$page = $this->getDataGenerator()->create_module('page', ['course' => $course->id]);
$context = module::instance($page->cmid);
$this->assertInstanceOf(module::class, $context);
$this->assertInstanceOf(\context_module::class, $context);
}
/**
* Tests covered methods.
* @covers ::instance
* @covers \core\context::instance_by_id
*/
public function test_factory_methods(): void {
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course();
$page = $this->getDataGenerator()->create_module('page', ['course' => $course->id]);
$context = module::instance($page->cmid);
$this->assertInstanceOf(module::class, $context);
$this->assertSame((string)$page->cmid, $context->instanceid);
$context = context::instance_by_id($context->id);
$this->assertInstanceOf(module::class, $context);
$this->assertSame((string)$page->cmid, $context->instanceid);
}
/**
* Tests covered method.
* @covers ::get_short_name
*/
public function test_get_short_name(): void {
$this->assertSame('module', module::get_short_name());
}
/**
* Tests context level.
* @coversNothing
*/
public function test_level(): void {
$this->assertSame(70, module::LEVEL);
$this->assertSame(CONTEXT_MODULE, module::LEVEL);
}
/**
* Tests covered method.
* @covers ::get_level_name
*/
public function test_get_level_name(): void {
$this->assertSame('Activity module', module::get_level_name());
}
/**
* Tests covered method.
* @covers ::get_context_name
*/
public function test_get_context_name(): void {
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course();
$page = $this->getDataGenerator()->create_module('page', ['course' => $course->id, 'name' => 'Pokus']);
$context = module::instance($page->cmid);
$this->assertSame('Page: Pokus', $context->get_context_name());
$this->assertSame('Page: Pokus', $context->get_context_name(true));
$this->assertSame('Pokus', $context->get_context_name(false));
$this->assertSame('Pokus', $context->get_context_name(false, true));
$this->assertSame('Page: Pokus', $context->get_context_name(true, true, false));
}
/**
* Tests covered method.
* @covers ::get_url
*/
public function test_get_url(): void {
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course();
$page = $this->getDataGenerator()->create_module('page', ['course' => $course->id, 'name' => 'Pokus']);
$context = module::instance($page->cmid);
$expected = new \moodle_url('/mod/page/view.php', ['id' => $page->cmid]);
$url = $context->get_url();
$this->assertInstanceOf(\moodle_url::class, $url);
$this->assertSame($expected->out(), $url->out());
}
/**
* Tests covered methods.
* @covers ::get_instance_table()
* @covers ::get_behat_reference_columns()
* @covers \core\context_helper::resolve_behat_reference
*/
public function test_resolve_behat_reference(): void {
global $DB;
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course();
$page = $this->getDataGenerator()->create_module('page', ['course' => $course->id, 'idnumber' => 'xyz']);
$instance = $DB->get_record('course_modules', ['id' => $page->cmid], '*', MUST_EXIST);
$context = module::instance($instance->id);
$result = context_helper::resolve_behat_reference('Activity module', $instance->idnumber);
$this->assertSame($context->id, $result->id);
$result = context_helper::resolve_behat_reference('module', $instance->idnumber);
$this->assertSame($context->id, $result->id);
$result = context_helper::resolve_behat_reference('70', $instance->idnumber);
$this->assertSame($context->id, $result->id);
$result = context_helper::resolve_behat_reference('Activity module', 'dshjkdshjkhjsadjhdsa');
$this->assertNull($result);
$result = context_helper::resolve_behat_reference('Activity module', '');
$this->assertNull($result);
}
/**
* Tests covered method.
* @covers ::get_compatible_role_archetypes
*/
public function test_get_compatible_role_archetypes(): void {
global $DB;
$allarchetypes = $DB->get_fieldset_select('role', 'DISTINCT archetype', 'archetype IS NOT NULL');
foreach ($allarchetypes as $allarchetype) {
$levels = context_helper::get_compatible_levels($allarchetype);
if ($allarchetype === 'editingteacher' || $allarchetype === 'teacher' || $allarchetype === 'student') {
$this->assertContains(module::LEVEL, $levels, "$allarchetype is expected to be compatible with context");
} else {
$this->assertNotContains(module::LEVEL, $levels, "$allarchetype is not expected to be compatible with context");
}
}
}
/**
* Tests covered method.
* @covers ::get_possible_parent_levels
*/
public function test_get_possible_parent_levels(): void {
$this->assertSame([course::LEVEL], module::get_possible_parent_levels());
}
/**
* Tests covered method.
* @covers ::get_capabilities
*/
public function test_get_capabilities(): void {
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course();
$page = $this->getDataGenerator()->create_module('page', ['course' => $course->id, 'name' => 'Pokus']);
$context = module::instance($page->cmid);
$capabilities = $context->get_capabilities();
$capabilities = convert_to_array($capabilities);
$capabilities = array_column($capabilities, 'name');
$this->assertContains('mod/page:view', $capabilities);
$this->assertNotContains('mod/url:view', $capabilities);
$this->assertNotContains('moodle/course:view', $capabilities);
$this->assertNotContains('moodle/category:manage', $capabilities);
$this->assertNotContains('moodle/user:viewalldetails', $capabilities);
}
/**
* Tests covered method.
* @covers ::create_level_instances
*/
public function test_create_level_instances(): void {
global $DB;
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course();
$page = $this->getDataGenerator()->create_module('page', ['course' => $course->id, 'name' => 'Pokus']);
$context = module::instance($page->cmid);
$DB->delete_records('context', ['id' => $context->id]);
context_helper::create_instances(module::LEVEL);
$record = $DB->get_record('context', ['contextlevel' => module::LEVEL, 'instanceid' => $page->cmid], '*', MUST_EXIST);
}
/**
* Tests covered method.
* @covers ::get_child_contexts
*/
public function test_get_child_contexts(): void {
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course();
$page = $this->getDataGenerator()->create_module('page', ['course' => $course->id, 'name' => 'Pokus']);
$context = module::instance($page->cmid);
$children = $context->get_child_contexts();
$this->assertCount(0, $children);
}
/**
* Tests covered method.
* @covers ::get_cleanup_sql
*/
public function test_get_cleanup_sql(): void {
global $DB;
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course();
$page = $this->getDataGenerator()->create_module('page', ['course' => $course->id, 'name' => 'Pokus']);
$context = module::instance($page->cmid);
$DB->delete_records('course_modules', ['id' => $page->cmid]);
$DB->delete_records('page', ['id' => $page->id]);
context_helper::cleanup_instances();
$this->assertFalse($DB->record_exists('context', ['contextlevel' => module::LEVEL, 'instanceid' => $page->cmid]));
}
/**
* Tests covered method.
* @covers ::build_paths
*/
public function test_build_paths(): void {
global $DB;
$this->resetAfterTest();
$syscontext = system::instance();
$course = $this->getDataGenerator()->create_course();
$coursecontext = course::instance($course->id);
$page = $this->getDataGenerator()->create_module('page', ['course' => $course->id, 'name' => 'Pokus']);
$pagecontext = module::instance($page->cmid);
$DB->set_field('context', 'depth', 1, ['id' => $pagecontext->id]);
$DB->set_field('context', 'path', '/0', ['id' => $pagecontext->id]);
context_helper::build_all_paths(true);
$record = $DB->get_record('context', ['id' => $pagecontext->id]);
$categorycontext = coursecat::instance($course->category);
$this->assertSame('4', $record->depth);
$this->assertSame('/' . $syscontext->id . '/' . $categorycontext->id . '/'
. $coursecontext->id . '/' . $record->id, $record->path);
}
/**
* Tests covered method.
* @covers ::set_locked
*/
public function test_set_locked(): void {
global $DB;
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course();
$page = $this->getDataGenerator()->create_module('page', ['course' => $course->id, 'name' => 'Pokus']);
$context = module::instance($page->cmid);
$context->set_locked(true);
$context = module::instance($page->cmid);
$this->assertTrue($context->locked);
$record = $DB->get_record('context', ['id' => $context->id]);
$this->assertSame('1', $record->locked);
$context->set_locked(false);
$context = module::instance($page->cmid);
$this->assertFalse($context->locked);
$record = $DB->get_record('context', ['id' => $context->id]);
$this->assertSame('0', $record->locked);
}
}
+233
View File
@@ -0,0 +1,233 @@
<?php
// This file is part of Moodle - https://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 <https://www.gnu.org/licenses/>.
namespace core\context;
use core\context, core\context_helper;
/**
* Unit tests for system context class.
*
* NOTE: more tests are in lib/tests/accesslib_test.php
*
* @package core
* @copyright Petr Skoda
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @coversDefaultClass \core\context\system
*/
class system_test extends \advanced_testcase {
/**
* Tests legacy class.
* @coversNothing
*/
public function test_legacy_classname(): void {
$context = \context_system::instance();
$this->assertInstanceOf(system::class, $context);
$this->assertInstanceOf(\context_system::class, $context);
}
/**
* Tests covered methods.
* @covers ::instance
* @covers \core\context::instance_by_id
*/
public function test_factory_methods(): void {
$context = system::instance();
$this->assertInstanceOf(system::class, $context);
$this->assertEquals(SYSCONTEXTID, $context->id);
$context = context::instance_by_id($context->id);
$this->assertInstanceOf(system::class, $context);
$this->assertEquals(SYSCONTEXTID, $context->id);
}
/**
* Tests covered method.
* @covers ::get_short_name
*/
public function test_get_short_name(): void {
$this->assertSame('system', system::get_short_name());
}
/**
* Tests context level.
* @coversNothing
*/
public function test_level(): void {
$this->assertSame(10, system::LEVEL);
$this->assertSame(CONTEXT_SYSTEM, system::LEVEL);
}
/**
* Tests covered method.
* @covers ::get_level_name
*/
public function test_get_level_name(): void {
$this->assertSame('System', system::get_level_name());
}
/**
* Tests covered method.
* @covers ::get_context_name
*/
public function test_get_context_name(): void {
$context = system::instance();
$this->assertSame('System', $context->get_context_name());
$this->assertSame('System', $context->get_context_name(true));
$this->assertSame('System', $context->get_context_name(false));
$this->assertSame('System', $context->get_context_name(false, true));
$this->assertSame('System', $context->get_context_name(true, true, false));
}
/**
* Tests covered method.
* @covers ::get_url
*/
public function test_get_url(): void {
$context = system::instance();
$expected = new \moodle_url('/');
$url = $context->get_url();
$this->assertInstanceOf(\moodle_url::class, $url);
$this->assertSame($expected->out(), $url->out());
}
/**
* Tests covered method.
* @covers \core\context_helper::resolve_behat_reference
*/
public function test_resolve_behat_reference(): void {
$syscontext = context\system::instance();
$result = context_helper::resolve_behat_reference('System', '');
$this->assertSame($syscontext->id, $result->id);
$result = context_helper::resolve_behat_reference('System', '44');
$this->assertSame($syscontext->id, $result->id);
$result = context_helper::resolve_behat_reference('system', '');
$this->assertSame($syscontext->id, $result->id);
$result = context_helper::resolve_behat_reference('10', '');
$this->assertSame($syscontext->id, $result->id);
}
/**
* Tests covered method.
* @covers ::get_compatible_role_archetypes
*/
public function test_get_compatible_role_archetypes(): void {
global $DB;
$allarchetypes = $DB->get_fieldset_select('role', 'DISTINCT archetype', 'archetype IS NOT NULL');
foreach ($allarchetypes as $allarchetype) {
$levels = context_helper::get_compatible_levels($allarchetype);
if ($allarchetype === 'manager' || $allarchetype === 'coursecreator') {
$this->assertContains(system::LEVEL, $levels, "$allarchetype is expected to be compatible with context");
} else {
$this->assertNotContains(system::LEVEL, $levels, "$allarchetype is not expected to be compatible with context");
}
}
}
/**
* Tests covered method.
* @covers ::get_possible_parent_levels
*/
public function test_get_possible_parent_levels(): void {
$this->assertSame([], system::get_possible_parent_levels());
}
/**
* Tests covered method.
* @covers ::get_capabilities
*/
public function test_get_capabilities(): void {
global $DB;
$context = system::instance();
$capabilities = $context->get_capabilities();
$expected = $DB->count_records('capabilities', []);
$this->assertCount($expected, $capabilities);
}
/**
* Tests covered method.
* @covers ::create_level_instances
*/
public function test_create_level_instances(): void {
context_helper::create_instances(system::LEVEL);
}
/**
* Tests covered method.
* @covers ::get_child_contexts
*/
public function test_get_child_contexts(): void {
global $DB;
$context = system::instance();
$children = $context->get_child_contexts();
$expected = $DB->count_records('context', []) - 1;
$this->assertCount($expected, $children);
$this->assertDebuggingCalled('Fetching of system context child courses is strongly '
. 'discouraged on production servers (it may eat all available memory)!');
}
/**
* Tests covered method.
* @covers ::get_cleanup_sql
*/
public function test_get_cleanup_sql(): void {
// Nothing to clean up actually.
context_helper::cleanup_instances();
}
/**
* Tests covered method.
* @covers ::build_paths
*/
public function test_build_paths(): void {
global $DB;
$this->resetAfterTest();
$DB->set_field('context', 'depth', 2, ['id' => SYSCONTEXTID]);
$DB->set_field('context', 'path', '/0', ['id' => SYSCONTEXTID]);
context_helper::build_all_paths(true);
$record = $DB->get_record('context', ['id' => SYSCONTEXTID]);
$this->assertSame('1', $record->depth);
$this->assertSame('/' . $record->id, $record->path);
}
/**
* Tests covered method.
* @covers ::set_locked
*/
public function test_set_locked(): void {
$context = system::instance();
$context->set_locked(false);
try {
$context->set_locked(true);
} catch (\moodle_exception $e) {
$this->assertInstanceOf(\coding_exception::class, $e);
$this->assertSame('Coding error detected, it must be fixed by a programmer: '
. 'It is not possible to lock the system context', $e->getMessage());
}
}
}
+268
View File
@@ -0,0 +1,268 @@
<?php
// This file is part of Moodle - https://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 <https://www.gnu.org/licenses/>.
namespace core\context;
use core\context, core\context_helper;
/**
* Unit tests for user context class.
*
* NOTE: more tests are in lib/tests/accesslib_test.php
*
* @package core
* @copyright Petr Skoda
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @coversDefaultClass \core\context\user
*/
class user_test extends \advanced_testcase {
/**
* Tests legacy class.
* @coversNothing
*/
public function test_legacy_classname(): void {
$admin = get_admin();
$context = \context_user::instance($admin->id);
$this->assertInstanceOf(user::class, $context);
$this->assertInstanceOf(\context_user::class, $context);
}
/**
* Tests covered methods.
* @covers ::instance
* @covers \core\context::instance_by_id
*/
public function test_factory_methods(): void {
$admin = get_admin();
$context = user::instance($admin->id);
$this->assertInstanceOf(user::class, $context);
$this->assertSame($admin->id, $context->instanceid);
$context = context::instance_by_id($context->id);
$this->assertInstanceOf(user::class, $context);
$this->assertSame($admin->id, $context->instanceid);
}
/**
* Tests covered method.
* @covers ::get_short_name
*/
public function test_get_short_name(): void {
$this->assertSame('user', user::get_short_name());
}
/**
* Tests context level.
* @coversNothing
*/
public function test_level(): void {
$this->assertSame(30, user::LEVEL);
$this->assertSame(CONTEXT_USER, user::LEVEL);
}
/**
* Tests covered method.
* @covers ::get_level_name
*/
public function test_get_level_name(): void {
$this->assertSame('User', user::get_level_name());
}
/**
* Tests covered method.
* @covers ::get_context_name
*/
public function test_get_context_name(): void {
$admin = get_admin();
$context = user::instance($admin->id);
$this->assertSame('User: Admin User', $context->get_context_name());
$this->assertSame('User: Admin User', $context->get_context_name(true));
$this->assertSame('Admin User', $context->get_context_name(false));
$this->assertSame('Admin User', $context->get_context_name(false, true));
$this->assertSame('User: Admin User', $context->get_context_name(true, true, false));
}
/**
* Tests covered method.
* @covers ::get_url
*/
public function test_get_url(): void {
$admin = get_admin();
$context = user::instance($admin->id);
$expected = new \moodle_url('/user/profile.php', ['id' => $admin->id]);
$url = $context->get_url();
$this->assertInstanceOf(\moodle_url::class, $url);
$this->assertSame($expected->out(), $url->out());
}
/**
* Tests covered methods.
* @covers ::get_instance_table()
* @covers ::get_behat_reference_columns()
* @covers \core\context_helper::resolve_behat_reference
*/
public function test_resolve_behat_reference(): void {
$this->resetAfterTest();
$instance = $this->getDataGenerator()->create_user();
$context = context\user::instance($instance->id);
$result = context_helper::resolve_behat_reference('User', $instance->username);
$this->assertSame($context->id, $result->id);
$result = context_helper::resolve_behat_reference('user', $instance->username);
$this->assertSame($context->id, $result->id);
$result = context_helper::resolve_behat_reference('30', $instance->username);
$this->assertSame($context->id, $result->id);
$result = context_helper::resolve_behat_reference('User', 'dshjkdshjkhjsadjhdsa');
$this->assertNull($result);
$result = context_helper::resolve_behat_reference('User', '');
$this->assertNull($result);
}
/**
* Tests covered method.
* @covers ::get_compatible_role_archetypes
*/
public function test_get_compatible_role_archetypes(): void {
global $DB;
$allarchetypes = $DB->get_fieldset_select('role', 'DISTINCT archetype', 'archetype IS NOT NULL');
foreach ($allarchetypes as $allarchetype) {
$levels = context_helper::get_compatible_levels($allarchetype);
$this->assertNotContains(user::LEVEL, $levels, "$allarchetype is not expected to be compatible with context");
}
}
/**
* Tests covered method.
* @covers ::get_possible_parent_levels
*/
public function test_get_possible_parent_levels(): void {
$this->assertSame([system::LEVEL], user::get_possible_parent_levels());
}
/**
* Tests covered method.
* @covers ::get_capabilities
*/
public function test_get_capabilities(): void {
$admin = get_admin();
$context = user::instance($admin->id);
$capabilities = $context->get_capabilities();
$capabilities = convert_to_array($capabilities);
$capabilities = array_column($capabilities, 'name');
$this->assertContains('moodle/user:viewalldetails', $capabilities);
$this->assertContains('moodle/grade:viewall', $capabilities);
$this->assertNotContains('moodle/course:view', $capabilities);
$this->assertNotContains('moodle/site:config', $capabilities);
}
/**
* Tests covered method.
* @covers ::create_level_instances
*/
public function test_create_level_instances(): void {
global $DB;
$this->resetAfterTest();
$user = $this->getDataGenerator()->create_user();
$usercontext = user::instance($user->id);
$DB->delete_records('context', ['id' => $usercontext->id]);
context_helper::create_instances(user::LEVEL);
$record = $DB->get_record('context', ['contextlevel' => user::LEVEL, 'instanceid' => $user->id], '*', MUST_EXIST);
}
/**
* Tests covered method.
* @covers ::get_child_contexts
*/
public function test_get_child_contexts(): void {
$admin = get_admin();
$context = user::instance($admin->id);
$children = $context->get_child_contexts();
$this->assertCount(0, $children);
}
/**
* Tests covered method.
* @covers ::get_cleanup_sql
*/
public function test_get_cleanup_sql(): void {
global $DB;
$this->resetAfterTest();
$user = $this->getDataGenerator()->create_user();
$usercontext = user::instance($user->id);
$DB->set_field('user', 'deleted', 1, ['id' => $user->id]);
context_helper::cleanup_instances();
$this->assertFalse($DB->record_exists('context', ['contextlevel' => user::LEVEL, 'instanceid' => $user->id]));
}
/**
* Tests covered method.
* @covers ::build_paths
*/
public function test_build_paths(): void {
global $DB;
$this->resetAfterTest();
$user = $this->getDataGenerator()->create_user();
$usercontext = user::instance($user->id);
$syscontext = system::instance();
$DB->set_field('context', 'depth', 1, ['id' => $usercontext->id]);
$DB->set_field('context', 'path', '/0', ['id' => $usercontext->id]);
context_helper::build_all_paths(true);
$record = $DB->get_record('context', ['id' => $usercontext->id]);
$this->assertSame('2', $record->depth);
$this->assertSame('/' . $syscontext->id . '/' . $record->id, $record->path);
}
/**
* Tests covered method.
* @covers ::set_locked
*/
public function test_set_locked(): void {
global $DB;
$this->resetAfterTest();
$admin = get_admin();
$context = user::instance($admin->id);
$this->assertFalse($context->locked);
$context->set_locked(true);
$this->assertTrue($context->locked);
$record = $DB->get_record('context', ['id' => $context->id]);
$this->assertSame('1', $record->locked);
$context->set_locked(false);
$this->assertFalse($context->locked);
$record = $DB->get_record('context', ['id' => $context->id]);
$this->assertSame('0', $record->locked);
}
}