Skip to content

Commit

Permalink
[zfcampus#249] Ensure content_name is validated correctly
Browse files Browse the repository at this point in the history
The ContentNegotiationInputFilter was not validating the `content_name`
key correctly, leading to invalidation of submitted data. The proposed
change fixes it.
  • Loading branch information
weierophinney committed Jan 26, 2015
1 parent 8e04810 commit 170f010
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/InputFilter/ContentNegotiationInputFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ public function isValid()
$isValid = true;

foreach ($this->data as $className => $mediaTypes) {
if ($className === 'content_name' && is_string($mediaTypes)) {
continue;
}

if ($className === 'content_name' && ! is_string($mediaTypes)) {
$this->messages[$className][] = 'Content name (' . $className . ') is invalid; must be a string';
$isValid = false;
continue;
}

if (! class_exists($className)) {
$this->messages[$className][] = 'Class name (' . $className . ') does not exist';
$isValid = false;
Expand Down
8 changes: 7 additions & 1 deletion test/InputFilter/ContentNegotiationInputFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ public function dataProviderIsValid()
'Zend\View\Model\ViewModel' => array('text/html', 'application/xhtml+xml'),
),
),
'with-content-name' => array(
array(
'content_name' => 'test',
'Zend\View\Model\ViewModel' => array('text/html', 'application/xhtml+xml'),
)
),
);
}

Expand Down Expand Up @@ -63,7 +69,7 @@ public function testIsValid($data)
{
$filter = new ContentNegotiationInputFilter;
$filter->setData($data);
$this->assertTrue($filter->isValid());
$this->assertTrue($filter->isValid(), var_export($filter->getMessages(), 1));
}

/**
Expand Down
70 changes: 70 additions & 0 deletions test/Model/ContentNegotiationResourceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?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\Model;

use PHPUnit_Framework_TestCase as TestCase;
use Zend\Config\Writer\PhpArray as ConfigWriter;
use ZF\Apigility\Admin\Model\ContentNegotiationModel;
use ZF\Apigility\Admin\Model\ContentNegotiationResource;
use ZF\Configuration\ConfigResource;

class ContentNegotiationResourceTest extends TestCase
{
public function setUp()
{
$this->configPath = sys_get_temp_dir() . '/zf-apigility-admin/config';
$this->globalConfigPath = $this->configPath . '/global.php';
$this->removeConfigMocks();
$this->createConfigMocks();
$this->configWriter = new ConfigWriter();
}

public function createConfigMocks()
{
if (!is_dir($this->configPath)) {
mkdir($this->configPath, 0775, true);
}

$contents = "<" . "?php\nreturn array();";
file_put_contents($this->globalConfigPath, $contents);
}

public function removeConfigMocks()
{
if (file_exists($this->globalConfigPath)) {
unlink($this->globalConfigPath);
}
if (is_dir($this->configPath)) {
rmdir($this->configPath);
}
if (is_dir(dirname($this->configPath))) {
rmdir(dirname($this->configPath));
}
}

public function createModelFromConfigArray(array $global)
{
$this->configWriter->toFile($this->globalConfigPath, $global);
$globalConfig = new ConfigResource($global, $this->globalConfigPath, $this->configWriter);
return new ContentNegotiationModel($globalConfig);
}

public function createResourceFromConfigArray(array $global)
{
return new ContentNegotiationResource($this->createModelFromConfigArray($global));
}


public function testCreateShouldAcceptContentNameAndReturnNewEntity()
{
$resource = $this->createResourceFromConfigArray(array());
$data = (object) array('content_name' => 'Test');
$entity = $resource->create($data);
$this->assertInstanceOf('ZF\Apigility\Admin\Model\ContentNegotiationEntity', $entity);
$this->assertEquals('Test', $entity->name);
}
}

0 comments on commit 170f010

Please sign in to comment.