/** * Test the tag cleanup function used by the cron. */ public function test_tag_cleanup() { global $DB; // Create some users. $users = array(); for ($i = 0; $i < 10; $i++) { $users[] = $this->getDataGenerator()->create_user(); } // Create a course to tag. $course = $this->getDataGenerator()->create_course(); $context = context_course::instance($course->id); // Test clean up instances with tags that no longer exist. $tags = array(); for ($i = 0; $i < 10; $i++) { $tags[] = $this->getDataGenerator()->create_tag(array('userid' => $users[0]->id)); } // Create instances with the tags. foreach ($tags as $tag) { tag_assign('course', $course->id, $tag->id, 0, 0, 'core', $context->id); } // We should now have ten tag instances. $coursetaginstances = $DB->count_records('tag_instance', array('itemtype' => 'course')); $this->assertEquals(10, $coursetaginstances); // Delete four tags // Manual delete of tags is done as the function will remove the instances as well. $DB->delete_records('tag', array('id' => $tags[6]->id)); $DB->delete_records('tag', array('id' => $tags[7]->id)); $DB->delete_records('tag', array('id' => $tags[8]->id)); $DB->delete_records('tag', array('id' => $tags[9]->id)); // Clean up the tags. tag_cleanup(); // Check that we now only have six tag_instance records left. $coursetaginstances = $DB->count_records('tag_instance', array('itemtype' => 'course')); $this->assertEquals(6, $coursetaginstances); // Test clean up with users that have been deleted. // Create a tag for this course. foreach ($users as $user) { tag_assign('user', $user->id, $tags[0]->id, 0, 0, 'core', $context->id); } $usertags = $DB->count_records('tag_instance', array('itemtype' => 'user')); $this->assertCount($usertags, $users); // Remove three students. // Using the proper function to delete the user will also remove the tags. $DB->update_record('user', array('id' => $users[4]->id, 'deleted' => 1)); $DB->update_record('user', array('id' => $users[5]->id, 'deleted' => 1)); $DB->update_record('user', array('id' => $users[6]->id, 'deleted' => 1)); // Clean up the tags. tag_cleanup(); $usertags = $DB->count_records('tag_instance', array('itemtype' => 'user')); $usercount = $DB->count_records('user', array('deleted' => 0)); // Remove admin and guest from the count. $this->assertEquals($usertags, $usercount - 2); // Test clean up where a course has been removed. // Delete the course. This also needs to be this way otherwise the tags are removed by using the proper function. $DB->delete_records('course', array('id' => $course->id)); tag_cleanup(); $coursetags = $DB->count_records('tag_instance', array('itemtype' => 'course')); $this->assertEquals(0, $coursetags); // Test clean up where a post has been removed. // Create default post. $post = new stdClass(); $post->userid = $users[1]->id; $post->content = 'test post content text'; $post->id = $DB->insert_record('post', $post); tag_assign('post', $post->id, $tags[0]->id, 0, 0, 'core', $context->id); // Add another one with a fake post id to be removed. tag_assign('post', 15, $tags[0]->id, 0, 0, 'core', $context->id); // Check that there are two tag instances. $posttags = $DB->count_records('tag_instance', array('itemtype' => 'post')); $this->assertEquals(2, $posttags); // Clean up the tags. tag_cleanup(); // We should only have one entry left now. $posttags = $DB->count_records('tag_instance', array('itemtype' => 'post')); $this->assertEquals(1, $posttags); }
/** * Tasks that should be performed at cron time */ function tag_cron() { tag_compute_correlations(); tag_cleanup(); }
/** * Test for function tag_cleanup() that is part of tag cron */ public function test_cleanup() { global $DB; $user = $this->getDataGenerator()->create_user(); // Setting tags will create non-official tags 'cat', 'dog' and 'fish'. tag_set('user', $user->id, array('cat', 'dog', 'fish'), 'core', context_user::instance($user->id)->id); $this->assertTrue($DB->record_exists('tag', array('name' => 'cat'))); $this->assertTrue($DB->record_exists('tag', array('name' => 'dog'))); $this->assertTrue($DB->record_exists('tag', array('name' => 'fish'))); // Make tag 'dog' official. $dogtag = tag_get('name', 'dog'); $fishtag = tag_get('name', 'fish'); tag_type_set($dogtag->id, 'official'); // Manually remove the instances pointing on tags 'dog' and 'fish'. $DB->execute('DELETE FROM {tag_instance} WHERE tagid in (?,?)', array($dogtag->id, $fishtag->id)); // Call tag_cleanup(). tag_cleanup(); // Tag 'cat' is still present because it's used. Tag 'dog' is present because it's official. // Tag 'fish' was removed because it is not official and it is no longer used by anybody. $this->assertTrue($DB->record_exists('tag', array('name' => 'cat'))); $this->assertTrue($DB->record_exists('tag', array('name' => 'dog'))); $this->assertFalse($DB->record_exists('tag', array('name' => 'fish'))); // Delete user without using API function. $DB->update_record('user', array('id' => $user->id, 'deleted' => 1)); // Call tag_cleanup(). tag_cleanup(); // Tag 'cat' was now deleted too. $this->assertFalse($DB->record_exists('tag', array('name' => 'cat'))); // Assign tag to non-existing record. Make sure tag was created in the DB. tag_set('course', 1231231, array('bird'), 'core', context_system::instance()->id); $this->assertTrue($DB->record_exists('tag', array('name' => 'bird'))); // Call tag_cleanup(). tag_cleanup(); // Tag 'bird' was now deleted because the related record does not exist in the DB. $this->assertFalse($DB->record_exists('tag', array('name' => 'bird'))); // Now we have a tag instance pointing on 'sometag' tag. $user = $this->getDataGenerator()->create_user(); tag_set('user', $user->id, array('sometag'), 'core', context_user::instance($user->id)->id); $sometag = tag_get('name', 'sometag'); $this->assertTrue($DB->record_exists('tag_instance', array('tagid' => $sometag->id))); // Some hacker removes the tag without using API. $DB->delete_records('tag', array('id' => $sometag->id)); // Call tag_cleanup(). tag_cleanup(); // The tag instances were also removed. $this->assertFalse($DB->record_exists('tag_instance', array('tagid' => $sometag->id))); }