Permalink
Browse files

[#19] Ensure entities are validated as entities, not collections

- When a route match is found for the identifier, then we have an
  entity, not a collection.
  • Loading branch information...
1 parent 3270434 commit 1261a05ffc16996dd3d37d7e49d6a649cd652f77 @weierophinney committed Jul 18, 2014
Showing with 60 additions and 1 deletion.
  1. +1 −1 src/ContentValidationListener.php
  2. +59 −0 test/ContentValidationListenerTest.php
View
2 src/ContentValidationListener.php
@@ -303,7 +303,7 @@ protected function isCollection($serviceName, $data, RouteMatch $matches, HttpRe
$identifierName = $this->restControllers[$serviceName];
if ($matches->getParam($identifierName)) {
- return true;
+ return false;
}
return (null === $request->getQuery($identifierName, null));
View
59 test/ContentValidationListenerTest.php
@@ -1042,4 +1042,63 @@ public function testIndicatesInvalidPostedEntityWhenCollectionIsPossibleForServi
$this->assertInstanceOf('ZF\ApiProblem\ApiProblemResponse', $response);
$this->assertEquals(422, $response->getApiProblem()->status);
}
+
+ /**
+ * @dataProvider listMethods
+ * @group 19
+ */
+ public function testDoesNotAttemptToValidateAnEntityAsACollection($method)
+ {
+ $services = new ServiceManager();
+ $factory = new InputFilterFactory();
+ $services->setService('FooValidator', $factory->createInputFilter(array(
+ 'foo' => array(
+ 'name' => 'foo',
+ 'validators' => array(
+ array('name' => 'Digits'),
+ ),
+ ),
+ 'bar' => array(
+ 'name' => 'bar',
+ 'validators' => array(
+ array(
+ 'name' => 'Regex',
+ 'options' => array('pattern' => '/^[a-z]+/i'),
+ ),
+ ),
+ ),
+ )));
+
+ // Create ContentValidationListener with rest controllers populated
+ $listener = new ContentValidationListener(array(
+ 'Foo' => array('input_filter' => 'FooValidator'),
+ ), $services, array(
+ 'Foo' => 'foo_id',
+ ));
+
+ $request = new HttpRequest();
+ $request->setMethod($method);
+
+ $matches = new RouteMatch(array(
+ 'controller' => 'Foo',
+ 'foo_id' => uniqid(),
+ ));
+
+ $dataParams = new ParameterDataContainer();
+
+ $params = array(
+ 'foo' => 123,
+ 'bar' => 'abc',
+ );
+
+ $dataParams->setBodyParams($params);
+
+ $event = new MvcEvent();
+ $event->setRequest($request);
+ $event->setRouteMatch($matches);
+ $event->setParam('ZFContentNegotiationParameterData', $dataParams);
+
+ $this->assertNull($listener->onRoute($event));
+ $this->assertNull($event->getResponse());
+ }
}

0 comments on commit 1261a05

Please sign in to comment.