50
50
use Google \Cloud \ModelArmor \V1 \RaiFilterType ;
51
51
use Google \Cloud \ModelArmor \V1 \RaiFilterSettings ;
52
52
use Google \Cloud \ModelArmor \V1 \RaiFilterSettings \RaiFilter ;
53
+ use Google \Cloud \ModelArmor \V1 \FloorSetting ;
54
+ use Google \Cloud \ModelArmor \V1 \UpdateFloorSettingRequest ;
53
55
54
56
class modelarmorTest extends TestCase
55
57
{
@@ -76,6 +78,8 @@ class modelarmorTest extends TestCase
76
78
protected static $ testRaiTemplateId ;
77
79
protected static $ testMaliciousTemplateId ;
78
80
protected static $ testPIandJailbreakTemplateId ;
81
+ protected static $ organizationId ;
82
+ protected static $ folderId ;
79
83
80
84
public static function setUpBeforeClass (): void
81
85
{
@@ -96,10 +100,23 @@ public static function setUpBeforeClass(): void
96
100
self ::$ testSanitizeModelResponseUserPromptId = self ::getTemplateId ('php-sanitize-model-response-user-prompt- ' );
97
101
self ::$ testRaiTemplateId = self ::getTemplateId ('php-rai-template- ' );
98
102
self ::$ testMaliciousTemplateId = self ::getTemplateId ('php-malicious-template- ' );
99
- self ::$ testPIandJailbreakTemplateId = self ::getTemplateId ('php-pi-and-jailbreak-template- ' );
103
+ self ::$ testPIandJailbreakTemplateId = self ::getTemplateId ('php-template-with-pijailbreak- ' );
104
+ self ::$ organizationId = self ::requireEnv ('MA_ORG_ID ' );
105
+ self ::$ folderId = self ::requireEnv ('MA_FOLDER_ID ' );
100
106
self ::createTemplateWithMaliciousURI ();
101
107
self ::createTemplateWithPIJailbreakFilter ();
102
108
self ::createTemplateWithRAI ();
109
+
110
+ // Reset floor settings before tests
111
+ if (self ::$ projectId ) {
112
+ self ::resetFloorSettings ('project ' , self ::$ projectId );
113
+ }
114
+ if (self ::$ folderId ) {
115
+ self ::resetFloorSettings ('folder ' , self ::$ folderId );
116
+ }
117
+ if (self ::$ organizationId ) {
118
+ self ::resetFloorSettings ('organization ' , self ::$ organizationId );
119
+ }
103
120
}
104
121
105
122
public static function tearDownAfterClass (): void
@@ -122,6 +139,18 @@ public static function tearDownAfterClass(): void
122
139
self ::deleteTemplate (self ::$ projectId , self ::$ locationId , self ::$ testMaliciousTemplateId );
123
140
self ::deleteTemplate (self ::$ projectId , self ::$ locationId , self ::$ testPIandJailbreakTemplateId );
124
141
self ::deleteDlpTemplates (self ::$ inspectTemplateName , self ::$ deidentifyTemplateName , self ::$ locationId );
142
+
143
+ // Reset floor settings after tests
144
+ if (self ::$ projectId ) {
145
+ self ::resetFloorSettings ('project ' , self ::$ projectId );
146
+ }
147
+ if (self ::$ folderId ) {
148
+ self ::resetFloorSettings ('folder ' , self ::$ folderId );
149
+ }
150
+ if (self ::$ organizationId ) {
151
+ self ::resetFloorSettings ('organization ' , self ::$ organizationId );
152
+ }
153
+
125
154
self ::$ client ->close ();
126
155
}
127
156
@@ -143,6 +172,48 @@ public static function getTemplateId(string $testId): string
143
172
return uniqid ($ testId );
144
173
}
145
174
175
+ /**
176
+ * Resets floor settings to default values for various resource types
177
+ *
178
+ * @param string $resourceType The type of resource (project, folder, organization)
179
+ * @param string $resourceId The ID of the resource
180
+ */
181
+ protected static function resetFloorSettings (string $ resourceType , string $ resourceId ): void
182
+ {
183
+ try {
184
+ $ client = new ModelArmorClient ();
185
+
186
+ // Format resource path based on resource type
187
+ $ resourcePathFormat = match ($ resourceType ) {
188
+ 'project ' => 'projects/%s/locations/global/floorSetting ' ,
189
+ 'folder ' => 'folders/%s/locations/global/floorSetting ' ,
190
+ 'organization ' => 'organizations/%s/locations/global/floorSetting ' ,
191
+ default => throw new \InvalidArgumentException ("Invalid resource type: {$ resourceType }" ),
192
+ };
193
+
194
+ $ floorSettingsName = sprintf ($ resourcePathFormat , $ resourceId );
195
+
196
+ // Create an empty filter config
197
+ $ filterConfig = new FilterConfig ();
198
+
199
+ // Create floor setting with enforcement disabled
200
+ $ floorSetting = (new FloorSetting ())
201
+ ->setName ($ floorSettingsName )
202
+ ->setFilterConfig ($ filterConfig )
203
+ ->setEnableFloorSettingEnforcement (false );
204
+
205
+ $ updateRequest = (new UpdateFloorSettingRequest ())->setFloorSetting ($ floorSetting );
206
+ $ response = $ client ->updateFloorSetting ($ updateRequest );
207
+
208
+ echo "Floor settings reset for {$ resourceType } {$ resourceId }\n" ;
209
+ } catch (\Exception $ e ) {
210
+ // Log but don't fail teardown if reset fails
211
+ echo "Warning: Failed to reset {$ resourceType } floor settings: " . $ e ->getMessage () . "\n" ;
212
+ }
213
+ }
214
+
215
+ // Wrapper methods removed in favor of directly calling resetFloorSettings
216
+
146
217
public function testCreateTemplate ()
147
218
{
148
219
$ output = $ this ->runFunctionSnippet ('create_template ' , [
@@ -696,5 +767,63 @@ protected static function createTemplate($templateId, $template)
696
767
}
697
768
}
698
769
699
- # TODO: Add tests for floor settings once API issues are resolved.
770
+ public function testGetFolderFloorSettings ()
771
+ {
772
+ $ output = $ this ->runSnippet ('get_folder_floor_settings ' , [
773
+ self ::$ folderId ,
774
+ ]);
775
+
776
+ $ expectedResponseString = 'Floor settings retrieved successfully: ' ;
777
+ $ this ->assertStringContainsString ($ expectedResponseString , $ output );
778
+ }
779
+
780
+ public function testGetProjectFloorSettings ()
781
+ {
782
+ $ output = $ this ->runSnippet ('get_project_floor_settings ' , [
783
+ self ::$ projectId ,
784
+ ]);
785
+
786
+ $ expectedResponseString = 'Floor settings retrieved successfully: ' ;
787
+ $ this ->assertStringContainsString ($ expectedResponseString , $ output );
788
+ }
789
+
790
+ public function testGetOrganizationFloorSettings ()
791
+ {
792
+ $ output = $ this ->runSnippet ('get_organization_floor_settings ' , [
793
+ self ::$ organizationId ,
794
+ ]);
795
+
796
+ $ expectedResponseString = 'Floor settings retrieved successfully: ' ;
797
+ $ this ->assertStringContainsString ($ expectedResponseString , $ output );
798
+ }
799
+
800
+ public function testUpdateFolderFloorSettings ()
801
+ {
802
+ $ output = $ this ->runSnippet ('update_folder_floor_settings ' , [
803
+ self ::$ folderId ,
804
+ ]);
805
+
806
+ $ expectedResponseString = 'Floor setting updated ' ;
807
+ $ this ->assertStringContainsString ($ expectedResponseString , $ output );
808
+ }
809
+
810
+ public function testUpdateProjectFloorSettings ()
811
+ {
812
+ $ output = $ this ->runSnippet ('update_project_floor_settings ' , [
813
+ self ::$ projectId ,
814
+ ]);
815
+
816
+ $ expectedResponseString = 'Floor setting updated ' ;
817
+ $ this ->assertStringContainsString ($ expectedResponseString , $ output );
818
+ }
819
+
820
+ public function testUpdateOrganizationFloorSettings ()
821
+ {
822
+ $ output = $ this ->runSnippet ('update_organization_floor_settings ' , [
823
+ self ::$ organizationId ,
824
+ ]);
825
+
826
+ $ expectedResponseString = 'Floor setting updated ' ;
827
+ $ this ->assertStringContainsString ($ expectedResponseString , $ output );
828
+ }
700
829
}
0 commit comments