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 ;
@@ -480,6 +481,16 @@ public function cleanUploadsSessions(): void {
480
481
Assert::assertEquals ("200 " , $ response ->getStatusCode (), "Failed to clean upload sessions " );
481
482
}
482
483
484
+ /**
485
+ * @AfterScenario @cli-stale-uploads
486
+ *
487
+ * @return void
488
+ */
489
+ public function cleanUpStaleUploads (): void {
490
+ $ response = $ this ->runDeleteStaleUploadsCommand (false );
491
+ $ this ->featureContext ->theHTTPStatusCodeShouldBe (200 , "Failed to cleanup stale upload " , $ response );
492
+ }
493
+
483
494
/**
484
495
* @When /^the administrator triggers "([^"]*)" email notifications using the CLI$/
485
496
*
@@ -496,4 +507,148 @@ public function theAdministratorTriggersEmailNotificationsUsingTheCLI(string $in
496
507
497
508
$ this ->featureContext ->setResponse (CliHelper::runCommand ($ body ));
498
509
}
510
+
511
+ /**
512
+ * @Given the administrator has created stale upload
513
+ *
514
+ * @return void
515
+ */
516
+ public function theAdministratorHasCreatedStaleUpload (): void {
517
+ $ folderPath = $ this ->featureContext ->getStorageUsersRoot () . "/uploads " ;
518
+ $ infoFiles = glob ($ folderPath . '/*.info ' );
519
+ foreach ($ infoFiles as $ file ) {
520
+ if (!unlink ($ file )) {
521
+ Assert::fail ("Fail to delete info file " );
522
+ }
523
+ }
524
+ }
525
+
526
+ /**
527
+ * Runs the stale uploads CLI command
528
+ *
529
+ * @param bool $dryRun
530
+ * @param string|null $spaceId
531
+ *
532
+ * @return ResponseInterface
533
+ */
534
+ protected function runDeleteStaleUploadsCommand (bool $ dryRun =true , ?string $ spaceId = null ): ResponseInterface {
535
+ $ command = "storage-users uploads delete-stale-nodes " ;
536
+ $ command .= " --dry-run= " . ($ dryRun ? "true " : "false " );
537
+
538
+ if ($ spaceId !== null ) {
539
+ $ command .= " --spaceid= $ spaceId " ;
540
+ }
541
+
542
+ $ body = [
543
+ "command " => $ command
544
+ ];
545
+ return CliHelper::runCommand ($ body );
546
+ }
547
+
548
+ /**
549
+ * @When the administrator lists all the stale uploads
550
+ *
551
+ * @return void
552
+ */
553
+ public function theAdministratorListsAllTheStaleUploads (): void {
554
+ $ this ->featureContext ->setResponse ($ this ->runDeleteStaleUploadsCommand (true ));
555
+ }
556
+
557
+ /**
558
+ * @When the administrator lists all the stale uploads of space :space owned by user :user
559
+ *
560
+ * @param string $spaceName
561
+ * @param string $user
562
+ *
563
+ * @return void
564
+ */
565
+ public function theAdministratorListsTheStaleUploadsOfSpace (string $ spaceName , string $ user ): void {
566
+ $ space = $ this ->spacesContext ->getSpaceByName (
567
+ $ user ,
568
+ $ spaceName
569
+ );
570
+ $ spaceOwnerId = $ space ["owner " ]["user " ]["id " ];
571
+ $ this ->featureContext ->setResponse ($ this ->runDeleteStaleUploadsCommand (true , $ spaceOwnerId ));
572
+ }
573
+
574
+ /**
575
+ * @Then the CLI response should contain the following message:
576
+ *
577
+ * @param PyStringNode $content
578
+ *
579
+ * @return void
580
+ */
581
+ public function theCLIResponseShouldContainTheseMessage (PyStringNode $ content ): void {
582
+ $ response = $ this ->featureContext ->getJsonDecodedResponseBodyContent ();
583
+ $ this ->featureContext ->theHTTPStatusCodeShouldBe (200 );
584
+ $ expectedMessage = str_replace ("\r\n" , "\n" , trim ($ content ->getRaw ()));
585
+ $ actualMessage = str_replace ("\r\n" , "\n" , trim ($ response ->message ?? '' ));
586
+
587
+ Assert::assertSame (
588
+ $ expectedMessage ,
589
+ $ actualMessage ,
590
+ "Expected cli output to be $ expectedMessage but found $ actualMessage "
591
+ );
592
+ }
593
+
594
+ /**
595
+ * @When the administrator deletes all the stale uploads
596
+ *
597
+ * @return void
598
+ */
599
+ public function theAdministratorDeletesAllTheStaleUploads (): void {
600
+ $ this ->featureContext ->setResponse ($ this ->runDeleteStaleUploadsCommand (false ));
601
+ }
602
+
603
+ /**
604
+ * @When the administrator deletes all the stale uploads of space :spaceName owned by user :user
605
+ *
606
+ * @param string $spaceName
607
+ * @param string $user
608
+ *
609
+ * @return void
610
+ */
611
+ public function theAdministratorDeletesTheStaleUploadsOfSpaceOwnedByUser (string $ spaceName , string $ user ): void {
612
+ $ space = $ this ->spacesContext ->getSpaceByName (
613
+ $ user ,
614
+ $ spaceName
615
+ );
616
+ $ spaceOwnerId = $ space ["owner " ]["user " ]["id " ];
617
+ $ this ->featureContext ->setResponse ($ this ->runDeleteStaleUploadsCommand (false , $ spaceOwnerId ));
618
+ }
619
+
620
+ /**
621
+ * @Then there should be :number stale uploads
622
+ * @Then there should be :number stale uploads of space :spaceName owned by user :user
623
+ *
624
+ * @param int $number
625
+ * @param string|null $spaceName
626
+ * @param string|null $user
627
+ *
628
+ * @return void
629
+ * @throws GuzzleException
630
+ */
631
+ public function thereShouldBeStaleUploadsOfSpaceOwnedByUser (int $ number , string $ spaceName ='' , string $ user ='' ): void {
632
+ $ spaceOwnerId = null ;
633
+ if ($ spaceName !== '' && $ user !== '' ) {
634
+ $ space = $ this ->spacesContext ->getSpaceByName (
635
+ $ user ,
636
+ $ spaceName
637
+ );
638
+ $ spaceOwnerId = $ space ["owner " ]["user " ]["id " ];
639
+ }
640
+
641
+ $ response = $ this ->runDeleteStaleUploadsCommand (true , $ spaceOwnerId );
642
+ $ jsonDecodedResponse = $ this ->featureContext ->getJsonDecodedResponseBodyContent ($ response );
643
+ $ this ->featureContext ->theHTTPStatusCodeShouldBe (200 , "" , $ response );
644
+
645
+ $ expectedMessage = "Total stale nodes: $ number " ;
646
+
647
+ Assert::assertStringContainsString (
648
+ $ expectedMessage ,
649
+ $ jsonDecodedResponse ->message ?? '' ,
650
+ "Expected message to contain ' $ expectedMessage', but got: " . ($ jsonDecodedResponse ->message ?? 'null ' )
651
+ );
652
+
653
+ }
499
654
}
0 commit comments