Skip to content

Commit

Permalink
MDL-61937 tag: new method in privacy api
Browse files Browse the repository at this point in the history
  • Loading branch information
marinaglancy committed May 4, 2018
1 parent e5ec530 commit 1c4b87c
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 0 deletions.
48 changes: 48 additions & 0 deletions tag/classes/privacy/provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,52 @@ public static function export_item_tags(
->export_related_data($subcontext, 'tags', $tags);
}
}

/**
* Deletes all tag instances for given context, component, itemtype, itemid
*
* In most situations you will want to specify $userid as null. Per-user tag instances
* are possible in Tags API, however there are no components or standard plugins that actually use them.
*
* @param \context $context The context to export for
* @param string $component Tagarea component
* @param string $itemtype Tagarea item type
* @param int $itemid The itemid within that component and itemtype (optional)
* @param int $userid Only delete tag instances made by this user, per-user tags must be enabled for the tagarea
*/
public static function delete_item_tags(\context $context, $component, $itemtype,
$itemid = null, $userid = null) {
global $DB;
$params = ['contextid' => $context->id, 'component' => $component, 'itemtype' => $itemtype];
if ($itemid) {
$params['itemid'] = $itemid;
}
if ($userid) {
$params['userid'] = $userid;
}
$DB->delete_records('tag_instance', $params);
}

/**
* Deletes all tag instances for given context, component, itemtype using subquery for itemids
*
* In most situations you will want to specify $userid as null. Per-user tag instances
* are possible in Tags API, however there are no components or standard plugins that actually use them.
*
* @param \context $context The context to export for
* @param string $component Tagarea component
* @param string $itemtype Tagarea item type
* @param string $itemidstest an SQL fragment that the itemid must match. Used
* in the query like WHERE itemid $itemidstest. Must use named parameters,
* and may not use named parameters called contextid, component or itemtype.
* @param array $params any query params used by $itemidstest.
*/
public static function delete_item_tags_select(\context $context, $component, $itemtype,
$itemidstest, $params = []) {
global $DB;
$params += ['contextid' => $context->id, 'component' => $component, 'itemtype' => $itemtype];
$DB->delete_records_select('tag_instance',
'contextid = :contextid AND component = :component AND itemtype = :itemtype AND itemid ' . $itemidstest,
$params);
}
}
65 changes: 65 additions & 0 deletions tag/tests/privacy_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,69 @@ public function test_export_tags() {
$this->assertContains($exportedtag->rawname, $dummytags);
}
}

/**
* Test method delete_item_tags().
*/
public function test_delete_item_tags() {
global $DB;

$this->resetAfterTest(true);

// Create a course to tag.
$course1 = $this->getDataGenerator()->create_course();
$context1 = context_course::instance($course1->id);
$course2 = $this->getDataGenerator()->create_course();
$context2 = context_course::instance($course2->id);

// Tag courses.
core_tag_tag::set_item_tags('core_course', 'course', $course1->id, $context1, ['Tag 1', 'Tag 2', 'Tag 3']);
core_tag_tag::set_item_tags('core_course', 'course', $course2->id, $context2, ['Tag 1', 'Tag 2']);

$expectedtagcount = $DB->count_records('tag_instance');
// Delete tags for course1.
core_tag\privacy\provider::delete_item_tags($context1, 'core_course', 'course');
$expectedtagcount -= 3;
$this->assertEquals($expectedtagcount, $DB->count_records('tag_instance'));

// Delete tags for course2. Use wrong itemid.
core_tag\privacy\provider::delete_item_tags($context2, 'core_course', 'course', $course1->id);
$this->assertEquals($expectedtagcount, $DB->count_records('tag_instance'));

// Use correct itemid.
core_tag\privacy\provider::delete_item_tags($context2, 'core_course', 'course', $course2->id);
$expectedtagcount -= 2;
$this->assertEquals($expectedtagcount, $DB->count_records('tag_instance'));
}

/**
* Test method delete_item_tags_select().
*/
public function test_delete_item_tags_select() {
global $DB;

$this->resetAfterTest(true);

// Create a course to tag.
$course1 = $this->getDataGenerator()->create_course();
$context1 = context_course::instance($course1->id);
$course2 = $this->getDataGenerator()->create_course();
$context2 = context_course::instance($course2->id);

// Tag courses.
core_tag_tag::set_item_tags('core_course', 'course', $course1->id, $context1, ['Tag 1', 'Tag 2', 'Tag 3']);
core_tag_tag::set_item_tags('core_course', 'course', $course2->id, $context2, ['Tag 1', 'Tag 2']);

$expectedtagcount = $DB->count_records('tag_instance');
// Delete tags for course1.
list($sql, $params) = $DB->get_in_or_equal([$course1->id, $course2->id], SQL_PARAMS_NAMED);
core_tag\privacy\provider::delete_item_tags_select($context1, 'core_course', 'course', $sql, $params);
$expectedtagcount -= 3;
$this->assertEquals($expectedtagcount, $DB->count_records('tag_instance'));

// Delete tags for course2.
core_tag\privacy\provider::delete_item_tags_select($context2, 'core_course', 'course', $sql, $params);
$expectedtagcount -= 2;
$this->assertEquals($expectedtagcount, $DB->count_records('tag_instance'));
}
}

0 comments on commit 1c4b87c

Please sign in to comment.