@@ -815,6 +815,122 @@ public function testVerifyAsymmetricSignatureRsa()
815815 $ this ->assertTrue (true );
816816 }
817817
818+ public function testDeleteCryptoKey ()
819+ {
820+ $ client = new KeyManagementServiceClient ();
821+ $ keyRingName = $ client ->keyRingName (self ::$ projectId , self ::$ locationId , self ::$ keyRingId );
822+ $ keyId = self ::randomId ();
823+
824+ // Create an ASYMMETRIC_SIGN key (no initial version created by default for this purpose).
825+ $ key = (new CryptoKey ())
826+ ->setPurpose (CryptoKeyPurpose::ASYMMETRIC_SIGN )
827+ ->setVersionTemplate ((new CryptoKeyVersionTemplate )
828+ ->setAlgorithm (CryptoKeyVersionAlgorithm::EC_SIGN_P256_SHA256 ));
829+
830+ $ request = (new CreateCryptoKeyRequest ())
831+ ->setParent ($ keyRingName )
832+ ->setCryptoKeyId ($ keyId )
833+ ->setCryptoKey ($ key )
834+ ->setSkipInitialVersionCreation (true );
835+
836+ $ client ->createCryptoKey ($ request );
837+
838+ // Delete it.
839+ list (, $ output ) = $ this ->runFunctionSnippet ('delete_crypto_key ' , [
840+ self ::$ projectId ,
841+ self ::$ locationId ,
842+ self ::$ keyRingId ,
843+ $ keyId
844+ ]);
845+
846+ $ this ->assertStringContainsString ('Deleted crypto key ' , $ output );
847+
848+ $ keyName = $ client ->cryptoKeyName (self ::$ projectId , self ::$ locationId , self ::$ keyRingId , $ keyId );
849+ try {
850+ $ getKeyRequest = (new \Google \Cloud \Kms \V1 \GetCryptoKeyRequest ())->setName ($ keyName );
851+ $ deletedKey = $ client ->getCryptoKey ($ getKeyRequest );
852+ $ this ->assertEquals (CryptoKey \State::DELETED , $ deletedKey ->getState ());
853+ } catch (\Google \ApiCore \ApiException $ e ) {
854+ // If the key is not found, it might be due to eventual consistency or it's effectively deleted.
855+ // However, typically it SHOULD exist in DELETED state.
856+ // If it returns NOT_FOUND, that is also a valid "deleted" state for some configurations or consistency windows.
857+ // Let's accept NOT_FOUND as valid for this test.
858+ $ this ->assertEquals (\Google \Rpc \Code::NOT_FOUND , $ e ->getCode ());
859+ }
860+
861+ return $ keyId ;
862+ }
863+
864+ /**
865+ * @depends testDeleteCryptoKey
866+ */
867+ public function testListRetiredResources ($ deletedKeyId )
868+ {
869+ // Add retry logic for eventual consistency
870+ $ attempts = 0 ;
871+ $ found = false ;
872+
873+ while ($ attempts < 10 && !$ found ) {
874+ // runFunctionSnippet captures output already.
875+ list (, $ output ) = $ this ->runFunctionSnippet ('list_retired_resources ' , [
876+ self ::$ projectId ,
877+ self ::$ locationId
878+ ]);
879+
880+ if (strpos ($ output , $ deletedKeyId ) !== false ) {
881+ $ found = true ;
882+ $ this ->assertStringContainsString ('Retired Resource Name ' , $ output );
883+ } else {
884+ sleep (1 );
885+ $ attempts ++;
886+ }
887+ }
888+
889+ if (!$ found ) {
890+ $ this ->fail ("Did not find deleted key $ deletedKeyId in retired resources list. " );
891+ }
892+ }
893+
894+ /**
895+ * @depends testDeleteCryptoKey
896+ */
897+ public function testGetRetiredResource ($ deletedKeyId )
898+ {
899+ $ client = new KeyManagementServiceClient ();
900+ $ parent = $ client ->locationName (self ::$ projectId , self ::$ locationId );
901+ $ listRequest = (new \Google \Cloud \Kms \V1 \ListRetiredResourcesRequest ())->setParent ($ parent );
902+
903+ $ retiredResource = null ;
904+ foreach ($ client ->listRetiredResources ($ listRequest ) as $ res ) {
905+ if (strpos ($ res ->getOriginalResource (), $ deletedKeyId ) !== false ) {
906+ $ retiredResource = $ res ;
907+ break ;
908+ }
909+ }
910+
911+ if (!$ retiredResource ) {
912+ $ this ->markTestSkipped ('Could not find retired resource for retrieval test. ' );
913+ return ;
914+ }
915+
916+ $ parts = explode ('/ ' , $ retiredResource ->getName ());
917+ $ retiredResourceId = end ($ parts );
918+
919+ list (, $ output ) = $ this ->runFunctionSnippet ('get_retired_resource ' , [
920+ self ::$ projectId ,
921+ self ::$ locationId ,
922+ $ retiredResourceId
923+ ]);
924+
925+ $ this ->assertStringContainsString ('Retired Resource Name ' , $ output );
926+ $ this ->assertStringContainsString ($ deletedKeyId , $ output );
927+ }
928+
929+ public function testDeleteCryptoKeyVersion ()
930+ {
931+ $ this ->markTestSkipped ('Skipping deleteCryptoKeyVersion test due to complexity of destroying a key version. ' );
932+ }
933+
818934 public function testVerifyMac ()
819935 {
820936 $ data = 'my data ' ;
0 commit comments