|
16 | 16 |
|
17 | 17 | namespace tool_objectfs;
|
18 | 18 |
|
| 19 | +use coding_exception; |
19 | 20 | use tool_objectfs\local\store\object_file_system;
|
20 | 21 | use tool_objectfs\local\manager;
|
| 22 | +use tool_objectfs\local\tag\tag_manager; |
21 | 23 | use tool_objectfs\tests\test_file_system;
|
22 | 24 |
|
23 | 25 | /**
|
@@ -1017,5 +1019,104 @@ public function test_add_file_from_string_update_object_fail() {
|
1017 | 1019 | $this->assertTrue($result[2]);
|
1018 | 1020 | }
|
1019 | 1021 |
|
1020 |
| - // TODO test tag syncing somehow ? Using a test FS ? Maybe test FS can store them in memory ? |
| 1022 | + /** |
| 1023 | + * Test syncing tags throws exception when client does not support tagging. |
| 1024 | + */ |
| 1025 | + public function test_sync_object_tags_not_supported() { |
| 1026 | + global $CFG; |
| 1027 | + $CFG->phpunit_objectfs_supports_object_tagging = false; |
| 1028 | + $this->expectException(coding_exception::class); |
| 1029 | + $this->expectExceptionMessage('Cannot sync tags, external client does not support tagging'); |
| 1030 | + $this->filesystem->sync_object_tags('123'); |
| 1031 | + } |
| 1032 | + |
| 1033 | + /** |
| 1034 | + * Tests syncing object tags where the file is not replicated. |
| 1035 | + */ |
| 1036 | + public function test_sync_object_tags_object_not_replicated() { |
| 1037 | + global $CFG, $DB; |
| 1038 | + $CFG->phpunit_objectfs_supports_object_tagging = true; |
| 1039 | + |
| 1040 | + // Create object - not replicated to 'external' store yet. |
| 1041 | + $object = $this->create_local_object('test syncing local'); |
| 1042 | + |
| 1043 | + // Sync, this should do nothing but change sync status - cannot sync object tags |
| 1044 | + // where the object is not replicated. |
| 1045 | + $this->filesystem->sync_object_tags($object->contenthash); |
| 1046 | + $object = $DB->get_record('tool_objectfs_objects', ['contenthash' => $object->contenthash]); |
| 1047 | + $this->assertEquals($object->tagsyncstatus, tag_manager::SYNC_STATUS_SYNC_NOT_REQUIRED); |
| 1048 | + } |
| 1049 | + |
| 1050 | + /** |
| 1051 | + * Provides values to sync_object_tags_replicated |
| 1052 | + * @return array |
| 1053 | + */ |
| 1054 | + public static function sync_object_tags_replicated_provider(): array { |
| 1055 | + return [ |
| 1056 | + 'can override' => [ |
| 1057 | + 'can override' => true, |
| 1058 | + ], |
| 1059 | + 'cannot override' => [ |
| 1060 | + 'cannot override' => false, |
| 1061 | + ], |
| 1062 | + ]; |
| 1063 | + } |
| 1064 | + |
| 1065 | + /** |
| 1066 | + * Tests sync_object_tags when the object is replicated. |
| 1067 | + * @dataProvider sync_object_tags_replicated_provider |
| 1068 | + */ |
| 1069 | + public function test_sync_object_tags_replicated(bool $canoverride) { |
| 1070 | + global $CFG, $DB; |
| 1071 | + $CFG->phpunit_objectfs_supports_object_tagging = true; |
| 1072 | + |
| 1073 | + set_config('canoverrideobjects', $canoverride, 'tool_objectfs'); |
| 1074 | + $this->assertEquals($canoverride, manager::can_override_existing_objects()); |
| 1075 | + |
| 1076 | + $object = $this->create_duplicated_object('test syncing replicated'); |
| 1077 | + |
| 1078 | + $testtags = [ |
| 1079 | + 'test' => 123, |
| 1080 | + 'test2' => 123, |
| 1081 | + 'test3' => 123, |
| 1082 | + 'test4' => 123, |
| 1083 | + ]; |
| 1084 | + |
| 1085 | + // Fake set the tags in the external store. |
| 1086 | + $this->filesystem->get_external_client()->tags[$object->contenthash] = $testtags; |
| 1087 | + |
| 1088 | + // Ensure tags are set 'externally'. |
| 1089 | + $tags = $this->filesystem->get_external_client()->get_object_tags($object->contenthash); |
| 1090 | + $this->assertCount(count($testtags), $tags); |
| 1091 | + |
| 1092 | + // But tags will not be stored locally (yet). |
| 1093 | + $localtags = $DB->get_records('tool_objectfs_object_tags', ['contenthash' => $object->contenthash]); |
| 1094 | + $this->assertCount(0, $localtags); |
| 1095 | + |
| 1096 | + // Sync the file. |
| 1097 | + $this->filesystem->sync_object_tags($object->contenthash); |
| 1098 | + |
| 1099 | + // Tags should now be replicated locally. |
| 1100 | + $localtags = $DB->get_records('tool_objectfs_object_tags', ['contenthash' => $object->contenthash]); |
| 1101 | + $externaltags = $this->filesystem->get_external_client()->get_object_tags($object->contenthash); |
| 1102 | + |
| 1103 | + if ($canoverride) { |
| 1104 | + // If can override, we expect it to be overwritten by the tags defined in the sources. |
| 1105 | + $expectednum = count(tag_manager::get_defined_tag_sources()); |
| 1106 | + $this->assertCount($expectednum, $localtags); |
| 1107 | + |
| 1108 | + // Also expect the external store to be updated. |
| 1109 | + $this->assertCount($expectednum, $externaltags); |
| 1110 | + } else { |
| 1111 | + // If cannot override, we expect the tags to match what was in the 'external' store. |
| 1112 | + $this->assertCount(count($testtags), $localtags); |
| 1113 | + |
| 1114 | + // External store should not be updated. |
| 1115 | + $this->assertCount(count($testtags), $externaltags); |
| 1116 | + } |
| 1117 | + |
| 1118 | + // Ensure status changed to not needing sync. |
| 1119 | + $object = $DB->get_record('tool_objectfs_objects', ['contenthash' => $object->contenthash]); |
| 1120 | + $this->assertEquals($object->tagsyncstatus, tag_manager::SYNC_STATUS_SYNC_NOT_REQUIRED); |
| 1121 | + } |
1021 | 1122 | }
|
0 commit comments