Skip to content

Commit 9ac34ae

Browse files
committed
WIP tests and check api for tagging
1 parent c4ba44f commit 9ac34ae

9 files changed

+86
-15
lines changed

classes/local/store/object_client.php

+2
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ public function test_connection();
120120
*/
121121
public function test_permissions($testdelete);
122122

123+
public function test_set_object_tag();
124+
123125
/**
124126
* proxy_range_request
125127
* @param \stored_file $file

classes/local/store/object_client_base.php

+4
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,10 @@ public function test_permissions($testdelete) {
188188
return (object)['success' => false, 'details' => ''];
189189
}
190190

191+
public function test_set_object_tag() {
192+
return (object)['success' => false, 'details' => ''];
193+
}
194+
191195
public function set_object_tags(string $contenthash, array $tags) {
192196
return;
193197
}

classes/local/store/s3/client.php

+37
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use tool_objectfs\local\store\object_client_base;
3030
use tool_objectfs\local\store\signed_url;
3131
use local_aws\admin_settings_aws_region;
32+
use Throwable;
3233

3334
define('AWS_API_VERSION', '2006-03-01');
3435
define('AWS_CAN_READ_OBJECT', 0);
@@ -390,6 +391,42 @@ public function test_permissions($testdelete) {
390391
return $permissions;
391392
}
392393

394+
public function test_set_object_tag() {
395+
try {
396+
// First ensure a test object exists to put tags on.
397+
// Note this will override the existing object if exists.
398+
$key = $this->bucketkeyprefix . 'tagging_check_file';
399+
400+
$this->client->putObject([
401+
'Bucket' => $this->bucket,
402+
'Key' => $key,
403+
'Body' => 'test content'
404+
]);
405+
406+
// Next try to tag it.
407+
$res = $this->client->putObjectTagging([
408+
'Bucket' => $this->bucket,
409+
'Key' => $key,
410+
'Tagging' => [
411+
'TagSet' => [
412+
[
413+
'Key' => 'test',
414+
'Value' => 'test',
415+
]
416+
]
417+
]
418+
]);
419+
} catch (Throwable $e) {
420+
return (object) [
421+
'success' => false,
422+
'details' => $e->getMessage(),
423+
];
424+
}
425+
426+
// Success - no exceptions thrown.
427+
return (object) ['success' => true, 'details' => ''];
428+
}
429+
393430
/**
394431
* get_exception_details
395432
* @param \Exception $exception

classes/local/tag/tag_manager.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public static function get_defined_tag_sources(): array {
3232
* Is the tagging feature enabled?
3333
* @return bool
3434
*/
35-
public static function is_tagging_enabled(): bool {
35+
public static function is_tagging_enabled_and_supported(): bool {
3636
$enabledinconfig = !empty(get_config('tool_objectfs', 'taggingenabled'));
3737

3838
$client = manager::get_client(manager::get_objectfs_config());
@@ -133,7 +133,7 @@ private static function store_tags(string $contenthash, array $tags) {
133133
// We always mark them needing sync after changing the values.
134134
self::mark_object_tag_status($contenthash, self::SYNC_STATUS_NEEDS_SYNC);
135135
} catch (Exception $e) {
136-
// Failed - release lock and return false.
136+
// Failed - release lock and rethrow.
137137
$lock->release();
138138
throw $e;
139139
}
@@ -166,6 +166,11 @@ public static function replicate_local_to_external_tags_for_object(string $conte
166166

167167
// Get current client.
168168
$client = manager::get_client(manager::get_objectfs_config());
169+
170+
if (empty($client)) {
171+
throw new coding_exception("No client set, cannot replicate tags"); // TODO moodle ex ?
172+
}
173+
169174
$client->set_object_tags($contenthash, $tags);
170175

171176
// Mark as synced, this stops it from re-syncing in the future.

classes/local/tag/tag_source.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ interface tag_source {
66
/**
77
* Returns an unchanging identifier for this source.
88
* Must never change, otherwise it will lose connection with the tags replicated to objects.
9-
* If it ever must change, a migration step must be completed to trigger all objects to recalculate the value under the new tag name.
10-
* Must not exceed 128 chars. TODO unit test this.
9+
* If it ever must change, a migration step must be completed to trigger all objects to recalculate their tags.
10+
* Must not exceed 128 chars.
1111
* @return string
1212
*/
1313
public static function get_identifier(): string;
1414

1515
/**
1616
* Returns the value of this tag for the file with the given content hash.
17-
* This must be deterministic, and should never exceed 256 chars. TODO unit test this.
17+
* This must be deterministic, and should never exceed 256 chars.
1818
* @param string $contenthash
1919
* @return string
2020
*/

classes/task/queue_objects_needing_tags.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public function get_name() {
1212
}
1313

1414
public function execute() {
15-
if (!tag_manager::is_tagging_enabled()) {
15+
if (!tag_manager::is_tagging_enabled_and_supported()) {
1616
mtrace("Tagging feature not available or supported, ignoring.");
1717
return;
1818
}

classes/task/update_object_tags.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
class update_object_tags extends adhoc_task {
99
public function execute() {
10-
if (!tag_manager::is_tagging_enabled()) {
10+
if (!tag_manager::is_tagging_enabled_and_supported()) {
1111
mtrace("Tagging feature not enabled or supported by filesystem, exiting early.");
1212
return;
1313
}

lib.php

+7-4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
*/
2525

2626
use tool_objectfs\local\object_manipulator\manipulator_builder;
27+
use tool_objectfs\local\tag\tag_manager;
2728

2829
define('OBJECTFS_PLUGIN_NAME', 'tool_objectfs');
2930

@@ -120,11 +121,13 @@ function tool_objectfs_pluginfile($course, $cm, context $context, $filearea, arr
120121
* @return array
121122
*/
122123
function tool_objectfs_status_checks() {
124+
$checks = [
125+
new tool_objectfs\check\tagging_status(),
126+
];
127+
123128
if (get_config('tool_objectfs', 'proxyrangerequests')) {
124-
return [
125-
new tool_objectfs\check\proxy_range_request()
126-
];
129+
$checks[] = new tool_objectfs\check\proxy_range_request();
127130
}
128131

129-
return [];
132+
return $checks;
130133
}

tests/local/tagging_test.php

+24-4
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function test_tag_sources_identifier(tag_source $source) {
4040
$this->assertGreaterThan(0, $count);
4141
}
4242

43-
public static function is_tagging_enabled_provider(): array {
43+
public static function is_tagging_enabled_and_supported_provider(): array {
4444
return [
4545
'neither config nor fs supports' => [
4646
'enabledinconfig' => false,
@@ -61,9 +61,9 @@ public static function is_tagging_enabled_provider(): array {
6161
}
6262

6363
/**
64-
* @dataProvider is_tagging_enabled_provider
64+
* @dataProvider is_tagging_enabled_and_supported_provider
6565
*/
66-
public function test_is_tagging_enabled(bool $enabledinconfig, bool $supportedbyfs, bool $expected) {
66+
public function test_is_tagging_enabled_and_supported(bool $enabledinconfig, bool $supportedbyfs, bool $expected) {
6767
global $CFG;
6868
// Set config.
6969
set_config('taggingenabled', $enabledinconfig, 'tool_objectfs');
@@ -76,7 +76,7 @@ public function test_is_tagging_enabled(bool $enabledinconfig, bool $supportedby
7676
manager::set_objectfs_config($config);
7777
$CFG->phpunit_objectfs_supports_object_tagging = $supportedbyfs;
7878

79-
$this->assertEquals($expected, tag_manager::is_tagging_enabled());
79+
$this->assertEquals($expected, tag_manager::is_tagging_enabled_and_supported());
8080
}
8181

8282
public function test_query_local_tags() {
@@ -210,4 +210,24 @@ public function test_get_objects_needing_sync_limit() {
210210
$this->assertCount(2, tag_manager::get_objects_needing_sync(2));
211211
$this->assertCount(1, tag_manager::get_objects_needing_sync(1));
212212
}
213+
214+
public function test_replicate_local_to_external_tags_for_object() {
215+
global $DB;
216+
217+
// Enable tagging, setup test fs.
218+
$config = manager::get_objectfs_config();
219+
$config->taggingenabled = true;
220+
$config->enabletasks = true;
221+
$config->filesystem = '\\tool_objectfs\\tests\\test_file_system';
222+
manager::set_objectfs_config($config);
223+
224+
// Setup object, mark as needing sync.
225+
$object = $this->create_remote_object();
226+
$DB->set_field('tool_objectfs_objects', 'tagsyncstatus', tag_manager::SYNC_STATUS_NEEDS_SYNC, ['id' => $object->id]);
227+
tag_manager::replicate_local_to_external_tags_for_object($object->contenthash);
228+
229+
// Ensure status is now set as not needing sync.
230+
$status = $DB->get_field('tool_objectfs_objects', 'tagsyncstatus', ['id' => $object->id]);
231+
$this->assertEquals(tag_manager::SYNC_STATUS_SYNC_NOT_REQUIRED, $status);
232+
}
213233
}

0 commit comments

Comments
 (0)