Skip to content

Commit dfe02d6

Browse files
committed
WIP more tweaks
1 parent e7a8f78 commit dfe02d6

9 files changed

+45
-28
lines changed

classes/local/store/object_file_system.php

+8-5
Original file line numberDiff line numberDiff line change
@@ -1160,11 +1160,15 @@ private function update_object(array $result): array {
11601160

11611161
/**
11621162
* Syncs tags post upload for a given hash.
1163-
* Note this function assumes tagging is enable and supported by the fs.
1163+
* External client must support tagging.
11641164
*
11651165
* @param string $contenthash file to sync tags for
11661166
*/
11671167
public function sync_object_tags(string $contenthash) {
1168+
if (!$this->get_external_client()->is_tagging_supported()) {
1169+
throw new coding_exception("Cannot sync tags, external client does not support tagging.");
1170+
}
1171+
11681172
// Get a lock before syncing, to ensure other parts of objectfs are not moving/interacting with this object.
11691173
$lock = $this->acquire_object_lock($contenthash, 5);
11701174

@@ -1177,18 +1181,17 @@ public function sync_object_tags(string $contenthash) {
11771181
$objectexists = $this->is_file_readable_externally_by_hash($contenthash);
11781182
// If object does not exist, cannot sync tags to nothing, abort.
11791183
if (!$objectexists) {
1180-
// TODO maybe this should be a failed status?
11811184
tag_manager::mark_object_tag_sync_status($contenthash, tag_manager::SYNC_STATUS_SYNC_NOT_REQUIRED);
11821185
return;
11831186
}
11841187

1185-
// Cannot override and object exists, query existing and store.
1186-
if (!manager::can_override_existing_objects() && $objectexists) {
1188+
// Cannot override and object exists, query existing and store locally.
1189+
if ($objectexists && !manager::can_override_existing_objects()) {
11871190
// Query existing tags and store them.
11881191
$existingtags = $this->get_external_client()->get_object_tags($contenthash);
11891192
tag_manager::store_tags_locally($contenthash, $existingtags);
11901193

1191-
// Else can override, upload new and store.
1194+
// Else can override, upload new and store locally.
11921195
} else {
11931196
$tags = tag_manager::gather_object_tags_for_upload($contenthash);
11941197
$this->get_external_client()->set_object_tags($contenthash, $tags);

classes/local/store/s3/client.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -929,7 +929,7 @@ public function test_set_object_tag(): stdClass {
929929
* @return array tags in s3 format.
930930
*/
931931
private function convert_tags_to_s3_format(array $tags): array {
932-
foreach($tags as $key => $value) {
932+
foreach ($tags as $key => $value) {
933933
$s3tags[] = [
934934
'Key' => $key,
935935
'Value' => $value,
@@ -972,7 +972,7 @@ public function get_object_tags(string $contenthash): array {
972972

973973
// Ensure tags are what we expect, and AWS have not changed the format.
974974
if (empty($result->toArray()['TagSet'])) {
975-
throw new coding_exception("Unexpected tag format received"); // TODO different ex type.
975+
throw new coding_exception("Unexpected tag format received. Result did not contain a TagSet");
976976
}
977977

978978
// Convert from S3 format to key=>value format.

classes/local/store/s3/file_system.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public function copy_from_local_to_external($contenthash) {
8989

9090
try {
9191
// If cannot override, and already exists, skip.
92-
// We don't want to upload and potentially override.
92+
// We don't want to upload and potentially override the object, or associated data e.g. tags.
9393
if (!manager::can_override_existing_objects() && $this->is_file_readable_externally_by_hash($contenthash)) {
9494
return true;
9595
}

classes/local/tag/environment_source.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,21 @@ public static function get_identifier(): string {
3333
return 'environment';
3434
}
3535

36+
/**
37+
* Description for source displayed in the admin settings.
38+
* @return string
39+
*/
3640
public static function get_description(): string {
3741
return get_string('tagsource:environment', 'tool_objectfs', self::get_env());
3842
}
3943

44+
/**
45+
* Returns current env value from $CFG
46+
* @return string|null string if set, else null
47+
*/
4048
private static function get_env(): ?string {
4149
global $CFG;
42-
return $CFG->objectfs_environment_name ?? null;
50+
return !empty($CFG->objectfs_environment_name) ? $CFG->objectfs_environment_name : null;
4351
}
4452

4553
/**

classes/local/tag/mime_type_source.php

+4
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ public static function get_identifier(): string {
3333
return 'mimetype';
3434
}
3535

36+
/**
37+
* Description for source displayed in the admin settings.
38+
* @return string
39+
*/
3640
public static function get_description(): string {
3741
return get_string('tagsource:mimetype', 'tool_objectfs');
3842
}

classes/local/tag/tag_manager.php

-9
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@
1717
namespace tool_objectfs\local\tag;
1818

1919
use coding_exception;
20-
use html_writer;
2120
use tool_objectfs\local\manager;
22-
use tool_objectfs\local\store\object_file_system;
2321

2422
defined('MOODLE_INTERNAL') || die();
2523
require_once($CFG->dirroot . '/admin/tool/objectfs/lib.php');
@@ -78,11 +76,6 @@ public static function is_tagging_enabled_and_supported(): bool {
7876
return $enabledinconfig && $supportedbyfs;
7977
}
8078

81-
// TODO unit tests for this functionality
82-
public static function can_override_existing_tags(): bool {
83-
return !empty(get_config('tool_objectfs', 'overridetags'));
84-
}
85-
8679
/**
8780
* Gathers the tag values for a given content hash
8881
* @param string $contenthash
@@ -141,8 +134,6 @@ public static function get_objects_needing_sync(int $limit) {
141134
// Find object records where the status is NEEDS_SYNC and is replicated.
142135
[$insql, $inparams] = $DB->get_in_or_equal([OBJECT_LOCATION_DUPLICATED, OBJECT_LOCATION_EXTERNAL, OBJECT_LOCATION_ORPHANED], SQL_PARAMS_NAMED);
143136
$inparams['syncstatus'] = self::SYNC_STATUS_NEEDS_SYNC;
144-
145-
// TODO somehow exclude ones already queued ?
146137
$records = $DB->get_records_select('tool_objectfs_objects', 'tagsyncstatus = :syncstatus AND location ' . $insql, $inparams, '', 'contenthash', 0, $limit);
147138
return array_column($records, 'contenthash');
148139
}

db/tasks.php

+1
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@
116116
'dayofweek' => '*',
117117
'month' => '*',
118118
// Default disabled - intended to be manually run.
119+
// Also, objectfs tagging support is default off.
119120
'disabled' => true,
120121
),
121122
);

tests/local/tagging_test.php

+19-10
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public function test_tag_sources_identifier(tag_source $source) {
7777
*/
7878
public function test_tag_sources_value(tag_source $source) {
7979
// Ensure tag value is < 128 chars, AWS & Azure spec allow for 256, but we reserve 128 for future use.
80-
$file = $this->create_duplicated_object();
80+
$file = $this->create_duplicated_object('tag source value test ' . $source->get_identifier());
8181
$value = $source->get_value_for_contenthash($file->contenthash);
8282

8383
// Null value - allowed, but means we cannot test.
@@ -90,9 +90,6 @@ public function test_tag_sources_value(tag_source $source) {
9090
$this->assertGreaterThan(0, $count);
9191
}
9292

93-
// TODO ensure all sources have unique keys.
94-
// TODO ensure keys and values are all strings.
95-
9693
/**
9794
* Provides values to test_is_tagging_enabled_and_supported
9895
* @return array
@@ -141,8 +138,12 @@ public function test_is_tagging_enabled_and_supported(bool $enabledinconfig, boo
141138
$this->assertEquals($expected, tag_manager::is_tagging_enabled_and_supported());
142139
}
143140

141+
/**
142+
* Tests gather_object_tags_for_upload
143+
* @covers \tool_objectfs\local\tag_manager::gather_object_tags_for_upload
144+
*/
144145
public function test_gather_object_tags_for_upload() {
145-
$object = $this->create_duplicated_object();
146+
$object = $this->create_duplicated_object('gather tags for upload test');
146147
$tags = tag_manager::gather_object_tags_for_upload($object->contenthash);
147148

148149
$this->assertArrayHasKey('mimetype', $tags);
@@ -151,6 +152,10 @@ public function test_gather_object_tags_for_upload() {
151152
$this->assertEquals('test', $tags['environment']);
152153
}
153154

155+
/**
156+
* Tests store_tags_locally
157+
* @covers \tool_objectfs\local\tag_manager::store_tags_locally
158+
*/
154159
public function test_store_tags_locally() {
155160
global $DB;
156161

@@ -228,13 +233,13 @@ public function test_get_objects_needing_sync(int $location, int $syncstatus, bo
228233
// Create the test object at the required location.
229234
switch ($location) {
230235
case OBJECT_LOCATION_DUPLICATED:
231-
$object = $this->create_duplicated_object();
236+
$object = $this->create_duplicated_object('tagging test object duplicated');
232237
break;
233238
case OBJECT_LOCATION_LOCAL:
234-
$object = $this->create_local_object();
239+
$object = $this->create_local_object('tagging test object local');
235240
break;
236241
case OBJECT_LOCATION_EXTERNAL:
237-
$object = $this->create_remote_object();
242+
$object = $this->create_remote_object('tagging test object remote');
238243
break;
239244
default:
240245
throw new coding_exception("Object location not handled in test");
@@ -261,16 +266,20 @@ public function test_get_objects_needing_sync_limit() {
261266
global $DB;
262267

263268
// Create two duplicated objects needing sync.
264-
$object = $this->create_duplicated_object();
269+
$object = $this->create_duplicated_object('sync limit test duplicated');
265270
$DB->set_field('tool_objectfs_objects', 'tagsyncstatus', tag_manager::SYNC_STATUS_NEEDS_SYNC, ['id' => $object->id]);
266-
$object = $this->create_remote_object();
271+
$object = $this->create_remote_object('sync limit test remote');
267272
$DB->set_field('tool_objectfs_objects', 'tagsyncstatus', tag_manager::SYNC_STATUS_NEEDS_SYNC, ['id' => $object->id]);
268273

269274
// Ensure a limit of 2 returns 2, and limit of 1 returns 1.
270275
$this->assertCount(2, tag_manager::get_objects_needing_sync(2));
271276
$this->assertCount(1, tag_manager::get_objects_needing_sync(1));
272277
}
273278

279+
/**
280+
* Test get_tag_summary_html
281+
* @covers \tool_objectfs\local\tag_manager::get_tag_summary_html
282+
*/
274283
public function test_get_tag_summary_html() {
275284
// Quick test just to ensure it generates and nothing explodes.
276285
$html = tag_manager::get_tag_summary_html();

tests/task/populate_objects_filesize_test.php

+1
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ public function test_that_non_null_values_are_not_updated() {
179179
*/
180180
public function test_orphaned_objects_are_not_updated() {
181181
global $DB;
182+
$objects = $DB->get_records('tool_objectfs_objects');
182183
$file1 = $this->create_local_file("Test 1");
183184
$this->create_local_file("Test 2");
184185
$this->create_local_file("Test 3");

0 commit comments

Comments
 (0)