-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dfe006f
commit c4ba44f
Showing
9 changed files
with
231 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,213 @@ | ||
<?php | ||
|
||
namespace tool_objectfs\local; | ||
|
||
use coding_exception; | ||
use tool_objectfs\local\manager; | ||
use tool_objectfs\local\tag\tag_manager; | ||
use tool_objectfs\local\tag\tag_source; | ||
use tool_objectfs\tests\testcase; | ||
|
||
class tagging_test extends testcase { | ||
public function test_get_defined_tag_sources() { | ||
$sources = tag_manager::get_defined_tag_sources(); | ||
$this->assertIsArray($sources); | ||
|
||
// Both AWS and Azure limit 10 tags per object, so ensure never more than 10 sources defined. | ||
$this->assertLessThanOrEqual(10, count($sources)); | ||
} | ||
|
||
public static function tag_source_provider(): array { | ||
$sources = tag_manager::get_defined_tag_sources(); | ||
$tests = []; | ||
|
||
foreach ($sources as $source) { | ||
$tests[$source->get_identifier()] = [ | ||
'source' => $source | ||
]; | ||
} | ||
|
||
return $tests; | ||
} | ||
|
||
/** | ||
* @dataProvider tag_source_provider | ||
*/ | ||
public function test_tag_sources_identifier(tag_source $source) { | ||
// Ensure tag source is < 128 chars, to fit AWS & Azure spec. | ||
$count = strlen($source->get_identifier()); | ||
$this->assertLessThan(128, $count); | ||
$this->assertGreaterThan(0, $count); | ||
} | ||
|
||
public static function is_tagging_enabled_provider(): array { | ||
return [ | ||
'neither config nor fs supports' => [ | ||
'enabledinconfig' => false, | ||
'supportedbyfs' => false, | ||
'expected' => false, | ||
], | ||
'enabled in config but fs does not support' => [ | ||
'enabledinconfig' => true, | ||
'supportedbyfs' => false, | ||
'expected' => false, | ||
], | ||
'enabled in config and fs does support' => [ | ||
'enabledinconfig' => true, | ||
'supportedbyfs' => true, | ||
'expected' => true, | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider is_tagging_enabled_provider | ||
*/ | ||
public function test_is_tagging_enabled(bool $enabledinconfig, bool $supportedbyfs, bool $expected) { | ||
global $CFG; | ||
// Set config. | ||
set_config('taggingenabled', $enabledinconfig, 'tool_objectfs'); | ||
|
||
// Set supported by fs. | ||
$config = manager::get_objectfs_config(); | ||
$config->taggingenabled = $enabledinconfig; | ||
$config->enabletasks = true; | ||
$config->filesystem = '\\tool_objectfs\\tests\\test_file_system'; | ||
manager::set_objectfs_config($config); | ||
$CFG->phpunit_objectfs_supports_object_tagging = $supportedbyfs; | ||
|
||
$this->assertEquals($expected, tag_manager::is_tagging_enabled()); | ||
} | ||
|
||
public function test_query_local_tags() { | ||
global $DB; | ||
|
||
// Setup some fake tag data. | ||
$DB->insert_records('tool_objectfs_object_tags', [ | ||
[ | ||
'contenthash' => 'abc123', | ||
'tagkey' => 'test', | ||
'tagvalue' => 'test', | ||
'timemodified' => time() | ||
], | ||
[ | ||
'contenthash' => 'abc123', | ||
'tagkey' => 'test2', | ||
'tagvalue' => 'test2', | ||
'timemodified' => time() | ||
] | ||
]); | ||
|
||
$this->assertCount(2, tag_manager::query_local_tags('abc123')); | ||
$this->assertCount(0, tag_manager::query_local_tags('doesnotexist')); | ||
} | ||
|
||
public function test_update_tags_if_necessary() { | ||
global $DB; | ||
|
||
// There should be no existing tags to begin with. | ||
$this->assertEmpty(tag_manager::query_local_tags('test')); | ||
|
||
// Update tags if necessary - should update the tags. | ||
tag_manager::update_tags_if_necessary('test'); | ||
|
||
// Query tags - should be equal to number defined by the manager. | ||
$expectedcount = count(tag_manager::get_defined_tag_sources()); | ||
$this->assertCount($expectedcount, tag_manager::query_local_tags('test'), 'Tags created match the number defined by manager'); | ||
|
||
// Note the timemodified of one of the tags. | ||
$timemodified = $DB->get_field('tool_objectfs_object_tags', 'timemodified', ['contenthash' => 'test', 'tagkey' => tag_manager::get_defined_tag_sources()[0]->get_identifier()]); | ||
|
||
// Running again, timemodified should not change. | ||
$this->waitForSecond(); | ||
tag_manager::update_tags_if_necessary('test'); | ||
|
||
$newtimemodified = $DB->get_field('tool_objectfs_object_tags', 'timemodified', ['contenthash' => 'test', 'tagkey' => tag_manager::get_defined_tag_sources()[0]->get_identifier()]); | ||
$this->assertEquals($timemodified, $newtimemodified, 'Tags timemodified must not change if not forced and values are not different'); | ||
|
||
// Except if it is forced. | ||
$this->waitForSecond(); | ||
tag_manager::update_tags_if_necessary('test', true); | ||
|
||
$timemodifiedafterforce = $DB->get_field('tool_objectfs_object_tags', 'timemodified', ['contenthash' => 'test', 'tagkey' => tag_manager::get_defined_tag_sources()[0]->get_identifier()]); | ||
$this->assertNotEquals($timemodified, $timemodifiedafterforce, 'Forced tag update changed time modified'); | ||
} | ||
|
||
public static function get_objects_needing_sync_provider(): array { | ||
return [ | ||
'duplicated, needs sync' => [ | ||
'location' => OBJECT_LOCATION_DUPLICATED, | ||
'status' => tag_manager::SYNC_STATUS_NEEDS_SYNC, | ||
'expectedneedssync' => true, | ||
], | ||
'remote, needs sync' => [ | ||
'location' => OBJECT_LOCATION_EXTERNAL, | ||
'status' => tag_manager::SYNC_STATUS_NEEDS_SYNC, | ||
'expectedneedssync' => true, | ||
], | ||
'local, needs sync' => [ | ||
'location' => OBJECT_LOCATION_LOCAL, | ||
'status' => tag_manager::SYNC_STATUS_NEEDS_SYNC, | ||
'expectedneedssync' => false, | ||
], | ||
'duplicated, does not need sync' => [ | ||
'location' => OBJECT_LOCATION_DUPLICATED, | ||
'status' => tag_manager::SYNC_STATUS_SYNC_NOT_REQUIRED, | ||
'expectedneedssync' => false, | ||
], | ||
'local, does not need sync' => [ | ||
'location' => OBJECT_LOCATION_LOCAL, | ||
'status' => tag_manager::SYNC_STATUS_SYNC_NOT_REQUIRED, | ||
'expectedneedssync' => false, | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider get_objects_needing_sync_provider | ||
*/ | ||
public function test_get_objects_needing_sync(int $location, int $syncstatus, bool $expectedneedssync) { | ||
global $DB; | ||
|
||
// Create the test object at the required location. | ||
switch ($location) { | ||
case OBJECT_LOCATION_DUPLICATED: | ||
$object = $this->create_duplicated_object(); | ||
break; | ||
case OBJECT_LOCATION_LOCAL: | ||
$object = $this->create_local_object(); | ||
break; | ||
case OBJECT_LOCATION_EXTERNAL: | ||
$object = $this->create_remote_object(); | ||
break; | ||
default: | ||
throw new coding_exception("Object location not handled in test"); | ||
} | ||
|
||
// Set the sync status. | ||
$DB->set_field('tool_objectfs_objects', 'tagsyncstatus', $syncstatus, ['id' => $object->id]); | ||
|
||
// Check if it is included in the list. | ||
$needssync = tag_manager::get_objects_needing_sync(1); | ||
|
||
if ($expectedneedssync) { | ||
$this->assertContains($object->contenthash, $needssync); | ||
} else { | ||
$this->assertNotContains($object->contenthash, $needssync); | ||
} | ||
} | ||
|
||
public function test_get_objects_needing_sync_limit() { | ||
global $DB; | ||
|
||
// Create two duplicated objects needing sync. | ||
$object = $this->create_duplicated_object(); | ||
$DB->set_field('tool_objectfs_objects', 'tagsyncstatus', tag_manager::SYNC_STATUS_NEEDS_SYNC, ['id' => $object->id]); | ||
$object = $this->create_remote_object(); | ||
$DB->set_field('tool_objectfs_objects', 'tagsyncstatus', tag_manager::SYNC_STATUS_NEEDS_SYNC, ['id' => $object->id]); | ||
|
||
// Ensure a limit of 2 returns 2, and limit of 1 returns 1. | ||
$this->assertCount(2, tag_manager::get_objects_needing_sync(2)); | ||
$this->assertCount(1, tag_manager::get_objects_needing_sync(1)); | ||
} | ||
} |