|
| 1 | +<?php |
| 2 | + |
| 3 | +use Statamic\Facades\Taxonomy; |
| 4 | +use Tdwesten\StatamicBuilder\Http\Controllers\TaxonomyBlueprintsController; |
| 5 | +use Tests\TestCase; |
| 6 | + |
| 7 | +pest()->extend(TestCase::class); |
| 8 | + |
| 9 | +const TEST_FILES_DIRECTORY = __DIR__.'/../__fixtures__'; |
| 10 | +const TAX_DIRECTORY = TEST_FILES_DIRECTORY.'/content/taxonomies'; |
| 11 | +const TEST_TAX_BLUEPRINTS_DIRECTORY = TEST_FILES_DIRECTORY.'/resources/blueprints/taxonomies/test_tax'; |
| 12 | + |
| 13 | +test('taxonomy blueprints can be edited', function (): void { |
| 14 | + |
| 15 | + // Prepare fixtures directory |
| 16 | + if (! (is_dir(TEST_FILES_DIRECTORY) && is_dir(TAX_DIRECTORY) && is_dir(TEST_TAX_BLUEPRINTS_DIRECTORY))) { |
| 17 | + $created = mkdir(TAX_DIRECTORY, recursive: true) |
| 18 | + && mkdir(TEST_TAX_BLUEPRINTS_DIRECTORY, recursive: true); |
| 19 | + |
| 20 | + if (! $created) { |
| 21 | + $this->fail('Could not create test files directory (in '.TEST_FILES_DIRECTORY.')'); |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + $test_tax_blueprint = <<<'BLUEPRINT' |
| 26 | +title: Test_Taxonomy_Blueprint |
| 27 | + tabs: |
| 28 | + main: |
| 29 | + display: Main |
| 30 | + sections: |
| 31 | + - |
| 32 | + fields: |
| 33 | + - |
| 34 | + handle: title |
| 35 | + field: |
| 36 | + type: text |
| 37 | + required: true |
| 38 | + validate: |
| 39 | + - required |
| 40 | +BLUEPRINT; |
| 41 | + |
| 42 | + $test_tax = <<<'TAXONOMY' |
| 43 | +title: Test_Tax |
| 44 | +TAXONOMY; |
| 45 | + |
| 46 | + file_put_contents(TAX_DIRECTORY.'/test_tax.yaml', $test_tax); |
| 47 | + file_put_contents(TEST_TAX_BLUEPRINTS_DIRECTORY.'/test_tax.yaml', $test_tax_blueprint); |
| 48 | + |
| 49 | + // Create a new controller and attempt to edit the taxonomy blueprint with it |
| 50 | + (new TaxonomyBlueprintsController)->edit(Taxonomy::findByHandle('test_tax'), 'test_tax'); |
| 51 | +}) |
| 52 | + ->throwsNoExceptions() |
| 53 | + ->after(function () { |
| 54 | + $it = new RecursiveDirectoryIterator(TEST_FILES_DIRECTORY, RecursiveDirectoryIterator::SKIP_DOTS); |
| 55 | + $files = new RecursiveIteratorIterator( |
| 56 | + $it, |
| 57 | + RecursiveIteratorIterator::CHILD_FIRST |
| 58 | + ); |
| 59 | + foreach ($files as $file) { |
| 60 | + if ($file->isDir()) { |
| 61 | + rmdir($file->getPathname()); |
| 62 | + } else { |
| 63 | + unlink($file->getPathname()); |
| 64 | + } |
| 65 | + } |
| 66 | + rmdir(TEST_FILES_DIRECTORY); |
| 67 | + }); |
0 commit comments