diff --git a/Core/Libs/Test/Case/View/Helper/InfinitasHelperTest.php b/Core/Libs/Test/Case/View/Helper/InfinitasHelperTest.php index 0f3f34e34..a1dc10539 100644 --- a/Core/Libs/Test/Case/View/Helper/InfinitasHelperTest.php +++ b/Core/Libs/Test/Case/View/Helper/InfinitasHelperTest.php @@ -8,6 +8,10 @@ * */ class InfinitasHelperTest extends CakeTestCase { + public $fixtures = array( + 'plugin.management.ticket', + 'plugin.users.user' + ); /** * setUp method @@ -32,43 +36,199 @@ public function tearDown() { } /** - * testStatus method + * @brief test status icon * - * @return void + * @dataProvider statusDataProvider */ - public function testStatus() { + public function testStatus($data, $expected) { + $result = $this->Infinitas->status($data['status'], $data['options']); + $this->assertTags($result, $expected); } /** - * testFeatured method - * - * @return void + * @brief data provider for testing status */ - public function testFeatured() { + public function statusDataProvider() { + return array( + 'basic_yes' => array( + array('status' => 1, 'options' => array()), + array( + array('img' => array( + 'src' => '/img/core/icons/status/active.png', + 'class' => 'icon-status', + 'title' => 'Status :: This record is active', + 'alt' => 'On' + )) + ) + ), + 'custom_yes' => array( + array('status' => 1, 'options' => array('title_yes' => 'foo bar')), + array( + array('img' => array( + 'src' => '/img/core/icons/status/active.png', + 'class' => 'icon-status', + 'title' => 'foo bar', + 'alt' => 'On' + )) + ) + ), + 'basic_no' => array( + array('status' => 0, 'options' => array()), + array( + array('img' => array( + 'src' => '/img/core/icons/status/inactive.png', + 'class' => 'icon-status', + 'title' => 'Status :: This record is disabled', + 'alt' => 'Off' + )) + ) + ), + 'custom_no' => array( + array('status' => 0, 'options' => array('title_no' => 'foo bar')), + array( + array('img' => array( + 'src' => '/img/core/icons/status/inactive.png', + 'class' => 'icon-status', + 'title' => 'foo bar', + 'alt' => 'Off' + )) + ) + ) + ); } /** - * testLoggedInUserText method + * @brief test mass action check box * - * @return void + * @dataProvider massActionCheckBoxDataProvider */ - public function testLoggedInUserText() { + public function testMassActionCheckBox($data, $expected) { + $this->Infinitas->request->params['models'] = array('User' => array('plugin' => 'Users', 'model' => 'User')); + $result = $this->Infinitas->massActionCheckBox($data['data'], $data['options']); + $this->assertTags($result, $expected); } /** - * testMassActionCheckBox method + * @brief data provider for mass action checkbox tests * - * @return void + * @return array */ - public function testMassActionCheckBox() { - } + public function massActionCheckBoxDataProvider() { + return array( + 'basic' => array( + array( + 'data' => array( + 'User' => array( + 'id' => 123 + ) + ), + 'options' => array( -/** - * testHasPlugin method - * - * @return void - */ - public function testHasPlugin() { + )), + array( + array('input' => array( + 'type' => 'hidden', + 'name' => 'data[User][0][massCheckBox]', + 'id' => 'User0MassCheckBox_', + 'value' => 0 + )), + array('input' => array( + 'type' => 'checkbox', + 'name' => 'data[User][0][massCheckBox]', + 'id' => 'User0MassCheckBox', + 'value' => 123 + )), + ) + ), + 'hidden' => array( + array( + 'data' => array( + 'User' => array( + 'id' => 123 + ) + ), + 'options' => array( + 'hidden' => true + )), + array( + array('input' => array( + 'type' => 'hidden', + 'name' => 'data[User][0][massCheckBox]', + 'id' => 'User0MassCheckBox_', + 'value' => 0 + )), + array('input' => array( + 'type' => 'checkbox', + 'hidden' => 1, + 'name' => 'data[User][0][massCheckBox]', + 'id' => 'User0MassCheckBox', + 'value' => 123 + )), + ) + ), + 'checked' => array( + array( + 'data' => array( + 'User' => array( + 'id' => 123 + ) + ), + 'options' => array( + 'checked' => true + )), + array( + array('input' => array( + 'type' => 'hidden', + 'name' => 'data[User][0][massCheckBox]', + 'id' => 'User0MassCheckBox_', + 'value' => 0 + )), + array('input' => array( + 'type' => 'checkbox', + 'checked' => 'checked', + 'name' => 'data[User][0][massCheckBox]', + 'id' => 'User0MassCheckBox', + 'value' => 123 + )), + ) + ), + 'aliased' => array( + array( + 'data' => array( + 'MyUser' => array( + 'id' => 123 + ) + ), + 'options' => array( + 'alias' => 'MyUser' + )), + array( + array('input' => array( + 'type' => 'hidden', + 'name' => 'data[MyUser][0][massCheckBox]', + 'id' => 'MyUser0MassCheckBox_', + 'value' => 0 + )), + array('input' => array( + 'type' => 'checkbox', + 'name' => 'data[MyUser][0][massCheckBox]', + 'id' => 'MyUser0MassCheckBox', + 'value' => 123 + )), + ) + ), + 'missing_data' => array( + array( + 'data' => array( + 'MyUser' => array( + 'id' => 123 + ) + ), + 'options' => array( + )), + array() + ) + ); } } diff --git a/Core/Libs/View/Helper/InfinitasHelper.php b/Core/Libs/View/Helper/InfinitasHelper.php index 70f03457f..808211a60 100755 --- a/Core/Libs/View/Helper/InfinitasHelper.php +++ b/Core/Libs/View/Helper/InfinitasHelper.php @@ -1,213 +1,137 @@ 'No error', + JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded', + JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded', + JSON_ERROR_SYNTAX => 'Syntax error', + ); + + protected $_menuData = ''; + + protected $_menuLevel = 0; + + public $View = null; + + private $__massActionCheckBoxCounter = 0; + +/** + * Set to true when the menu has a current marker to avoid duplicates. + * @var unknown_type + */ + protected $_currentCssDone = false; + +/** + * Create a status icon. + * + * Takes a int 0 || 1 and returns a icon with title tags etc to be used + * in places like admin to show iff something is on/off etc. + * + * @param int $status the status tinyint(1) from a db find. + * + * @return string some html for the generated image. + */ + public function status($status, $options = array()) { + $params = array('class' => 'icon-status'); + + $options = array_merge( + array( + 'title_yes' => __d('infinitas', 'Status :: This record is active'), + 'title_no' => __d('infinitas', 'Status :: This record is disabled'), + ), + (array)$options ); - /** - * JSON errors. - * - * Set up some errors for json. - * @access public - */ - public $jsonErrors = array( - JSON_ERROR_NONE => 'No error', - JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded', - JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded', - JSON_ERROR_SYNTAX => 'Syntax error', - ); - protected $_menuData = ''; - - protected $_menuLevel = 0; - - public $external = true; - - public $View = null; - - private $__rowCount = 0; - - private $__massActionCheckBoxCounter = 0; - - /** - * Set to true when the menu has a current marker to avoid duplicates. - * @var unknown_type - */ - protected $_currentCssDone = false; - - /** - * Create a status icon. - * - * Takes a int 0 || 1 and returns a icon with title tags etc to be used - * in places like admin to show iff something is on/off etc. - * - * @param int $status the status tinyint(1) from a db find. - * - * @return string some html for the generated image. - */ - public function status($status = null, $options = array()) { - $image = false; - $params = array('class' => 'icon-status'); - - $options = array_merge( - array( - 'title_yes' => __d('infinitas', 'Status :: This record is active'), - 'title_no' => __d('infinitas', 'Status :: This record is disabled'), - ), - (array)$options - ); - - switch (strtolower($status)) { - case 1: - case 'yes': - case 'on': - if ($this->external) { - $params['title'] = $options['title_yes']; - } - - $image = $this->Html->image( - $this->Image->getRelativePath('status', 'active'), - $params + array( - 'alt' => __d('infinitas', 'On') - ) - ); - break; - - case 0: - case 'no': - case 'off': - if ($this->external) { - $params['title'] = $options['title_no']; - } - - $image = $this->Html->image( - $this->Image->getRelativePath('status', 'inactive'), - $params + array( - 'alt' => __d('infinitas', 'Off') - ) - ); - break; - } - - return $image; + if(in_array((string)strtolower($status), array('1', 'yes', 'on'))) { + $params['title'] = $options['title_yes']; + $params['alt'] = __d('infinitas', 'On'); + return $this->Html->image($this->Image->getRelativePath('status', 'active'), $params); } - /** - * Featured icon. - * - * Creates a featured icon like the status and locked. - * - * @param array $record the data from find - * @param string $model the model alias - * - * @return string html of the icon. - */ - public function featured($record = array(), $model = 'Feature') { - $record = array_filter($record[$model]); - if (empty($record)) { - return $this->Html->image( - $this->Image->getRelativePath('status', 'not-featured'), - array( - 'alt' => __('No'), - 'title' => __('Not a featured item'), - 'width' => '16px' - ) - ); - } - - return $this->Html->image( - $this->Image->getRelativePath('status', 'featured'), - array( - 'alt' => __('Yes'), - 'title' => __('Featured Item'), - 'width' => '16px' - ) - ); - } + $params['title'] = $options['title_no']; + $params['alt'] = __d('infinitas', 'Off'); + return $this->Html->image($this->Image->getRelativePath('status', 'inactive'), $params); + } - public function loggedInUserText($counts) { - $allInIsAre = ($counts['all'] > 1) ? __('are') : __('is'); - $loggedInIsAre = ($counts['loggedIn'] > 1) ? __('are') : __('is'); - $guestsIsAre = ($counts['guests'] > 1) ? __('are') : __('is'); - $guests = ($counts['guests'] > 1) ? __('guests') : __('a guest'); - - return '

'. - sprintf( - __('There %s %s people on the site, %s %s logged in and %s %s %s.'), - $allInIsAre, $counts['all'], - $counts['loggedIn'], $loggedInIsAre, - $counts['guests'], $guestsIsAre, - $guests - ). - '

 

'; +/** + * @brief generate a checkbox for rows that use mass_action stuff + * + * it will keep track of the $i for the checkbox number so there are no duplicates. + * MassActionComponent::filter() will remove these fields from the searches so there + * are no sql errors. + * + * @param array $data the row from find either find('first') or find('all')[x] + * @param array $options set the fk or model manually + * + * @return a checkbox + */ + public function massActionCheckBox(array $data, $options = array()) { + $alias = current(array_keys($this->request->params['models'])); + $modelClass = implode('.', $this->request->params['models'][$alias]); + $options = array_merge( + array('alias' => $alias, 'hidden' => false, 'checked' => false, 'primaryKey' => false), + $options + ); + if(empty($options['primaryKey'])) { + $options['primaryKey'] = ClassRegistry::init($modelClass)->primaryKey; } - /** - * @brief generate a checkbox for rows that use mass_action stuff - * - * it will keep track of the $i for the checkbox number so there are no duplicates. - * MassActionComponent::filter() will remove these fields from the searches so there - * are no sql errors. - * - * @param array $data the row from find either find('first') or find('all')[x] - * @param array $options set the fk or model manually - * - * @return a checkbox - */ - public function massActionCheckBox($data = array(), $options = array()) { - $model = current(array_keys($this->request->params['models'])); - $modelClass = implode('.', $this->request->params['models'][$model]); - $options = array_merge( - array('model' => $model, 'primaryKey' => ClassRegistry::init($modelClass)->primaryKey, 'hidden' => false, 'checked' => false), - $options - ); - - if(!$data || !isset($data[$options['model']])) { - return false; - } - - $checkbox = $this->Form->checkbox( - $options['model'] . '.' . $this->__massActionCheckBoxCounter . '.' . 'massCheckBox', - array( - 'value' => $data[$options['model']][$options['primaryKey']], - 'hidden' => $options['hidden'], - 'checked' => $options['checked'] - ) - ); - - $this->__massActionCheckBoxCounter++; - - return $checkbox; + if(empty($data[$options['alias']]) || empty($data[$options['alias']][$options['primaryKey']])) { + return false; } - public function hasPlugin($plugin = null) { - return ClassRegistry::init('Installer.Plugin')->isInstalled($plugin); - } + $checkbox = $this->Form->checkbox( + $options['alias'] . '.' . $this->__massActionCheckBoxCounter++ . '.' . 'massCheckBox', + array( + 'value' => $data[$options['alias']][$options['primaryKey']], + 'hidden' => $options['hidden'], + 'checked' => $options['checked'] + ) + ); + + return $checkbox; } +}