Skip to content

Commit

Permalink
[zfcampus#19] Ensure entities are validated as entities, not collections
Browse files Browse the repository at this point in the history
- When a route match is found for the identifier, then we have an
  entity, not a collection.
  • Loading branch information
weierophinney committed Jul 18, 2014
1 parent 3270434 commit 1261a05
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/ContentValidationListener.php
Expand Up @@ -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));
Expand Down
59 changes: 59 additions & 0 deletions test/ContentValidationListenerTest.php
Expand Up @@ -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.