|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Unish; |
| 6 | + |
| 7 | +use Drupal\node\Entity\Node; |
| 8 | +use Drupal\workspaces\WorkspaceManagerInterface; |
| 9 | +use Drush\Commands\core\WorkspacesCommand; |
| 10 | +use Drush\Commands\pm\PmInstallCommand; |
| 11 | + |
| 12 | +/** |
| 13 | + * Tests Workspaces commands |
| 14 | + * |
| 15 | + * @group commands |
| 16 | + */ |
| 17 | +class WorkspacesTest extends UnishIntegrationTestCase |
| 18 | +{ |
| 19 | + private WorkspaceManagerInterface $workspaceManager; |
| 20 | + |
| 21 | + protected function setUp(): void |
| 22 | + { |
| 23 | + parent::setUp(); |
| 24 | + $this->drush(PmInstallCommand::NAME, ['workspaces', 'node']); |
| 25 | + $this->workspaceManager = \Drupal::service('workspaces.manager'); |
| 26 | + |
| 27 | + // Create article content type if it doesn't exist |
| 28 | + $node_types = \Drupal::entityTypeManager()->getStorage('node_type')->loadMultiple(); |
| 29 | + if (!isset($node_types['article'])) { |
| 30 | + $article_type = \Drupal\node\Entity\NodeType::create([ |
| 31 | + 'type' => 'article', |
| 32 | + 'name' => 'Article', |
| 33 | + ]); |
| 34 | + $article_type->save(); |
| 35 | + } |
| 36 | + |
| 37 | + // Create a test workspace if it doesn't exist |
| 38 | + $workspace_storage = \Drupal::entityTypeManager()->getStorage('workspace'); |
| 39 | + $existing_workspace = $workspace_storage->load('stage'); |
| 40 | + if (!$existing_workspace) { |
| 41 | + $workspace = \Drupal\workspaces\Entity\Workspace::create([ |
| 42 | + 'id' => 'stage', |
| 43 | + 'label' => 'Stage', |
| 44 | + ]); |
| 45 | + $workspace->save(); |
| 46 | + } |
| 47 | + |
| 48 | + // Set the stage workspace as active and create content in it |
| 49 | + $stage_workspace = $workspace_storage->load('stage'); |
| 50 | + $this->workspaceManager->setActiveWorkspace($stage_workspace); |
| 51 | + |
| 52 | + // Create a sample node in the workspace context |
| 53 | + $node = Node::create([ |
| 54 | + 'type' => 'article', |
| 55 | + 'title' => 'Test Node in Workspace', |
| 56 | + 'body' => 'This is a test node created in the stage workspace.', |
| 57 | + 'status' => 1, |
| 58 | + ]); |
| 59 | + $node->save(); |
| 60 | + } |
| 61 | + |
| 62 | + public function testWorkspaces(): void |
| 63 | + { |
| 64 | + // Test that workspace has content to publish |
| 65 | + $workspace_id = 'stage'; |
| 66 | + |
| 67 | + // Publish the workspace |
| 68 | + $this->drush(WorkspacesCommand::NAME, [$workspace_id]); |
| 69 | + $this->assertStringContainsString('Workspace Stage published', $this->getErrorOutput()); |
| 70 | + |
| 71 | + // Verify no more changes exist after publishing |
| 72 | + $this->drush(WorkspacesCommand::NAME, [$workspace_id]); |
| 73 | + $this->assertStringContainsString('There are no changes that can be published', $this->getErrorOutput()); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * {@inheritdoc} |
| 78 | + */ |
| 79 | + protected function tearDown(): void |
| 80 | + { |
| 81 | + // Cleanup the test workspace (this should also handle content deletion) |
| 82 | + $this->drush('entity:delete', ['workspace', 'stage'], ['yes' => true]); |
| 83 | + $this->drush('entity:delete', ['node'], ['yes' => true, 'bundle' => 'article']); |
| 84 | + $this->drush('pm:uninstall', ['workspaces', 'node'], ['yes' => true]); |
| 85 | + parent::tearDown(); |
| 86 | + } |
| 87 | +} |
0 commit comments