forked from zfcampus/zf-apigility-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[zfcampus#249] Proper fix for content-negotiation
POST is the only one that should allow `content_name` in the payload; otherwise, we're using PATCH or DELETE with the `content_name` in the URI. As such, this means two input filters. This patch creates CreateContentNegotiationInputFilter, which extends ContentNegotiationInputFilterTest and ensures that `content_name` is present.
- Loading branch information
1 parent
170f010
commit 674f66c
Showing
5 changed files
with
168 additions
and
16 deletions.
There are no files selected for viewing
This file contains 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 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 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,52 @@ | ||
<?php | ||
/** | ||
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause | ||
* @copyright Copyright (c) 2014 Zend Technologies USA Inc. (http://www.zend.com) | ||
*/ | ||
|
||
namespace ZF\Apigility\Admin\InputFilter; | ||
|
||
class CreateContentNegotiationInputFilter extends ContentNegotiationInputFilter | ||
{ | ||
/** | ||
* Is the data set valid? | ||
* | ||
* @return bool | ||
*/ | ||
public function isValid() | ||
{ | ||
$this->messages = array(); | ||
$isValid = true; | ||
|
||
if (! array_key_exists('content_name', $this->data)) { | ||
$this->messages['content_name'][] = 'No content_name was provided; must be present for new negotiators.'; | ||
$isValid = false; | ||
} | ||
|
||
if (array_key_exists('content_name', $this->data) && ! is_string($this->data['content_name'])) { | ||
$this->messages['content_name'][] = 'Content name provided is invalid; must be a string'; | ||
$isValid = false; | ||
} | ||
|
||
if (! $isValid) { | ||
return false; | ||
} | ||
|
||
$contentName = $this->data['content_name']; | ||
unset($this->data['content_name']); | ||
|
||
$isValid = parent::isValid(); | ||
|
||
$this->data['content_name'] = $contentName; | ||
|
||
return $isValid; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getMessages() | ||
{ | ||
return $this->messages; | ||
} | ||
} |
This file contains 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
114 changes: 114 additions & 0 deletions
114
test/InputFilter/CreateContentNegotiationInputFilterTest.php
This file contains 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,114 @@ | ||
<?php | ||
/** | ||
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause | ||
* @copyright Copyright (c) 2014 Zend Technologies USA Inc. (http://www.zend.com) | ||
*/ | ||
|
||
namespace ZFTest\Apigility\Admin\InputFilter; | ||
|
||
use PHPUnit_Framework_TestCase as TestCase; | ||
use ZF\Apigility\Admin\InputFilter\CreateContentNegotiationInputFilter; | ||
|
||
class CreateContentNegotiationInputFilterTest extends TestCase | ||
{ | ||
public function dataProviderIsValid() | ||
{ | ||
return array( | ||
'with-content-name' => array( | ||
array( | ||
'content_name' => 'test', | ||
'Zend\View\Model\ViewModel' => array('text/html', 'application/xhtml+xml'), | ||
) | ||
), | ||
); | ||
} | ||
|
||
public function dataProviderIsInvalid() | ||
{ | ||
return array( | ||
'missing-content-name' => array( | ||
array( | ||
'Zend\View\Model\ViewModel' => array('text/html', 'application/xhtml+xml'), | ||
), | ||
array( | ||
'content_name' => array('No content_name was provided; must be present for new negotiators.'), | ||
), | ||
), | ||
'null-content-name' => array( | ||
array( | ||
'content_name' => null, | ||
'Zend\View\Model\ViewModel' => array('text/html', 'application/xhtml+xml'), | ||
), | ||
array( | ||
'content_name' => array('Content name provided is invalid; must be a string'), | ||
), | ||
), | ||
'bool-content-name' => array( | ||
array( | ||
'content_name' => true, | ||
'Zend\View\Model\ViewModel' => array('text/html', 'application/xhtml+xml'), | ||
), | ||
array( | ||
'content_name' => array('Content name provided is invalid; must be a string'), | ||
), | ||
), | ||
'int-content-name' => array( | ||
array( | ||
'content_name' => 1, | ||
'Zend\View\Model\ViewModel' => array('text/html', 'application/xhtml+xml'), | ||
), | ||
array( | ||
'content_name' => array('Content name provided is invalid; must be a string'), | ||
), | ||
), | ||
'float-content-name' => array( | ||
array( | ||
'content_name' => 1.1, | ||
'Zend\View\Model\ViewModel' => array('text/html', 'application/xhtml+xml'), | ||
), | ||
array( | ||
'content_name' => array('Content name provided is invalid; must be a string'), | ||
), | ||
), | ||
'array-content-name' => array( | ||
array( | ||
'content_name' => array('content_name'), | ||
'Zend\View\Model\ViewModel' => array('text/html', 'application/xhtml+xml'), | ||
), | ||
array( | ||
'content_name' => array('Content name provided is invalid; must be a string'), | ||
), | ||
), | ||
'object-content-name' => array( | ||
array( | ||
'content_name' => (object) array('content_name'), | ||
'Zend\View\Model\ViewModel' => array('text/html', 'application/xhtml+xml'), | ||
), | ||
array( | ||
'content_name' => array('Content name provided is invalid; must be a string'), | ||
), | ||
), | ||
); | ||
} | ||
|
||
/** | ||
* @dataProvider dataProviderIsValid | ||
*/ | ||
public function testIsValid($data) | ||
{ | ||
$filter = new CreateContentNegotiationInputFilter; | ||
$filter->setData($data); | ||
$this->assertTrue($filter->isValid(), var_export($filter->getMessages(), 1)); | ||
} | ||
|
||
/** | ||
* @dataProvider dataProviderIsInvalid | ||
*/ | ||
public function testIsInvalid($data, $messages) | ||
{ | ||
$filter = new CreateContentNegotiationInputFilter; | ||
$filter->setData($data); | ||
$this->assertFalse($filter->isValid()); | ||
$this->assertEquals($messages, $filter->getMessages()); | ||
} | ||
} |