forked from COmanage/registry
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
411 additions
and
175 deletions.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace App\Test\TestCase\Command; | ||
|
|
||
| use Cake\Console\TestSuite\ConsoleIntegrationTestTrait; | ||
| use Cake\Datasource\ConnectionManager; | ||
| use Cake\TestSuite\TestCase; | ||
| use PHPUnit\Framework\Attributes\CoversClass; | ||
| use PHPUnit\Framework\Attributes\Group; | ||
|
|
||
| #[Group('registry-setup')] | ||
| #[CoversClass('SetupCommand')] | ||
| final class RegistrySetupTest extends TestCase | ||
| { | ||
| use ConsoleIntegrationTestTrait; | ||
|
|
||
| public function testRegistrySetupRunsSuccessfully(): void | ||
| { | ||
| $appDir = dirname(__DIR__, 3); | ||
|
|
||
| $adminGiven = getenv('COMANAGE_REGISTRY_ADMIN_GIVEN_NAME') ?: 'Admin'; | ||
| $adminFamily = getenv('COMANAGE_REGISTRY_ADMIN_FAMILY_NAME') ?: 'User'; | ||
| $adminUser = getenv('COMANAGE_REGISTRY_ADMIN_USERNAME') ?: 'admin'; | ||
|
|
||
| // The test bootstrap already sets the Security salt and prepares the test DB schema. | ||
| // Here we only validate that the setup command can run successfully. | ||
| $this->exec(sprintf( | ||
| 'setup --admin-given-name %s --admin-family-name %s --admin-username %s', | ||
| $adminGiven, | ||
| $adminFamily, | ||
| $adminUser, | ||
| )); | ||
| $this->assertExitCode(0, 'bin/cake setup should exit successfully'); | ||
|
|
||
| $connection = ConnectionManager::get('default'); | ||
|
|
||
| // Verify we can talk to the DB and it has tables. | ||
| $tables = $connection->getSchemaCollection()->listTables(); | ||
| $this->assertNotEmpty($tables, 'Expected at least one table in the test database'); | ||
|
|
||
| // Assert schema.json tables exist in the DB. | ||
| $schemaFile = $appDir . '/config/schema/schema.json'; | ||
| $this->assertFileExists($schemaFile, 'Expected schema.json to exist at config/schema/schema.json'); | ||
|
|
||
| $json = file_get_contents($schemaFile); | ||
| $this->assertNotFalse($json, 'Failed to read schema file: ' . $schemaFile); | ||
|
|
||
| $data = json_decode($json, true); | ||
| $this->assertIsArray($data, 'schema.json did not decode into an array'); | ||
| $this->assertArrayHasKey('tables', $data, 'schema.json is missing the "tables" key'); | ||
| $this->assertIsArray($data['tables'], 'schema.json "tables" should be an object/map'); | ||
|
|
||
| $expectedTables = array_keys($data['tables']); | ||
| $missing = array_values(array_diff($expectedTables, $tables)); | ||
|
|
||
| $this->assertSame( | ||
| [], | ||
| $missing, | ||
| "Some tables from config/schema/schema.json are missing in the database.\n" | ||
| . 'Missing: ' . implode(', ', $missing) . "\n" | ||
| . 'DB_ENGINE=' . (getenv('DB_ENGINE') ?: '(not set)') | ||
| ); | ||
| } | ||
| } |
98 changes: 98 additions & 0 deletions
98
app/tests/TestCase/Controller/MostlyStaticPagesControllerTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace App\Test\TestCase\Controller; | ||
|
|
||
| use App\Lib\Enum\PageContextEnum; | ||
| use App\Lib\Enum\SuspendableStatusEnum; | ||
| use Cake\ORM\TableRegistry; | ||
| use Cake\TestSuite\IntegrationTestTrait; | ||
| use Cake\TestSuite\TestCase; | ||
| use PHPUnit\Framework\Attributes\CoversClass; | ||
| use PHPUnit\Framework\Attributes\DataProvider; | ||
|
|
||
| #[CoversClass('MostlyStaticPagesController')] | ||
| final class MostlyStaticPagesControllerTest extends TestCase | ||
| { | ||
| use IntegrationTestTrait; | ||
|
|
||
| public static function defaultPagesProvider(): array | ||
| { | ||
| return [ | ||
| ['duplicate-landing', PageContextEnum::EnrollmentHandoff], | ||
| ['default-handoff', PageContextEnum::EnrollmentHandoff], | ||
| ['error-landing', PageContextEnum::ErrorLanding], | ||
| ['mfa-required', PageContextEnum::ErrorLanding], | ||
| ['petition-complete', PageContextEnum::EnrollmentHandoff], | ||
| ]; | ||
| } | ||
|
|
||
| public static function defaultPageSlugsProvider(): array | ||
| { | ||
| return [ | ||
| ['duplicate-landing'], | ||
| ['default-handoff'], | ||
| ['error-landing'], | ||
| ['mfa-required'], | ||
| ['petition-complete'], | ||
| ]; | ||
| } | ||
|
|
||
| private function getComanageCoId(): int | ||
| { | ||
| $Cos = TableRegistry::getTableLocator()->get('Cos'); | ||
| $co = $Cos->find('COmanageCO')->firstOrFail(); | ||
|
|
||
| return (int)$co->id; | ||
| } | ||
|
|
||
| private function assertDefaultPageRendersOverHttp(string $slug): void | ||
| { | ||
| $coId = $this->getComanageCoId(); | ||
|
|
||
| $MostlyStaticPages = TableRegistry::getTableLocator()->get('MostlyStaticPages'); | ||
| $page = $MostlyStaticPages->find() | ||
| ->where(['co_id' => $coId, 'name' => $slug]) | ||
| ->firstOrFail(); | ||
|
|
||
| // Correct route is /{coid}/{name} | ||
| $this->get("/{$coId}/{$slug}"); | ||
|
|
||
| $this->assertResponseOk(); | ||
| $this->assertResponseContains((string)$page->title); | ||
| } | ||
|
|
||
| #[DataProvider('defaultPageSlugsProvider')] | ||
| public function testDefaultPagesRenderOverHttp(string $slug): void | ||
| { | ||
| $this->assertDefaultPageRendersOverHttp($slug); | ||
| } | ||
|
|
||
| private function assertDefaultPageExists(string $name, string $expectedContext): void | ||
| { | ||
| $coId = $this->getComanageCoId(); | ||
|
|
||
| $MostlyStaticPages = TableRegistry::getTableLocator()->get('MostlyStaticPages'); | ||
|
|
||
| $page = $MostlyStaticPages->find() | ||
| ->where([ | ||
| 'co_id' => $coId, | ||
| 'name' => $name, | ||
| 'status' => SuspendableStatusEnum::Active, | ||
| 'context' => $expectedContext, | ||
| ]) | ||
| ->first(); | ||
|
|
||
| $this->assertNotEmpty( | ||
| $page, | ||
| sprintf('Expected Mostly Static Page "%s" to exist for CO %d after setup', $name, $coId) | ||
| ); | ||
| } | ||
|
|
||
| #[DataProvider('defaultPagesProvider')] | ||
| public function testDefaultPagesExist(string $name, string $expectedContext): void | ||
| { | ||
| $this->assertDefaultPageExists($name, $expectedContext); | ||
| } | ||
| } |
Oops, something went wrong.