|
1 | 1 | <?php |
2 | 2 |
|
| 3 | +use Statamic\Facades\Taxonomy; |
3 | 4 | use Tdwesten\StatamicBuilder\Exceptions\BlueprintRenderException; |
4 | 5 | use Tdwesten\StatamicBuilder\FieldTypes\Tab; |
5 | 6 | use Tdwesten\StatamicBuilder\FieldTypes\Text; |
| 7 | +use Tdwesten\StatamicBuilder\Http\Controllers\TaxonomyBlueprintsController; |
6 | 8 | use Tests\Helpers\TestBlueprint; |
| 9 | +use Tests\TestCase; |
7 | 10 |
|
8 | 11 | test('Has a title', function (): void { |
9 | 12 | $blueprint = TestBlueprint::make('test_blueprint'); |
|
81 | 84 |
|
82 | 85 | expect($blueprint->getHandle())->toBe('test_blueprint'); |
83 | 86 | }); |
| 87 | + |
| 88 | +pest()->extend(TestCase::class); |
| 89 | + |
| 90 | +const TEST_FILES_DIRECTORY = __DIR__.'/../__fixtures__'; |
| 91 | +const TAX_DIRECTORY = TEST_FILES_DIRECTORY.'/content/taxonomies'; |
| 92 | +const TEST_TAX_BLUEPRINTS_DIRECTORY = TEST_FILES_DIRECTORY.'/resources/blueprints/taxonomies/test_tax'; |
| 93 | + |
| 94 | +test('taxonomy blueprints can be edited', function (): void { |
| 95 | + |
| 96 | + // Prepare fixtures directory |
| 97 | + if (! (is_dir(TEST_FILES_DIRECTORY) && is_dir(TAX_DIRECTORY) && is_dir(TEST_TAX_BLUEPRINTS_DIRECTORY))) { |
| 98 | + $created = mkdir(TAX_DIRECTORY, recursive: true) |
| 99 | + && mkdir(TEST_TAX_BLUEPRINTS_DIRECTORY, recursive: true); |
| 100 | + |
| 101 | + if (! $created) { |
| 102 | + $this->fail('Could not create test files directory (in '.TEST_FILES_DIRECTORY.')'); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * The issue we're testing for regards normal Statamic Taxonomy Blueprints |
| 108 | + * and their interaction with Statamic-Builder's controller. |
| 109 | + * |
| 110 | + * So, we'll need to make files for Statamic to load in during the test. |
| 111 | + * These will be removed in the ->after callback. |
| 112 | + */ |
| 113 | + $test_tax_blueprint = <<<'BLUEPRINT' |
| 114 | +title: Test_Taxonomy_Blueprint |
| 115 | + tabs: |
| 116 | + main: |
| 117 | + display: Main |
| 118 | + sections: |
| 119 | + - |
| 120 | + fields: |
| 121 | + - |
| 122 | + handle: title |
| 123 | + field: |
| 124 | + type: text |
| 125 | + required: true |
| 126 | + validate: |
| 127 | + - required |
| 128 | +BLUEPRINT; |
| 129 | + |
| 130 | + $test_tax = <<<'TAXONOMY' |
| 131 | +title: Test_Tax |
| 132 | +TAXONOMY; |
| 133 | + |
| 134 | + file_put_contents(TAX_DIRECTORY.'/test_tax.yaml', $test_tax); |
| 135 | + file_put_contents(TEST_TAX_BLUEPRINTS_DIRECTORY.'/test_tax.yaml', $test_tax_blueprint); |
| 136 | + |
| 137 | + // Create a new controller and attempt to edit the taxonomy blueprint with it |
| 138 | + (new TaxonomyBlueprintsController)->edit(Taxonomy::findByHandle('test_tax'), 'test_tax'); |
| 139 | +}) |
| 140 | + ->throwsNoExceptions() |
| 141 | + ->after(function () { |
| 142 | + function removeDir(string $dir): void |
| 143 | + { |
| 144 | + $it = new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS); |
| 145 | + $files = new RecursiveIteratorIterator( |
| 146 | + $it, |
| 147 | + RecursiveIteratorIterator::CHILD_FIRST |
| 148 | + ); |
| 149 | + foreach ($files as $file) { |
| 150 | + if ($file->isDir()) { |
| 151 | + rmdir($file->getPathname()); |
| 152 | + } else { |
| 153 | + unlink($file->getPathname()); |
| 154 | + } |
| 155 | + } |
| 156 | + rmdir($dir); |
| 157 | + } |
| 158 | + |
| 159 | + removeDir(TEST_FILES_DIRECTORY); |
| 160 | + }); |
0 commit comments