From d321a595c638880d989992674329e384cfda3069 Mon Sep 17 00:00:00 2001 From: Dann Stockton Date: Fri, 15 Jul 2016 14:00:45 -0600 Subject: [PATCH 1/2] Fix bug where getParam could return falsey values - $matches->getParam() can return "0", which evaluates to false - This fixes that bug by explicity checking against null - Add test cases --- src/ContentValidationListener.php | 2 +- test/ContentValidationListenerTest.php | 100 +++++++++++++++++++++++++ 2 files changed, 101 insertions(+), 1 deletion(-) diff --git a/src/ContentValidationListener.php b/src/ContentValidationListener.php index b974c48..ce22fce 100644 --- a/src/ContentValidationListener.php +++ b/src/ContentValidationListener.php @@ -425,7 +425,7 @@ protected function isCollection($serviceName, $data, $matches, HttpRequest $requ } $identifierName = $this->restControllers[$serviceName]; - if ($matches->getParam($identifierName)) { + if ($matches->getParam($identifierName) !== null) { return false; } diff --git a/test/ContentValidationListenerTest.php b/test/ContentValidationListenerTest.php index 134e3c2..934b0fc 100644 --- a/test/ContentValidationListenerTest.php +++ b/test/ContentValidationListenerTest.php @@ -10,6 +10,7 @@ use Zend\EventManager\EventManager; use Zend\EventManager\EventManagerInterface; use Zend\Http\Request as HttpRequest; +use Zend\InputFilter\CollectionInputFilter; use Zend\InputFilter\Factory as InputFilterFactory; use Zend\InputFilter\InputFilter; use Zend\InputFilter\InputFilterInterface; @@ -500,6 +501,105 @@ public function testAllowsValidationOfPartialSetsForPatchRequests() $this->assertNull($listener->onRoute($event)); } + /** + * @dataProvider listMethods + */ + public function testPatchWithZeroRouteIdDoesNotEmitANoticeAndDoesNotHaveCollectionInputFilterWhenRequestHasABody( + $verb + ) { + $services = new ServiceManager(); + $factory = new InputFilterFactory(); + $services->setService( + 'FooValidator', + $factory->createInputFilter( + [ + 'foo' => [ + 'name' => 'foo', + 'required' => false, + ], + ] + ) + ); + $listener = new ContentValidationListener( + [ + 'Foo' => ['input_filter' => 'FooValidator'], + ], + $services, + [ + 'Foo' => 'foo_id', + ] + ); + + $request = new HttpRequest(); + $request->setMethod($verb); + + $matches = $this->createRouteMatch(['controller' => 'Foo']); + $matches->setParam('foo_id', "0"); + + $dataParams = new ParameterDataContainer(); + $dataParams->setBodyParams( + [ + 'foo' => 123, + ] + ); + + $event = new MvcEvent(); + $event->setRequest($request); + $event->setRouteMatch($matches); + $event->setParam('ZFContentNegotiationParameterData', $dataParams); + + $this->assertNull($listener->onRoute($event)); + $inputFilter = $event->getParam('ZF\ContentValidation\InputFilter'); + // with notices on, this won't get hit with broken code + $this->assertNotInstanceOf(CollectionInputFilter::class, $inputFilter); + } + + /** + * @dataProvider listMethods + */ + public function testPatchWithZeroRouteIdWithNoRequestBodyDoesNotHaveCollectionInputFilter($verb) + { + $services = new ServiceManager(); + $factory = new InputFilterFactory(); + $services->setService( + 'FooValidator', + $factory->createInputFilter( + [ + 'foo' => [ + 'name' => 'foo', + 'required' => false, + ], + ] + ) + ); + $listener = new ContentValidationListener( + [ + 'Foo' => ['input_filter' => 'FooValidator'], + ], + $services, + [ + 'Foo' => 'foo_id', + ] + ); + + $request = new HttpRequest(); + $request->setMethod($verb); + + $matches = $this->createRouteMatch(['controller' => 'Foo']); + $matches->setParam('foo_id', "0"); + + $dataParams = new ParameterDataContainer(); + $dataParams->setBodyParams([]); + + $event = new MvcEvent(); + $event->setRequest($request); + $event->setRouteMatch($matches); + $event->setParam('ZFContentNegotiationParameterData', $dataParams); + $this->assertNull($listener->onRoute($event)); + $inputFilter = $event->getParam('ZF\ContentValidation\InputFilter'); + $this->assertNotInstanceOf(CollectionInputFilter::class, $inputFilter); + } + public function testFailsValidationOfPartialSetsForPatchRequestsThatIncludeUnknownInputs() { $services = new ServiceManager(); From 50d08414de6e72c302e42242ec2ddf047c16f8d5 Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Tue, 26 Jul 2016 16:15:21 -0500 Subject: [PATCH 2/2] Added CHANGELOG for #78 --- CHANGELOG.md | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6366496..96d1502 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ All notable changes to this project will be documented in this file, in reverse chronological order by release. -## 1.3.3 - TBD +## 1.3.3 - 2016-07-26 ### Added @@ -18,7 +18,12 @@ All notable changes to this project will be documented in this file, in reverse ### Fixed -- Nothing. +- [#78](https://github.com/zfcampus/zf-content-validation/pull/78) updates + `ContentValidationListener::isCollection()` to check strictly for absence of + an identifier when determining if a collection was requested. Previously, a + `0` identifier would be incorrectly flagged as a request for a collection, + which would pull the collection input filter instead of the entity input + filter. ## 1.3.2 - 2016-07-21