From e143bae8eab0c3ada2bae7cd78cf24940052de3e Mon Sep 17 00:00:00 2001 From: Zig Tan Date: Wed, 28 Feb 2018 15:47:56 +0800 Subject: [PATCH] MDL-61307 core_tag: Unit tests for privacy implementation --- tag/tests/privacy_test.php | 89 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 tag/tests/privacy_test.php diff --git a/tag/tests/privacy_test.php b/tag/tests/privacy_test.php new file mode 100644 index 00000000000..db48c381b59 --- /dev/null +++ b/tag/tests/privacy_test.php @@ -0,0 +1,89 @@ +. + +/** + * Privacy tests for core_tag. + * + * @package core_comment + * @category test + * @copyright 2018 Zig Tan + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); +global $CFG; + +require_once($CFG->dirroot . '/tag/lib.php'); + +use \core_privacy\tests\provider_testcase; +use \core_privacy\local\request\writer; +use \core_tag\privacy\provider; + +/** + * Unit tests for tag/classes/privacy/policy + * + * @copyright 2018 Zig Tan + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class core_tag_privacy_testcase extends provider_testcase { + + /** + * Check the exporting of tags for a user id in a context. + */ + public function test_export_tags() { + global $DB; + + $this->resetAfterTest(true); + + // Create a user to perform tagging. + $user = $this->getDataGenerator()->create_user(); + $this->setUser($user); + + // Create a course to tag. + $course = $this->getDataGenerator()->create_course(); + $context = context_course::instance($course->id); + $subcontext = []; + + // Create three dummy tags and tag instances. + $dummytags = [ 'Tag 1', 'Tag 2', 'Tag 3' ]; + core_tag_tag::set_item_tags('core_course', 'course', $course->id, context_course::instance($course->id), + $dummytags, $user->id); + + // Get the tag instances that should have been created. + $taginstances = $DB->get_records('tag_instance', array('itemtype' => 'course', 'itemid' => $course->id)); + $this->assertCount(count($dummytags), $taginstances); + + // Check tag instances match the component and context. + foreach ($taginstances as $taginstance) { + $this->assertEquals('core_course', $taginstance->component); + $this->assertEquals(context_course::instance($course->id)->id, $taginstance->contextid); + } + + // Retrieve tags only for this user. + provider::export_item_tags($user->id, $context, $subcontext, 'core_course', 'course', $course->id, true); + + $writer = writer::with_context($context); + $this->assertTrue($writer->has_any_data()); + + $exportedtags = $writer->get_related_data($subcontext, 'tags'); + $this->assertCount(count($dummytags), $exportedtags); + + // Check the exported tag's rawname is found in the initial dummy tags. + foreach ($exportedtags as $exportedtag) { + $this->assertContains($exportedtag->rawname, $dummytags); + } + } +}