From a3a7ef4296ed5c34e8830e1bf2ffadc161e4fe67 Mon Sep 17 00:00:00 2001 From: dogmatic69 Date: Mon, 1 Oct 2012 00:11:43 +0100 Subject: [PATCH] adding tests for the image helper --- .../Test/Case/View/Helper/ImageHelperTest.php | 236 +++++++++++- Core/Libs/View/Helper/ImageHelper.php | 362 +++++++++--------- 2 files changed, 414 insertions(+), 184 deletions(-) diff --git a/Core/Libs/Test/Case/View/Helper/ImageHelperTest.php b/Core/Libs/Test/Case/View/Helper/ImageHelperTest.php index 7e5b68d41..2668c1ed4 100644 --- a/Core/Libs/Test/Case/View/Helper/ImageHelperTest.php +++ b/Core/Libs/Test/Case/View/Helper/ImageHelperTest.php @@ -18,6 +18,26 @@ public function setUp() { parent::setUp(); $View = new View(); $this->Image = new ImageHelper($View); + + $images = array( + 'foo' => array ( + 'foo1' => 'foo1.png', + 'foo2' => 'foo2.png', + 'delete' => 'delete.png' + ), + 'bar' => array ( + 'bar1' => 'bar1.png', + 'bar2' => 'bar2.png' + ), + 'folders' => array( + 'folder' => 'folder.png', + 'empty' => 'empty.png' + ), + 'unknown' => array( + 'unknown' => 'unknown.png' + ) + ); + Configure::write('CoreImages.images', $images); } /** @@ -34,25 +54,100 @@ public function tearDown() { /** * testImage method * - * @return void + * @dataProvider imageDataProvider */ - public function testImage() { + public function testImage($data, $expected) { + $result = $this->Image->image($data['path'], $data['key']); + $this->assertTags($result, $expected); + } + +/** + * @brief data provider for testing images + * + * @return array + */ + public function imageDataProvider() { + return array( + 'simple' => array( + array('path' => 'foo', 'key' => 'foo1'), + array( + array('img' => array( + 'src' => '/img/core/icons/foo/foo1.png', + 'width' => '20px', + 'title' => 'foo1', + 'alt' => 'foo1' + )) + ) + ), + 'missing' => array( + array('path' => 'fake', 'key' => 'foo1'), + array(array()) + ) + ); } /** * testFindByExtention method * - * @return void + * @dataProvider findByExtentionDataProvider */ - public function testFindByExtention() { + public function testFindByExtention($data, $expected) { + $result = $this->Image->findByExtention($data['ext'], $data['config']); + $this->assertTags($result, $expected); } /** - * testFindByChildren method + * @brief data provider for testing by extention * - * @return void + * @return array */ - public function testFindByChildren() { + public function findByExtentionDataProvider() { + return array( + 'empty' => array( + array('ext' => null, 'config' => array()), + array( + array('img' => array( + 'src' => '/img/core/icons/folders/empty.png', + 'width' => '20px', + 'title' => 'folder', + 'alt' => 'folder' + )) + ) + ), + 'foo' => array( + array('ext' => 'foo1', 'config' => array()), + array( + array('img' => array( + 'src' => '/img/core/icons/foo/foo1.png', + 'width' => '20px', + 'title' => 'foo1', + 'alt' => 'foo1' + )) + ) + ), + 'dot_ext' => array( + array('ext' => '.baz', 'config' => array()), + array( + array('img' => array( + 'src' => '/img/core/icons/unknown/unknown.png', + 'width' => '20px', + 'title' => 'unknown', + 'alt' => 'unknown' + )) + ) + ), + 'delete_niceTitleText' => array( + array('ext' => 'delete', 'config' => array()), + array( + array('img' => array( + 'src' => '/img/core/icons/foo/delete.png', + 'width' => '20px', + 'title' => 'Delete some :: Tick the checkboxes next to the you want to delete then click here.<br/>If possible the will be moved to the trash can. If not they will be deleted permanently.', + 'alt' => 'Delete some' + )) + ) + ) + ); } /** @@ -61,6 +156,133 @@ public function testFindByChildren() { * @return void */ public function testGetRelativePath() { + $expected = 'core/icons/foo/foo1.png'; + $result = $this->Image->getRelativePath('foo', 'foo1'); + $this->assertEquals($expected, $result); + $this->assertEmpty($this->Image->errors); + + $expected = 'core/icons/bar/bar1.png'; + $result = $this->Image->getRelativePath('bar', 'bar1'); + $this->assertEquals($expected, $result); + $this->assertEmpty($this->Image->errors); + + $result = $this->Image->getRelativePath('bar', 'foo1'); + $this->assertFalse($result); + + $expected = array('CoreImages.images.bar.foo1 does not exist'); + $this->assertEquals($expected, $this->Image->errors); + } + +/** + * @brief test checking places exist + * + * @return void + */ + public function testPlaceExists() { + $expected = array('foo', 'bar'); + $result = $this->Image->placeExists(array('foo', 'bar')); + $this->assertEquals($expected, $result); + $this->assertEmpty($this->Image->errors); + + $expected = array('foo'); + $result = $this->Image->placeExists(array('foo', 'baz')); + $this->assertEquals($expected, $result); + $this->assertEmpty($this->Image->errors); + + $expected = array('foo'); + $result = $this->Image->placeExists('foo'); + $this->assertEquals($expected, $result); + $this->assertEmpty($this->Image->errors); + + $result = $this->Image->placeExists(array('baz')); + $this->assertFalse($result); + + $expected = array('the place(s) does not exist.'); + $this->assertEquals($expected, $this->Image->errors); + } + +/** + * @brief test getting image + */ + public function testGetImages() { + $expected = array( + 'foo' => array ( + 'foo1' => 'foo1.png', + 'foo2' => 'foo2.png', + 'delete' => 'delete.png' + ), + 'bar' => array ( + 'bar1' => 'bar1.png', + 'bar2' => 'bar2.png' + ), + 'folders' => array( + 'folder' => 'folder.png', + 'empty' => 'empty.png' + ), + 'unknown' => array( + 'unknown' => 'unknown.png' + ) + ); + $result = $this->Image->getImages(); + $this->assertEquals($expected, $result); + } + +/** + * @brief test getting places + */ + public function testGetPlaces() { + $expected = array('foo', 'bar', 'folders', 'unknown'); + $result = $this->Image->getPlaces(); + $this->assertEquals($expected, $result); + } + +/** + * @brief test exists + * + * @dataProvider existsDataProvider + */ + public function testExists($data, $expected) { + foreach($expected as $returnType => $expect) { + $result = $this->Image->exists($data['place'], $data['key'], $returnType); + $this->assertEquals($expect, $result); + $this->assertEmpty($this->Image->errors); + } + } + +/** + * @brief data provider for existing images + * @return type + */ + public function existsDataProvider() { + return array( + array( + array('place' => 'foo', 'key' => 'foo1'), + array( + 'fileName' => 'foo1.png', + 'relativePath' => 'core/icons/foo/foo1.png', + 'absolutePath' => true + ) + ), + array( + array('place' => 'bar', 'key' => 'bar1'), + array( + 'fileName' => 'bar1.png', + 'relativePath' => 'core/icons/bar/bar1.png', + 'absolutePath' => true + ) + ), + ); + } + +/** + * @brief test non existant + */ + public function testNonExists() { + $result = $this->Image->exists('foo', 'bar1'); + $this->assertFalse($result); + + $expected = array('CoreImages.images.foo.bar1 does not exist'); + $this->assertEquals($expected, $this->Image->errors); } } diff --git a/Core/Libs/View/Helper/ImageHelper.php b/Core/Libs/View/Helper/ImageHelper.php index 9c836b2ef..6d2a8e70c 100755 --- a/Core/Libs/View/Helper/ImageHelper.php +++ b/Core/Libs/View/Helper/ImageHelper.php @@ -1,204 +1,212 @@ '20px' - ); - - public $places = null; - - public $images = null; - - function image($path = null, $key = null) { - $images = Configure::read('CoreImages.images'); - if (!$path && !$key) { - $this->errors[] = 'Give some data'; - return false; - } - - if (!$path) { - return $this->findByExtention($key); - } - - return $this->Html->image( - Configure::read('CoreImages.path') . $path . '/' . $images[$path][$key], - $this->settings + array( - 'title' => $this->niceTitleText($key), - 'alt' => $this->niceAltText($key) - ) - ); +/** + * ImageHelper + * + * @package + * @author Carl Sutton (dogmatic69) + * @copyright Copyright (c) 2010 + * @version $Id$ + * @access public + */ +class ImageHelper extends AppHelper { +/** + * @brief default config for the images + * + * @var array + */ + public $settings = array( + 'width' => '20px' + ); + +/** + * @brief cached list of the places + * + * @var array + */ + protected $_places = array(); + +/** + * @brief cached list of the images + * + * @var array + */ + protected $_images = array(); + +/** + * @brief get an image + * + * @param string $path the relative path (to core images path) + * @param string $key the key + * + * @return boolean|string + */ + public function image($path, $key, array $config = array()) { + $image = $this->getRelativePath($path, $key); + if(!$image) { + return false; } - /** - * ImageHelper::findByExtention() - * - * @param mixed $extention - * @return - */ - function findByExtention($extention = null) { - $images = Configure::read('CoreImages'); - $imageData = array(); - if (!$extention) { - $imageData['path'] = Configure::read('CoreImages.path') . 'folders/' . $images['images']['folders']['empty']; - $imageData['title'] = $imageData['alt'] = $this->niceAltText('folder'); - } - - if (empty($imageData)) { - foreach ($images['images'] as $path => $image) { - if (isset($image[$extention])) { - $imageData['path'] = Configure::read('CoreImages.path') . $path . '/' . $image[$extention]; - $imageData['title'] = $imageData['alt'] = $this->niceTitleText($extention); - } - } - } - - if ($extention[0] == '.' || empty($imageData)) { - $imageData['path'] = Configure::read('CoreImages.path') . 'unknown/' . $images['images']['unknown']['unknown']; - $imageData['title'] = $imageData['alt'] = $this->niceAltText('unknown'); - } - - return $this->Html->image( - $imageData['path'], - $this->settings + array( - 'title' => $imageData['title'], - 'alt' => $imageData['alt'] - ) - ); + return $this->Html->image($image, $this->_config($config, $key)); + } + +/** + * @brief find an image by its extension + * + * @param string $extention the extension to lookup + * + * @return string + */ + public function findByExtention($extention = null, array $config = array()) { + $images = Configure::read('CoreImages'); + $imageData = array(); + $key = null; + if (!$extention) { + $imageData['path'] = $images['path'] . 'folders/' . $images['images']['folders']['empty']; + $key = 'folder'; } - /** - * ImageHelper::findByExtention() - * - * @param mixed $extention - * @return - */ - function findByChildren($children = array()) { - $images = Configure::read('CoreImages.images'); - if (empty($children[1])) { - return $this->Html->image( - Configure::read('CoreImages.path') . 'folders/' . $images['folders']['empty'], - $this->settings + array( - 'title' => __('Empty Folder'), - 'alt' => __('Empty Folder') - ) - ); - } - - App::import('File'); - - foreach ($children[1] as $child) { - $File = new File($child); - $ext = $File->ext(); - - if (!isset($data[$ext])) { - $data[$ext] = 0; - continue; + if (empty($imageData)) { + foreach ($images['images'] as $path => $image) { + if (isset($image[$extention])) { + $imageData['path'] = $images['path'] . $path . '/' . $image[$extention]; + $key = $extention; } - - $data[$ext]++; - unset($File); } - - $highest = 0; - $_ext = ''; - foreach ($data as $k => $v) { - if ($v > $highest) { - $highest = $v; - $_ext = $k; - } - } - - return $this->findByExtention($_ext); } - function getRelativePath($places = null, $key = null) { - $places = $this->__placeExists($places); - - if (!$places) { - return $places; - } - - if (!$key) { - $this->errors[] = 'No key or place given to find a path'; - } - - foreach ($this->__getImages() as $path => $image) { - $return = $this->__imageExists($path, $key, 'relativePath'); - - if ($return !== false) { - return $return; - } - } + if ($extention[0] == '.' || empty($imageData)) { + $imageData['path'] = $images['path'] . 'unknown/' . $images['images']['unknown']['unknown']; + $key = 'unknown'; } - function __placeExists($places = null) { - if (!is_array($places)) { - $places = array($places); - } - - foreach ($places as $k => $place) { - if (!in_array($place, $this->__getPlaces())) { - unset($places[$k]); - } - } + return $this->Html->image($imageData['path'], $this->_config($config, $key)); + } + +/** + * @brief get a relative path + * + * @param string $place the place + * @param string $key the key + * + * @return boolean|string + */ + public function getRelativePath($place, $key = null) { + return $this->exists($place, $key, 'relativePath'); + } + +/** + * @brief check if the passed location exists + * + * @param type $places + * + * @return boolean|array + */ + public function placeExists($places = null) { + if (!is_array($places)) { + $places = array($places); + } - if (empty($places)) { - $this->errors[] = 'the place(s) does not exist.'; - return false; + $currentPlaces = $this->getPlaces(); + foreach ($places as $k => $place) { + if (!in_array($place, $currentPlaces)) { + unset($places[$k]); } - - return $places; } - function __getImages() { - if (!$this->images) { - $this->images = Configure::read('CoreImages.images'); - } + if (empty($places)) { + $this->errors[] = 'the place(s) does not exist.'; + return false; + } - return $this->images; + return $places; + } + +/** + * @brief get a list of the images + * + * @return array + */ + public function getImages() { + if (!$this->_images) { + $this->_images = Configure::read('CoreImages.images'); } - function __getPlaces() { - if (!$this->places) { - $this->places = array_keys($this->__getImages()); - } + return $this->_images; + } + +/** + * @brief get a list of possible locations + * + * @return array + */ + public function getPlaces() { + if (!$this->_places) { + $this->_places = array_keys($this->getImages()); + } - return $this->places; + return $this->_places; + } + +/** + * @brief check if an image exists + * + * You can get a return of the following: + * - fileName: the file name + * - relativePath: the full relative path + * - absolutePath: the full path + * + * @param string $place the location + * @param string $key the type + * @param string $returnType what data is returned + * + * @return boolean + */ + public function exists($place, $key, $returnType = null) { + $images = $this->getImages(); + + if (!isset($images[$place][$key])) { + $this->errors[] = 'CoreImages.images.' . $place . '.' . $key . ' does not exist'; + return false; } - function __imageExists($place, $key, $returnType = null) { - $images = $this->__getImages(); + switch ($returnType) { + case 'fileName': + return $images[$place][$key]; - if (!isset($images[$place][$key])) { - $this->errors[] = 'CoreImages.images.' . $place . '.' . $key . ' does not exist'; - return false; - } + case 'relativePath': + return Configure::read('CoreImages.path') . $place . '/' . $images[$place][$key]; - switch ($returnType) { - case 'fileName': - return $images[$place][$key]; - break; + case 'absolutePath': + // @todo implement this + } - case 'relativePath': - return Configure::read('CoreImages.path') . $place . '/' . $images[$place][$key]; - break; + return true; + } + +/** + * @brief get a config for the image tags + * + * @param array $config the config to overload with + * + * @return array + */ + protected function _config(array $config, $key = null) { + $config = array_merge($this->settings, $config); + if(empty($config['title'])) { + $config['title'] = $this->niceTitleText($key); + } - case 'absolutePath': - // @todo fix this - echo 'todo: ' . __LINE__ . ' - ' . __FILE__; - exit; - break; + if(substr($config['title'], -4) == ' :: ') { + $config['title'] = str_replace(' :: ', '', $config['title']); + } + if(empty($config['alt'])) { + $config['alt'] = $config['title']; + } - default: - return true; - } // switch + if(strstr($config['alt'], ' :: ') !== false) { + $config['alt'] = trim(current(explode(' :: ', $config['alt']))); } - } \ No newline at end of file + + return $config; + } +} \ No newline at end of file