-
-
Notifications
You must be signed in to change notification settings - Fork 185
Show content validation errors when Saving Ajaxy #3769
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
mindaugasjackunaspc
wants to merge
5
commits into
bolt:6.1
Choose a base branch
from
mindaugasjackunaspc:fix/validation-errors-on-ajaxy-save
base: 6.1
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.
+212
−1
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f5ce9b4
Show content validation errors when Saving Ajaxy
mindaugasjackunaspc 1b9a754
Only treat a 422 with an errors array as a validation failure
mindaugasjackunaspc e29c2a1
Add a test for the ajaxy save validation response
mindaugasjackunaspc a24bf6e
Merge branch '6.1' into fix/validation-errors-on-ajaxy-save
mindaugasjackunaspc 5e5140e
Merge branch '6.1' into fix/validation-errors-on-ajaxy-save
mindaugasjackunaspc 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
142 changes: 142 additions & 0 deletions
142
tests/php/Controller/Backend/ContentEditControllerSaveTest.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,142 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Bolt\Tests\Controller\Backend; | ||
|
|
||
| use Bolt\Configuration\Config; | ||
| use Bolt\Controller\Backend\ContentEditController; | ||
| use Bolt\Controller\TwigAwareController; | ||
| use Bolt\Entity\Content; | ||
| use Bolt\Enum\Statuses; | ||
| use Bolt\Event\Listener\ContentFillListener; | ||
| use Bolt\Repository\ContentRepository; | ||
| use Bolt\Repository\MediaRepository; | ||
| use Bolt\Repository\RelationRepository; | ||
| use Bolt\Repository\TaxonomyRepository; | ||
| use Bolt\Utils\ContentHelper; | ||
| use Bolt\Validator\ContentValidatorInterface; | ||
| use Doctrine\ORM\EntityManagerInterface; | ||
| use PHPUnit\Framework\TestCase; | ||
| use ReflectionProperty; | ||
| use Symfony\Component\DependencyInjection\Container; | ||
| use Symfony\Component\HttpFoundation\JsonResponse; | ||
| use Symfony\Component\HttpFoundation\Request; | ||
| use Symfony\Component\HttpFoundation\Response; | ||
| use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | ||
| use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; | ||
| use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; | ||
| use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface; | ||
| use Symfony\Component\Validator\ConstraintViolation; | ||
| use Symfony\Component\Validator\ConstraintViolationList; | ||
| use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; | ||
| use Symfony\Contracts\Translation\TranslatorInterface; | ||
|
|
||
| class ContentEditControllerSaveTest extends TestCase | ||
| { | ||
| /** | ||
| * A save over ajax that the content validator rejects has to answer with the | ||
| * violations as JSON, because `assets/js/app/ajax-save.js` can do nothing with a | ||
| * re-rendered editor. This test fails if that response contract changes. | ||
| */ | ||
| public function testAjaxSaveReturnsTheValidationViolationsAsJson(): void | ||
| { | ||
| $controller = $this->createController(); | ||
|
|
||
| $response = $controller->save( | ||
| $this->ajaxSaveRequest(), | ||
| new Content(), | ||
| $this->contentValidatorRejectingWith( | ||
| new ConstraintViolation('End datetime is required', null, [], null, 'end_datetime', null), | ||
| // Validators built by hand often leave the property path empty. | ||
| new ConstraintViolation('The record is not valid', null, [], null, null, null) | ||
| ) | ||
| ); | ||
|
|
||
| self::assertInstanceOf(JsonResponse::class, $response); | ||
| self::assertSame(Response::HTTP_UNPROCESSABLE_ENTITY, $response->getStatusCode()); | ||
|
|
||
| $payload = json_decode((string) $response->getContent(), true); | ||
|
|
||
| self::assertSame('danger', $payload['status']); | ||
| self::assertSame('warning', $payload['type']); | ||
| self::assertSame('flash_messages.notification', $payload['notification']); | ||
| self::assertSame([ | ||
| ['property' => 'end_datetime', 'message' => 'End datetime is required'], | ||
| ['property' => '', 'message' => 'The record is not valid'], | ||
| ], $payload['errors']); | ||
| } | ||
|
|
||
| private function createController(): ContentEditController | ||
| { | ||
| $controller = new ContentEditController( | ||
| $this->createMock(TaxonomyRepository::class), | ||
| $this->createMock(RelationRepository::class), | ||
| $this->createMock(ContentRepository::class), | ||
| $this->createMock(MediaRepository::class), | ||
| $this->createMock(EntityManagerInterface::class), | ||
| $this->createMock(UrlGeneratorInterface::class), | ||
| $this->createMock(ContentFillListener::class), | ||
| $this->createMock(EventDispatcherInterface::class), | ||
| 'en', | ||
| $this->translatorReturningKeys(), | ||
| $this->createMock(ContentHelper::class) | ||
| ); | ||
|
|
||
| $csrfTokenManager = $this->createMock(CsrfTokenManagerInterface::class); | ||
| $csrfTokenManager->method('isTokenValid')->willReturn(true); | ||
| $controller->setCsrfTokenManager($csrfTokenManager); | ||
|
|
||
| $authorizationChecker = $this->createMock(AuthorizationCheckerInterface::class); | ||
| $authorizationChecker->method('isGranted')->willReturn(true); | ||
|
|
||
| $container = new Container(); | ||
| $container->set('security.authorization_checker', $authorizationChecker); | ||
| $container->set('security.token_storage', $this->createMock(TokenStorageInterface::class)); | ||
| $controller->setContainer($container); | ||
|
|
||
| // Of the collaborators that `setAutowire()` injects, this code path only reads the | ||
| // config, so that is the only one set here. | ||
| (new ReflectionProperty(TwigAwareController::class, 'config'))->setValue($controller, $this->configWithValidatorEnabled()); | ||
|
|
||
| return $controller; | ||
| } | ||
|
|
||
| private function configWithValidatorEnabled(): Config | ||
| { | ||
| $config = $this->createMock(Config::class); | ||
| $config->method('get')->willReturnCallback( | ||
| static fn (string $path, $default = null) => $path === 'general/validator_options/enable' ? true : $default | ||
| ); | ||
|
|
||
| return $config; | ||
| } | ||
|
|
||
| private function translatorReturningKeys(): TranslatorInterface | ||
| { | ||
| $translator = $this->createMock(TranslatorInterface::class); | ||
| $translator->method('trans')->willReturnArgument(0); | ||
|
|
||
| return $translator; | ||
| } | ||
|
|
||
| private function contentValidatorRejectingWith(ConstraintViolation ...$violations): ContentValidatorInterface | ||
| { | ||
| $contentValidator = $this->createMock(ContentValidatorInterface::class); | ||
| $contentValidator->method('validate')->willReturn(new ConstraintViolationList($violations)); | ||
|
|
||
| return $contentValidator; | ||
| } | ||
|
|
||
| private function ajaxSaveRequest(): Request | ||
| { | ||
| $request = Request::create('/bolt/edit/1', Request::METHOD_POST, [ | ||
| '_csrf_token' => 'valid', | ||
| '_edit_locale' => 'en', | ||
| 'status' => Statuses::DRAFT, | ||
| ]); | ||
| $request->headers->set('X-Requested-With', 'XMLHttpRequest'); | ||
|
|
||
| return $request; | ||
| } | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.