PageRenderTime 55ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/dev/tests/unit/testsuite/Mage/Backend/Block/Widget/Grid/ColumnSetTest.php

https://bitbucket.org/jokusafet/magento2
PHP | 360 lines | 235 code | 53 blank | 72 comment | 0 complexity | 896d1d0c445f26a89b07446a3b0c9120 MD5 | raw file
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Magento
  22. * @package Mage_Backend
  23. * @subpackage unit_tests
  24. * @copyright Copyright (c) 2012 X.commerce, Inc. (http://www.magentocommerce.com)
  25. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  26. */
  27. class Mage_Backend_Block_Widget_Grid_ColumnSetTest extends PHPUnit_Framework_TestCase
  28. {
  29. /**
  30. * @var Mage_Backend_Block_Widget_Grid_ColumnSet
  31. */
  32. protected $_block;
  33. /**
  34. * @var PHPUnit_Framework_MockObject_MockObject
  35. */
  36. protected $_layoutMock;
  37. /**
  38. * @var PHPUnit_Framework_MockObject_MockObject
  39. */
  40. protected $_columnMock;
  41. /**
  42. * @var PHPUnit_Framework_MockObject_MockObject
  43. */
  44. protected $_helperMock;
  45. /**
  46. * @var PHPUnit_Framework_MockObject_MockObject
  47. */
  48. protected $_factoryMock;
  49. /**
  50. * @var PHPUnit_Framework_MockObject_MockObject
  51. */
  52. protected $_subtotalsMock;
  53. /**
  54. * @var PHPUnit_Framework_MockObject_MockObject
  55. */
  56. protected $_totalsMock;
  57. /**
  58. * @var PHPUnit_Framework_MockObject_MockObject
  59. */
  60. protected $_gridMock;
  61. protected function setUp()
  62. {
  63. $this->_columnMock = $this->getMock('Mage_Backend_Block_Widget_Grid_Column',
  64. array('setSortable', 'setRendererType', 'setFilterType'), array(), '', false);
  65. $this->_layoutMock = $this->getMock('Mage_Core_Model_Layout', array(), array(), '', false);
  66. $this->_layoutMock
  67. ->expects($this->any())
  68. ->method('getChildBlocks')
  69. ->will($this->returnValue(array('column' => $this->_columnMock)));
  70. $this->_helperMock = $this->getMock('Mage_Backend_Helper_Data', array(), array(), '', false);
  71. $this->_helperMock
  72. ->expects($this->any())
  73. ->method('__')
  74. ->will($this->returnValue('TRANSLATED STRING'));
  75. $this->_factoryMock = $this->getMock('Mage_Backend_Model_Widget_Grid_Row_UrlGeneratorFactory', array(), array(),
  76. '', false
  77. );
  78. $this->_subtotalsMock = $this->getMock(
  79. 'Mage_Backend_Model_Widget_Grid_SubTotals', array(), array(), '', false
  80. );
  81. $this->_totalsMock = $this->getMock(
  82. 'Mage_Backend_Model_Widget_Grid_Totals', array(), array(), '', false
  83. );
  84. $arguments = array(
  85. 'layout' => $this->_layoutMock,
  86. 'helper' => $this->_helperMock,
  87. 'generatorFactory' => $this->_factoryMock,
  88. 'totals' => $this->_totalsMock,
  89. 'subtotals' => $this->_subtotalsMock
  90. );
  91. $objectManagerHelper = new Magento_Test_Helper_ObjectManager($this);
  92. $this->_block = $objectManagerHelper->getBlock('Mage_Backend_Block_Widget_Grid_ColumnSet', $arguments);
  93. $this->_block->setNameInLayout('grid.columnSet');
  94. }
  95. public function tearDown()
  96. {
  97. unset($this->_block);
  98. unset($this->_layoutMock);
  99. unset($this->_columnMock);
  100. unset($this->_helperMock);
  101. unset($this->_factoryMock);
  102. unset($this->_totalsMock);
  103. unset($this->_subtotalsMock);
  104. }
  105. public function testSetSortablePropagatesSortabilityToChildren()
  106. {
  107. $this->_columnMock->expects($this->once())->method('setSortable')->with(false);
  108. $this->_block->setSortable(false);
  109. }
  110. public function testSetSortablePropagatesSortabilityToChildrenOnlyIfSortabilityIsFalse()
  111. {
  112. $this->_columnMock->expects($this->never())->method('setSortable');
  113. $this->_block->setSortable(true);
  114. }
  115. public function testSetRendererTypePropagatesRendererTypeToColumns()
  116. {
  117. $this->_columnMock->expects($this->once())->method('setRendererType')->with('renderer', 'Renderer_Class');
  118. $this->_block->setRendererType('renderer', 'Renderer_Class');
  119. }
  120. public function testSetFilterTypePropagatesFilterTypeToColumns()
  121. {
  122. $this->_columnMock->expects($this->once())->method('setFilterType')->with('filter', 'Filter_Class');
  123. $this->_block->setFilterType('filter', 'Filter_Class');
  124. }
  125. public function testGetRowUrlIfUrlPathNotSet()
  126. {
  127. $this->assertEquals('#', $this->_block->getRowUrl(new StdClass()));
  128. }
  129. public function testGetRowUrl()
  130. {
  131. $generatorClass = 'Mage_Backend_Model_Widget_Grid_Row_UrlGenerator';
  132. $itemMock = $this->getMock('Varien_Object', array(), array(), '', false);
  133. $rowUrlGenerator = $this->getMock('Mage_Backend_Model_Widget_Grid_Row_UrlGenerator', array('getUrl'), array(),
  134. '', false
  135. );
  136. $rowUrlGenerator->expects($this->once())
  137. ->method('getUrl')
  138. ->with($this->equalTo($itemMock))
  139. ->will($this->returnValue('http://localhost/mng/item/edit'));
  140. $factoryMock = $this->getMock('Mage_Backend_Model_Widget_Grid_Row_UrlGeneratorFactory',
  141. array('createUrlGenerator'), array(), '', false
  142. );
  143. $factoryMock->expects($this->once())
  144. ->method('createUrlGenerator')
  145. ->with($this->equalTo($generatorClass),
  146. $this->equalTo(array('args' => array('generatorClass' => $generatorClass)))
  147. )
  148. ->will($this->returnValue($rowUrlGenerator));
  149. $arguments = array(
  150. 'layout' => $this->_layoutMock,
  151. 'helper' => $this->_helperMock,
  152. 'generatorFactory' => $factoryMock,
  153. 'data' => array(
  154. 'rowUrl' => array('generatorClass' => $generatorClass)
  155. ),
  156. 'totals' => $this->_totalsMock,
  157. 'subtotals' => $this->_subtotalsMock
  158. );
  159. $objectManagerHelper = new Magento_Test_Helper_ObjectManager($this);
  160. /** @var $model Mage_Backend_Block_Widget_Grid_ColumnSet */
  161. $model = $objectManagerHelper->getBlock('Mage_Backend_Block_Widget_Grid_ColumnSet', $arguments);
  162. $url = $model->getRowUrl($itemMock);
  163. $this->assertEquals('http://localhost/mng/item/edit', $url);
  164. }
  165. public function testItemHasMultipleRows()
  166. {
  167. $item = new Varien_Object();
  168. // prepare sub-collection
  169. $subCollection = new Varien_Data_Collection();
  170. $subCollection->addItem(new Varien_Object(array('test4' => '1','test5' => '2')));
  171. $subCollection->addItem(new Varien_Object(array('test4' => '2','test5' => '2')));
  172. $item->setChildren($subCollection);
  173. $this->assertTrue($this->_block->hasMultipleRows($item));
  174. }
  175. public function testShouldRenderTotalWithNotEmptyCollection()
  176. {
  177. $this->_prepareLayoutWithGrid($this->_prepareGridMock($this->_getTestCollection()));
  178. $this->_block->setCountTotals(true);
  179. $this->assertTrue($this->_block->shouldRenderTotal());
  180. }
  181. public function testShouldRenderTotalWithEmptyCollection()
  182. {
  183. $this->_prepareLayoutWithGrid($this->_prepareGridMock(new Varien_Data_Collection()));
  184. $this->_block->setCountTotals(true);
  185. $this->assertFalse($this->_block->shouldRenderTotal());
  186. }
  187. public function testShouldRenderTotalWithFlagFalse()
  188. {
  189. $this->_block->setCountTotals(false);
  190. $this->assertFalse($this->_block->shouldRenderTotal());
  191. }
  192. public function testShouldRenderSubtotalWithFlagFalse()
  193. {
  194. $this->_block->setCountSubTotals(false);
  195. $this->assertFalse($this->_block->shouldRenderSubTotal(new Varien_Object()));
  196. }
  197. public function testShouldRenderSubtotalWithEmptySubData()
  198. {
  199. $this->_block->setCountSubTotals(true);
  200. $this->assertFalse($this->_block->shouldRenderSubTotal(new Varien_Object()));
  201. }
  202. public function testShouldRenderSubtotalWithNotEmptySubData()
  203. {
  204. $item = new Varien_Object();
  205. // prepare sub-collection
  206. $subCollection = new Varien_Data_Collection();
  207. $subCollection->addItem(new Varien_Object(array('test4' => '1','test5' => '2')));
  208. $subCollection->addItem(new Varien_Object(array('test4' => '2','test5' => '2')));
  209. $item->setChildren($subCollection);
  210. $this->_block->setCountSubTotals(true);
  211. $this->assertTrue($this->_block->shouldRenderSubTotal($item));
  212. }
  213. public function testUpdateItemByFirstMultiRow()
  214. {
  215. $item = new Varien_Object(array('test1' => '1'));
  216. // prepare sub-collection
  217. $subCollection = new Varien_Data_Collection();
  218. $subCollection->addItem(new Varien_Object(array('test4' => '1','test5' => '2')));
  219. $subCollection->addItem(new Varien_Object(array('test4' => '2','test5' => '2')));
  220. $item->setChildren($subCollection);
  221. $expectedItem = new Varien_Object(array('test1' => '1'));
  222. $expectedItem->addData(array('test4' => '1','test5' => '2'));
  223. $expectedItem->setChildren($subCollection);
  224. $this->_block->updateItemByFirstMultiRow($item);
  225. $this->assertEquals($expectedItem, $item);
  226. }
  227. public function testGetSubTotals()
  228. {
  229. // prepare sub-collection
  230. $subCollection = new Varien_Data_Collection();
  231. $subCollection->addItem(new Varien_Object(array('column' => '1')));
  232. $subCollection->addItem(new Varien_Object(array('column' => '1')));
  233. $this->_subtotalsMock->expects($this->once())
  234. ->method('countTotals')
  235. ->with($subCollection)
  236. ->will($this->returnValue(new Varien_Object(array('column' => '2'))));
  237. // prepare item
  238. $item = new Varien_Object(array('test1' => '1'));
  239. $item->setChildren($subCollection);
  240. $this->assertEquals(
  241. new Varien_Object(array('column' => '2')),
  242. $this->_block->getSubTotals($item)
  243. );
  244. }
  245. public function testGetTotals()
  246. {
  247. $collection = $this->_getTestCollection();
  248. $this->_prepareLayoutWithGrid($this->_prepareGridMock($collection));
  249. $this->_totalsMock->expects($this->once())
  250. ->method('countTotals')
  251. ->with($collection)
  252. ->will($this->returnValue(new Varien_Object(array('test1' => '3', 'test2' => '2'))));
  253. $this->assertEquals(
  254. new Varien_Object(array('test1' => '3', 'test2' => '2')),
  255. $this->_block->getTotals()
  256. );
  257. }
  258. /**
  259. * Retrieve prepared mock for Mage_Backend_Model_Widget_Grid with collection
  260. *
  261. * @param Varien_Data_Collection $collection
  262. * @return PHPUnit_Framework_MockObject_MockObject
  263. */
  264. protected function _prepareGridMock($collection)
  265. {
  266. // prepare block grid
  267. $gridMock = $this->getMock('Mage_Backend_Model_Widget_Grid', array('getCollection'), array(), '', true);
  268. $gridMock->expects($this->any())
  269. ->method('getCollection')
  270. ->will($this->returnValue($collection));
  271. return $gridMock;
  272. }
  273. /**
  274. * Retrieve test collection
  275. *
  276. * @return Varien_Data_Collection
  277. */
  278. protected function _getTestCollection()
  279. {
  280. $collection = new Varien_Data_Collection();
  281. $items = array(
  282. new Varien_Object(array('test1' => '1', 'test2' => '2')),
  283. new Varien_Object(array('test1' => '1', 'test2' => '2')),
  284. new Varien_Object(array('test1' => '1', 'test2' => '2'))
  285. );
  286. foreach ($items as $item) {
  287. $collection->addItem($item);
  288. }
  289. return $collection;
  290. }
  291. /**
  292. * Prepare layout for receiving grid block
  293. *
  294. * @param PHPUnit_Framework_MockObject_MockObject $gridMock
  295. */
  296. protected function _prepareLayoutWithGrid($gridMock)
  297. {
  298. $this->_layoutMock->expects($this->any())
  299. ->method('getParentName')
  300. ->with('grid.columnSet')
  301. ->will($this->returnValue('grid'));
  302. $this->_layoutMock->expects($this->any())
  303. ->method('getBlock')
  304. ->with('grid')
  305. ->will($this->returnValue($gridMock));
  306. }
  307. }