diff --git a/classes/local/course_category_manager.php b/classes/local/course_category_manager.php new file mode 100644 index 0000000..1970131 --- /dev/null +++ b/classes/local/course_category_manager.php @@ -0,0 +1,47 @@ +get_user_id_by_username($username)) { + throw new moodle_exception('user_not_found', 'local_adler'); + } + if (!$moodle_core_repository->get_role_id_by_shortname($role)) { + throw new moodle_exception('role_not_found', 'local_adler'); + } + if ($category_path->exists()) { + throw new moodle_exception('category_already_exists', 'local_adler'); + } + + + // create category and assign user to role + $category_id = $category_path->create(); + self::assign_user_to_role_in_category($username, $role, $category_id); + + return $category_id; + } + + private static function assign_user_to_role_in_category(string $username, string $role, int $category_id) { + $moodle_core_repository = new moodle_core_repository(); + + $user_id = $moodle_core_repository->get_user_id_by_username($username); + $role_id = $moodle_core_repository->get_role_id_by_shortname($role); + $context = moodle_core::context_coursecat_instance($category_id); + moodle_core::role_assign($role_id, $user_id, $context->id); + } +} \ No newline at end of file diff --git a/classes/local/db/base_repository.php b/classes/local/db/base_repository.php new file mode 100644 index 0000000..c338639 --- /dev/null +++ b/classes/local/db/base_repository.php @@ -0,0 +1,16 @@ +db = $DB; + } else { + $this->db = $db; + } + } +} \ No newline at end of file diff --git a/classes/local/db/moodle_core_repository.php b/classes/local/db/moodle_core_repository.php new file mode 100644 index 0000000..ca26080 --- /dev/null +++ b/classes/local/db/moodle_core_repository.php @@ -0,0 +1,16 @@ +get_field('role', 'id', array('shortname' => $shortname)); + } + + public function get_user_id_by_username(string $username): int { + global $DB; + return (int)$DB->get_field('user', 'id', array('username' => $username)); + } +} \ No newline at end of file diff --git a/classes/moodle_core.php b/classes/moodle_core.php new file mode 100644 index 0000000..e28679c --- /dev/null +++ b/classes/moodle_core.php @@ -0,0 +1,19 @@ +dirroot . '/local/adler/tests/lib/adler_testcase.php'); + +/** + * @runTestsInSeparateProcesses + */ +class course_category_manager_test extends adler_testcase { + private $mockRepo; + + public function setUp(): void { + $this->mockRepo = Mockery::mock('overload:' . moodle_core_repository::class); + } + + public function test_username_doesnt_exist() { + // Arrange + $this->mockRepo->shouldReceive('get_user_id_by_username')->andReturn(false); + + // Act and Assert + $this->expectException(moodle_exception::class); + $this->expectExceptionMessage('user_not_found'); + course_category_manager::create_category_user_can_create_courses_in('non_existing_username', 'role', 'category_path'); + } + + public function test_role_doesnt_exist() { + // Arrange + $this->mockRepo->shouldReceive('get_user_id_by_username')->andReturn(1); // return a valid user ID + $this->mockRepo->shouldReceive('get_role_id_by_shortname')->andReturn(false); + + // Act and Assert + $this->expectException(moodle_exception::class); + $this->expectExceptionMessage('role_not_found'); + course_category_manager::create_category_user_can_create_courses_in('username', 'non_existing_role', 'category_path'); + } + + public function test_category_already_exists() { + // Arrange + $this->mockRepo->shouldReceive('get_user_id_by_username')->andReturn(1); // return a valid user ID + $this->mockRepo->shouldReceive('get_role_id_by_shortname')->andReturn(1); // return a valid role ID + + $mockPath = Mockery::mock('overload:local_adler\local\course_category_path'); + $mockPath->shouldReceive('exists')->andReturn(true); + + // Act and Assert + $this->expectException(moodle_exception::class); + $this->expectExceptionMessage('category_already_exists'); + course_category_manager::create_category_user_can_create_courses_in('valid_username', 'valid_role', 'existing_category_path'); + } + + /** + * @dataProvider providerTestValidUsernameRoleAndCategoryPath + */ + public function test_valid_username_role_and_category_path($category_path, $expected_result) { + // Arrange + $this->mockRepo->shouldReceive('get_user_id_by_username')->andReturn(1); // return a valid user ID + $this->mockRepo->shouldReceive('get_role_id_by_shortname')->andReturn(1); // return a valid role ID + + $mockPath = Mockery::mock('overload:' . course_category_path::class); + $mockPath->shouldReceive('exists')->andReturn(false); + $mockPath->shouldReceive('create')->andReturn(42); + + $moodle_core_mock = Mockery::mock('alias:' . moodle_core::class) + ->shouldReceive('context_coursecat_instance') + ->andReturn(Mockery::mock(context_coursecat::class)); + + $moodle_core_mock + ->shouldReceive('role_assign') + ->withArgs([1, 1, null]) + ->andReturn(true); + + // Act + $result = course_category_manager::create_category_user_can_create_courses_in('valid_username', 'valid_role', $category_path); + + // Assert + $this->assertEquals($expected_result, $result); + } + + public function providerTestValidUsernameRoleAndCategoryPath() { + return [ + 'null category path' => [null, 42], + 'empty category path' => ['', 42], + 'non-existing category path' => ['non_existing_category_path', 42] + ]; + } +} \ No newline at end of file