-
Notifications
You must be signed in to change notification settings - Fork 7
fix(#3494240): skip the redirect when a file leaves the staging area #24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Decipher
wants to merge
2
commits into
8.x-1.x
Choose a base branch
from
feature/3494240-staging-redirect
base: 8.x-1.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,184 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Drupal\Tests\filefield_paths\Kernel; | ||
|
|
||
| use Drupal\Core\Field\FieldStorageDefinitionInterface; | ||
| use Drupal\KernelTests\KernelTestBase; | ||
| use Drupal\entity_test\Entity\EntityTest; | ||
| use Drupal\field\Entity\FieldConfig; | ||
| use Drupal\field\Entity\FieldStorageConfig; | ||
| use Drupal\file\Entity\File; | ||
| use PHPUnit\Framework\Attributes\Group; | ||
| use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses; | ||
|
|
||
| /** | ||
| * Tests which moves earn a redirect. | ||
| * | ||
| * @group filefield_paths | ||
| * @covers \Drupal\filefield_paths\Hook\FileFieldPathsProcessFileLegacy | ||
| */ | ||
| #[Group('filefield_paths')] | ||
| #[RunTestsInSeparateProcesses] | ||
| class StagingRedirectTest extends KernelTestBase { | ||
|
|
||
| /** | ||
| * Modules to enable. | ||
| * | ||
| * @var array<string> | ||
| */ | ||
| protected static $modules = [ | ||
| 'system', | ||
| 'user', | ||
| 'field', | ||
| 'file', | ||
| 'path_alias', | ||
| 'redirect', | ||
| 'link', | ||
| 'entity_test', | ||
| 'filefield_paths', | ||
| ]; | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| protected function setUp(): void { | ||
| parent::setUp(); | ||
| $this->installEntitySchema('user'); | ||
| $this->installEntitySchema('file'); | ||
| $this->installEntitySchema('entity_test'); | ||
| $this->installEntitySchema('redirect'); | ||
| $this->installEntitySchema('path_alias'); | ||
| $this->installSchema('file', ['file_usage']); | ||
| $this->installConfig(['filefield_paths']); | ||
| $this->config('redirect.settings')->set('default_status_code', 301)->save(); | ||
|
|
||
| FieldStorageConfig::create([ | ||
| 'field_name' => 'field_file', | ||
| 'entity_type' => 'entity_test', | ||
| 'type' => 'file', | ||
| 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED, | ||
| 'settings' => ['uri_scheme' => 'public'], | ||
| ])->save(); | ||
|
|
||
| $options = ['slashes' => FALSE, 'pathauto' => FALSE, 'transliterate' => FALSE]; | ||
| $field = FieldConfig::create([ | ||
| 'entity_type' => 'entity_test', | ||
| 'field_name' => 'field_file', | ||
| 'bundle' => 'entity_test', | ||
| ]); | ||
| $field->setThirdPartySetting('filefield_paths', 'enabled', TRUE); | ||
| $field->setThirdPartySetting('filefield_paths', 'file_path', [ | ||
| 'value' => 'sorted', | ||
| 'options' => $options, | ||
| ]); | ||
| $field->setThirdPartySetting('filefield_paths', 'file_name', [ | ||
| 'value' => '', | ||
| 'options' => $options, | ||
| ]); | ||
| $field->setThirdPartySetting('filefield_paths', 'active_updating', TRUE); | ||
| $field->setThirdPartySetting('filefield_paths', 'redirect', TRUE); | ||
| $field->setThirdPartySetting('filefield_paths', 'retroactive_update', FALSE); | ||
| $field->save(); | ||
| } | ||
|
|
||
| /** | ||
| * Attaches a file at the given URI to a new entity and saves it. | ||
| * | ||
| * @param string $uri | ||
| * Where the file sits before the entity is saved. | ||
| */ | ||
| private function attachFileAt(string $uri): void { | ||
| $file_system = $this->container->get('file_system'); | ||
| $directory = $file_system->dirname($uri); | ||
| $file_system->prepareDirectory($directory, $file_system::CREATE_DIRECTORY); | ||
| file_put_contents($uri, 'contents'); | ||
| $file = File::create(['uri' => $uri]); | ||
| $file->setPermanent(); | ||
| $file->save(); | ||
|
|
||
| EntityTest::create([ | ||
| 'name' => 'test', | ||
| 'field_file' => [['target_id' => $file->id()]], | ||
| ])->save(); | ||
| } | ||
|
|
||
| /** | ||
| * Counts the redirects that exist. | ||
| */ | ||
| private function redirectCount(): int { | ||
| return count($this->container->get('entity_type.manager') | ||
| ->getStorage('redirect') | ||
| ->loadMultiple()); | ||
| } | ||
|
|
||
| /** | ||
| * A file leaving the staging area earns no redirect. | ||
| * | ||
| * The staging path only ever existed between the upload and the save, so | ||
| * nothing can be linking to it. | ||
| * | ||
| * @see https://www.drupal.org/i/3494240 | ||
| */ | ||
| public function testNoRedirectWhenTheFileComesFromStaging(): void { | ||
| $this->attachFileAt('public://filefield_paths/example.txt'); | ||
|
|
||
| $this->assertFileExists('public://sorted/example.txt'); | ||
| $this->assertSame(0, $this->redirectCount(), 'A staged upload should not leave a redirect behind.'); | ||
| } | ||
|
|
||
| /** | ||
| * A file moved from a real location still earns a redirect. | ||
| * | ||
| * This is the control. The fix must not stop redirects for files that were | ||
| * genuinely reachable at their old path. | ||
| * | ||
| * @see https://www.drupal.org/i/3494240 | ||
| */ | ||
| public function testRedirectWhenTheFileWasAlreadyPublished(): void { | ||
| $this->attachFileAt('public://published/example.txt'); | ||
|
|
||
| $this->assertFileExists('public://sorted/example.txt'); | ||
| $this->assertSame(1, $this->redirectCount(), 'A move from a real location should still leave a redirect.'); | ||
| } | ||
|
|
||
| /** | ||
| * A bare scheme root is not a staging location. | ||
| * | ||
| * The settings form accepts "public://" on its own. Treating that as a | ||
| * staging prefix would match every file on the scheme and quietly stop | ||
| * redirects being created at all. | ||
| * | ||
| * @see https://www.drupal.org/i/3494240 | ||
| */ | ||
| public function testBareSchemeRootIsNotTreatedAsStaging(): void { | ||
| $this->config('filefield_paths.settings') | ||
| ->set('temp_location', 'public://') | ||
| ->save(); | ||
|
|
||
| $this->attachFileAt('public://published/example.txt'); | ||
|
|
||
| $this->assertFileExists('public://sorted/example.txt'); | ||
| $this->assertSame(1, $this->redirectCount(), 'A bare scheme root must not suppress redirects.'); | ||
| } | ||
|
|
||
| /** | ||
| * The field's own staging location wins over the global one. | ||
| * | ||
| * @see https://www.drupal.org/i/3494240 | ||
| */ | ||
| public function testFieldStagingLocationTakesPrecedence(): void { | ||
| $field = FieldConfig::loadByName('entity_test', 'entity_test', 'field_file'); | ||
| \assert($field instanceof FieldConfig); | ||
| $field->setThirdPartySetting('filefield_paths', 'temp_location', 'public://custom_stage'); | ||
| $field->save(); | ||
| $this->container->get('entity_field.manager')->clearCachedFieldDefinitions(); | ||
|
|
||
| $this->attachFileAt('public://custom_stage/example.txt'); | ||
|
|
||
| $this->assertFileExists('public://sorted/example.txt'); | ||
| $this->assertSame(0, $this->redirectCount(), 'The field level staging location should suppress the redirect.'); | ||
| } | ||
|
|
||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: Decipher/filefield_paths
Length of output: 24744
🏁 Script executed:
Repository: Decipher/filefield_paths
Length of output: 31682
Preserve redirects when active updating is disabled.
When a new entity references
public://published/...andactive_updatingis disabled, the hook still moves the file. The added prerequisite then skips the redirect. Remove it and letisStagedUpload()suppress redirects only for staged files. Add a kernel test for this configuration.🤖 Prompt for AI Agents