芝麻web文件管理V1.00
编辑当前文件:/home2/sdektunc/.trash/cepali/mod/forum/tests/vaults_forum_test.php
. /** * The forum vault tests. * * @package mod_forum * @copyright 2019 Ryan Wyllie
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ defined('MOODLE_INTERNAL') || die(); require_once(__DIR__ . '/generator_trait.php'); /** * The forum vault tests. * * @package mod_forum * @copyright 2019 Ryan Wyllie
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @coversDefaultClass \mod_forum\local\vaults\forum */ class mod_forum_vaults_forum_testcase extends advanced_testcase { // Make use of the test generator trait. use mod_forum_tests_generator_trait; /** * Test get_from_id. * * @covers ::get_from_id */ public function test_get_from_id() { $this->resetAfterTest(); $vaultfactory = \mod_forum\local\container::get_vault_factory(); $vault = $vaultfactory->get_forum_vault(); $datagenerator = $this->getDataGenerator(); $user = $datagenerator->create_user(); $course = $datagenerator->create_course(); $forum = $datagenerator->create_module('forum', ['course' => $course->id]); $entity = $vault->get_from_id($forum->id); $this->assertEquals($forum->id, $entity->get_id()); } /** * Test get_from_course_module_id. * * @covers ::get_from_course_module_id */ public function test_get_from_course_module_id() { $this->resetAfterTest(); $vaultfactory = \mod_forum\local\container::get_vault_factory(); $vault = $vaultfactory->get_forum_vault(); $datagenerator = $this->getDataGenerator(); $user = $datagenerator->create_user(); $course = $datagenerator->create_course(); $forum1 = $datagenerator->create_module('forum', ['course' => $course->id]); $forum2 = $datagenerator->create_module('forum', ['course' => $course->id]); $coursemodule1 = get_coursemodule_from_instance('forum', $forum1->id); $coursemodule2 = get_coursemodule_from_instance('forum', $forum2->id); // Don't exist. $entity = $vault->get_from_course_module_id($coursemodule1->id + 100); $this->assertEquals(null, $entity); $entity = $vault->get_from_course_module_id($coursemodule1->id); $this->assertEquals($forum1->id, $entity->get_id()); } /** * Test get_from_course_module_ids. * * @covers ::get_from_course_module_ids */ public function test_get_from_course_module_ids() { $this->resetAfterTest(); $vaultfactory = \mod_forum\local\container::get_vault_factory(); $vault = $vaultfactory->get_forum_vault(); $datagenerator = $this->getDataGenerator(); $user = $datagenerator->create_user(); $course = $datagenerator->create_course(); $forum1 = $datagenerator->create_module('forum', ['course' => $course->id]); $forum2 = $datagenerator->create_module('forum', ['course' => $course->id]); $coursemodule1 = get_coursemodule_from_instance('forum', $forum1->id); $coursemodule2 = get_coursemodule_from_instance('forum', $forum2->id); // Don't exist. $entities = array_values($vault->get_from_course_module_ids([$coursemodule1->id + 100, $coursemodule1->id + 200])); $this->assertEquals([], $entities); $entities = array_values($vault->get_from_course_module_ids([$coursemodule1->id, $coursemodule2->id])); usort($entities, function($a, $b) { return $a->get_id() <=> $b->get_id(); }); $this->assertCount(2, $entities); $this->assertEquals($forum1->id, $entities[0]->get_id()); $this->assertEquals($forum2->id, $entities[1]->get_id()); $entities = array_values($vault->get_from_course_module_ids([$coursemodule1->id])); usort($entities, function($a, $b) { return $a->get_id() <=> $b->get_id(); }); $this->assertCount(1, $entities); $this->assertEquals($forum1->id, $entities[0]->get_id()); } /** * Test get_from_post_id. * * @covers ::get_from_post_id */ public function test_get_from_post_id() { $this->resetAfterTest(); $vaultfactory = \mod_forum\local\container::get_vault_factory(); $vault = $vaultfactory->get_forum_vault(); $datagenerator = $this->getDataGenerator(); $user = $datagenerator->create_user(); $course = $datagenerator->create_course(); $forum = $datagenerator->create_module('forum', ['course' => $course->id]); [$discussion, $post] = $this->helper_post_to_forum($forum, $user); $reply = $this->helper_reply_to_post($post, $user); $otherforum = $datagenerator->create_module('forum', ['course' => $course->id]); [$otherdiscussion, $otherpost] = $this->helper_post_to_forum($otherforum, $user); $otherreply = $this->helper_reply_to_post($otherpost, $user); $entity = $vault->get_from_post_id($post->id); $this->assertEquals($forum->id, $entity->get_id()); $entity = $vault->get_from_post_id($reply->id); $this->assertEquals($forum->id, $entity->get_id()); $this->assertEmpty($vault->get_from_post_id(-1)); } }