22
22
23
23
use Behat \Behat \Hook \Scope \BeforeScenarioScope ;
24
24
use Behat \Behat \Context \Context ;
25
+ use Behat \Gherkin \Node \PyStringNode ;
25
26
use Behat \Gherkin \Node \TableNode ;
26
27
use GuzzleHttp \Exception \GuzzleException ;
27
28
use PHPUnit \Framework \Assert ;
@@ -295,10 +296,8 @@ public function theAdministratorRemovesTheVersionsOfFilesInSpaceUsingSpaceId(str
295
296
* @return void
296
297
*/
297
298
public function theCommandShouldBeSuccessful (string $ successfulOrNot ): void {
298
- $ response = $ this ->featureContext ->getResponse ();
299
- $ this ->featureContext ->theHTTPStatusCodeShouldBe (200 , '' , $ response );
300
-
301
- $ jsonResponse = $ this ->featureContext ->getJsonDecodedResponse ($ response );
299
+ $ this ->featureContext ->theHTTPStatusCodeShouldBe (200 );
300
+ $ jsonResponse = $ this ->featureContext ->getJsonDecodedResponse ();
302
301
303
302
$ expectedStatus = 'OK ' ;
304
303
$ expectedExitCode = 0 ;
@@ -480,6 +479,16 @@ public function cleanUploadsSessions(): void {
480
479
Assert::assertEquals ("200 " , $ response ->getStatusCode (), "Failed to clean upload sessions " );
481
480
}
482
481
482
+ /**
483
+ * @AfterScenario @cli-stale-uploads
484
+ *
485
+ * @return void
486
+ */
487
+ public function cleanUpStaleUploads (): void {
488
+ $ response = $ this ->deleteStaleUploads ();
489
+ $ this ->featureContext ->theHTTPStatusCodeShouldBe (200 , "Failed to cleanup stale upload " , $ response );
490
+ }
491
+
483
492
/**
484
493
* @When /^the administrator triggers "([^"]*)" email notifications using the CLI$/
485
494
*
@@ -496,4 +505,172 @@ public function theAdministratorTriggersEmailNotificationsUsingTheCLI(string $in
496
505
497
506
$ this ->featureContext ->setResponse (CliHelper::runCommand ($ body ));
498
507
}
508
+
509
+ /**
510
+ * @Given the administrator has created stale upload
511
+ *
512
+ * @return void
513
+ */
514
+ public function theAdministratorHasCreatedStaleUpload (): void {
515
+ $ folderPath = $ this ->featureContext ->getStorageUsersRoot () . "/uploads " ;
516
+ $ infoFiles = glob ($ folderPath . '/*.info ' );
517
+ foreach ($ infoFiles as $ file ) {
518
+ if (!unlink ($ file )) {
519
+ Assert::fail ("Fail to delete info file " );
520
+ }
521
+ }
522
+ }
523
+
524
+ /**
525
+ * @param string|null $spaceId
526
+ *
527
+ * @return ResponseInterface
528
+ * @throws GuzzleException
529
+ */
530
+ protected function listStaleUploads (?string $ spaceId = null ): ResponseInterface {
531
+ $ command = "storage-users uploads delete-stale-nodes --dry-run=true " ;
532
+
533
+ if ($ spaceId !== null ) {
534
+ $ command .= " --spaceid= $ spaceId " ;
535
+ }
536
+
537
+ $ body = [
538
+ "command " => $ command
539
+ ];
540
+ return CliHelper::runCommand ($ body );
541
+ }
542
+
543
+ /**
544
+ * @param string|null $spaceId
545
+ *
546
+ * @return ResponseInterface
547
+ * @throws GuzzleException
548
+ */
549
+ protected function deleteStaleUploads (?string $ spaceId = null ): ResponseInterface {
550
+ $ command = "storage-users uploads delete-stale-nodes --dry-run=false " ;
551
+ if ($ spaceId !== null ) {
552
+ $ command .= " --spaceid= $ spaceId " ;
553
+ }
554
+
555
+ $ body = [
556
+ "command " => $ command
557
+ ];
558
+ return CliHelper::runCommand ($ body );
559
+ }
560
+
561
+ /**
562
+ * @When the administrator lists all the stale uploads
563
+ *
564
+ * @return void
565
+ */
566
+ public function theAdministratorListsAllTheStaleUploads (): void {
567
+ $ this ->featureContext ->setResponse ($ this ->listStaleUploads ());
568
+ }
569
+
570
+ /**
571
+ * @When the administrator lists all the stale uploads of space :space owned by user :user
572
+ *
573
+ * @param string $spaceName
574
+ * @param string $user
575
+ *
576
+ * @return void
577
+ */
578
+ public function theAdministratorListsTheStaleUploadsOfSpace (
579
+ string $ spaceName ,
580
+ string $ user
581
+ ): void {
582
+ $ space = $ this ->spacesContext ->getSpaceByName (
583
+ $ user ,
584
+ $ spaceName
585
+ );
586
+ $ spaceOwnerId = $ space ["owner " ]["user " ]["id " ];
587
+ $ this ->featureContext ->setResponse ($ this ->listStaleUploads ($ spaceOwnerId ));
588
+ }
589
+
590
+ /**
591
+ * @Then the CLI response should contain the following message:
592
+ *
593
+ * @param PyStringNode $content
594
+ *
595
+ * @return void
596
+ */
597
+ public function theCLIResponseShouldContainTheseMessage (PyStringNode $ content ): void {
598
+ $ response = $ this ->featureContext ->getJsonDecodedResponseBodyContent ();
599
+ $ expectedMessage = str_replace ("\r\n" , "\n" , trim ($ content ->getRaw ()));
600
+ $ actualMessage = str_replace ("\r\n" , "\n" , trim ($ response ->message ?? '' ));
601
+
602
+ Assert::assertSame (
603
+ $ expectedMessage ,
604
+ $ actualMessage ,
605
+ "Expected cli output to be $ expectedMessage but found $ actualMessage "
606
+ );
607
+ }
608
+
609
+ /**
610
+ * @When the administrator deletes all the stale uploads
611
+ *
612
+ * @return void
613
+ */
614
+ public function theAdministratorDeletesAllTheStaleUploads (): void {
615
+ $ this ->featureContext ->setResponse ($ this ->deleteStaleUploads ());
616
+ }
617
+
618
+ /**
619
+ * @When the administrator deletes all the stale uploads of space :spaceName owned by user :user
620
+ *
621
+ * @param string $spaceName
622
+ * @param string $user
623
+ *
624
+ * @return void
625
+ */
626
+ public function theAdministratorDeletesTheStaleUploadsOfSpaceOwnedByUser (
627
+ string $ spaceName ,
628
+ string $ user
629
+ ): void {
630
+ $ space = $ this ->spacesContext ->getSpaceByName (
631
+ $ user ,
632
+ $ spaceName
633
+ );
634
+ $ spaceOwnerId = $ space ["owner " ]["user " ]["id " ];
635
+ $ this ->featureContext ->setResponse ($ this ->deleteStaleUploads ($ spaceOwnerId ));
636
+ }
637
+
638
+ /**
639
+ * @Then there should be :number stale uploads
640
+ * @Then there should be :number stale uploads of space :spaceName owned by user :user
641
+ *
642
+ * @param int $number
643
+ * @param string|null $spaceName
644
+ * @param string|null $user
645
+ *
646
+ * @return void
647
+ * @throws GuzzleException
648
+ */
649
+ public function thereShouldBeStaleUploadsOfSpaceOwnedByUser (
650
+ int $ number ,
651
+ string $ spaceName ='' ,
652
+ string $ user =''
653
+ ): void {
654
+ $ spaceOwnerId = null ;
655
+ if ($ spaceName !== '' && $ user !== '' ) {
656
+ $ space = $ this ->spacesContext ->getSpaceByName (
657
+ $ user ,
658
+ $ spaceName
659
+ );
660
+ $ spaceOwnerId = $ space ["owner " ]["user " ]["id " ];
661
+ }
662
+
663
+ $ response = $ this ->listStaleUploads ($ spaceOwnerId );
664
+ $ jsonDecodedResponse = $ this ->featureContext ->getJsonDecodedResponseBodyContent ($ response );
665
+ $ this ->featureContext ->theHTTPStatusCodeShouldBe (200 , "" , $ response );
666
+
667
+ $ expectedMessage = "Total stale nodes: $ number " ;
668
+
669
+ Assert::assertStringContainsString (
670
+ $ expectedMessage ,
671
+ $ jsonDecodedResponse ->message ?? '' ,
672
+ "Expected message to contain ' $ expectedMessage', but got: " . ($ jsonDecodedResponse ->message ?? 'null ' )
673
+ );
674
+
675
+ }
499
676
}
0 commit comments