addRowFromArray(array(Piwik_DataTable_Row::COLUMNS => array( 13,),)); /* * end fake tables */ /* * MAIN TABLE */ $table = new Piwik_DataTable; $subtable = new Piwik_DataTable; $idtable = $table->getId(); $idsubtable = $subtable->getId(); /* * create some fake tables to make sure that the serialized array of the first TABLE * does not take in consideration those tables */ $useless2 = new Piwik_DataTable; $useless1->addRowFromArray(array(Piwik_DataTable_Row::COLUMNS => array( 8487,),)); $useless3 = new Piwik_DataTable; $useless3->addRowFromArray(array(Piwik_DataTable_Row::COLUMNS => array( 8487,),)); /* * end fake tables */ $row = array(Piwik_DataTable_Row::COLUMNS => array( 0 => 1554, 1 => 42, 2 => 657,3 => 155744,), Piwik_DataTable_Row::DETAILS => array('logo' => 'test.png')); $row = new Piwik_DataTable_Row($row); $table->addRow($row); $table->addRowFromArray(array( Piwik_DataTable_Row::COLUMNS => array( 0 => 1554,1 => 42,), Piwik_DataTable_Row::DETAILS => array('url' => 'piwik.org'))); $table->addRowFromArray(array( Piwik_DataTable_Row::COLUMNS => array( 0 => 787877888787,), Piwik_DataTable_Row::DETAILS => array('url' => 'OUPLA ADDED'), Piwik_DataTable_Row::DATATABLE_ASSOCIATED => $subtable)); /* * SUB TABLE */ $row = array( Piwik_DataTable_Row::COLUMNS => array( 0 => 1554,), Piwik_DataTable_Row::DETAILS => array('searchengine' => 'google'), ); $subtable->addRowFromArray($row); $row = array( Piwik_DataTable_Row::COLUMNS => array( 0 => 84894,), Piwik_DataTable_Row::DETAILS => array('searchengine' => 'yahoo'), ); $subtable->addRowFromArray($row); $row = array( Piwik_DataTable_Row::COLUMNS => array( 0 => 4898978989,), Piwik_DataTable_Row::DETAILS => array('searchengine' => 'ask'), ); $subtable->addRowFromArray($row); /* * SUB SUB TABLE */ $subsubtable = new Piwik_DataTable; $subsubtable->addRowFromArray(array(Piwik_DataTable_Row::COLUMNS => array( 245), Piwik_DataTable_Row::DETAILS => array('yes' => 'subsubdetail1'),) ); $subsubtable->addRowFromArray(array(Piwik_DataTable_Row::COLUMNS => array( 13,), Piwik_DataTable_Row::DETAILS => array('yes' => 'subsubdetail2'),) ); $row = array( Piwik_DataTable_Row::COLUMNS => array( 0 => 666666666666666,), Piwik_DataTable_Row::DETAILS => array('url' => 'NEW ROW ADDED'), Piwik_DataTable_Row::DATATABLE_ASSOCIATED => $subsubtable); $subtable->addRowFromArray($row); $idsubsubtable = $subsubtable->getId(); $serialized = ($table->getSerialized()); $this->assertEqual(array_keys($serialized), array($idsubsubtable,$idsubtable,0)); $tableAfter = new Piwik_DataTable; $tableAfter->loadFromSerialized($serialized[0]); $this->assertEqual($table->getRows(),$tableAfter->getRows()); $subsubtableAfter = new Piwik_DataTable; $subsubtableAfter->loadFromSerialized($serialized[$idsubsubtable]); $this->assertEqual($subsubtable->getRows(),$subsubtableAfter->getRows()); $this->assertEqual($table, Piwik_DataTable_Manager::getInstance()->getTable($idtable)); $this->assertEqual($subsubtable, Piwik_DataTable_Manager::getInstance()->getTable($idsubsubtable)); } /** * test with a row without child */ function test_rendererConsoleSimple() { $table = new Piwik_DataTable; $table->addRowFromArray(array(Piwik_DataTable_Row::COLUMNS => array( 'visits'=>245,'visitors'=>245), Piwik_DataTable_Row::DETAILS => array('logo' => 'test.png'),) ); $expected="- 1 ['visits' => 245, 'visitors' => 245] ['logo' => test.png] [idsubtable = ]
\n"; /* * RENDER */ $render = new Piwik_DataTable_Renderer_Console ($table); $rendered = $render->render(); $this->assertEqual($expected,$rendered); } /** * test with a row without child * a row with a child that has a child * a row with w child */ function test_rendererConsole2SubLevelAnd2Different() { $table = new Piwik_DataTable; $idtable = $table->getId(); $table->addRowFromArray(array(Piwik_DataTable_Row::COLUMNS => array( 'visits'=>245,'visitors'=>245), Piwik_DataTable_Row::DETAILS => array('logo' => 'test.png'),) ); $subsubtable = new Piwik_DataTable; $idsubsubtable = $subsubtable->getId(); $subsubtable->addRowFromArray(array(Piwik_DataTable_Row::COLUMNS => array( 'visits'=>2))); $subtable = new Piwik_DataTable; $idsubtable1 = $subtable->getId(); $subtable->addRowFromArray(array(Piwik_DataTable_Row::COLUMNS => array( 'visits'=>1), Piwik_DataTable_Row::DATATABLE_ASSOCIATED => $subsubtable)); $table->addRowFromArray(array(Piwik_DataTable_Row::COLUMNS => array( 'visits'=>3), Piwik_DataTable_Row::DATATABLE_ASSOCIATED => $subtable) ); $subtable2 = new Piwik_DataTable; $idsubtable2 = $subtable2->getId(); $subtable2->addRowFromArray(array(Piwik_DataTable_Row::COLUMNS => array( 'visits'=>5),)); $table->addRowFromArray(array(Piwik_DataTable_Row::COLUMNS => array( 'visits'=>9), Piwik_DataTable_Row::DATATABLE_ASSOCIATED => $subtable2) ); $expected= "- 1 ['visits' => 245, 'visitors' => 245] ['logo' => test.png] [idsubtable = ]
- 2 ['visits' => 3] [] [idsubtable = $idsubtable1]
*- 1 ['visits' => 1] [] [idsubtable = $idsubsubtable]
**- 1 ['visits' => 2] [] [idsubtable = ]
- 3 ['visits' => 9] [] [idsubtable = $idsubtable2]
*- 1 ['visits' => 5] [] [idsubtable = ]
"; /* * RENDER */ $render = new Piwik_DataTable_Renderer_Console ($table); $render->setPrefixRow('*'); $rendered = $render->render(); $this->assertEqual($expected,$rendered); } /** * Simple test of the DataTable_Row */ function test_Row() { Zend_Loader::loadClass('Piwik_Timer'); $columns = array('test_column'=> 145, 092582495 => new Piwik_Timer, 'super'=>array('this column has an array value, amazing')); $details = array('logo'=> 'piwik.png', 'super'=>array('this column has an array value, amazing')); $arrayRow = array( Piwik_DataTable_Row::COLUMNS => $columns, Piwik_DataTable_Row::DETAILS => $details, 'fake useless key'=>38959, 43905724897=>'value'); $row = new Piwik_DataTable_Row($arrayRow); $this->assertEqual($row->getColumns(), $columns); $this->assertEqual($row->getDetails(), $details); $this->assertEqual($row->getIdSubDataTable(), null); } /** * Simple test of the DataTable_Row */ function test_sumRow() { Zend_Loader::loadClass('Piwik_Timer'); $columns = array('test_int'=> 145, 'test_float'=> 145.5, 'test_float3'=> 1.5, 'test_stringint'=> "145", "test" => 'string fake', 'super'=>array('this column has an array value, amazing') ); $details = array('logo'=> 'piwik.png', 'super'=>array('this column has an array value, amazing')); $arrayRow = array( Piwik_DataTable_Row::COLUMNS => $columns, Piwik_DataTable_Row::DETAILS => $details, 'fake useless key'=>38959, 43905724897=>'value'); $row1 = new Piwik_DataTable_Row($arrayRow); $columns2 = array('test_int'=> 5, 'test_float'=> 4.5, 'test_float2'=> 14.5, 'test_stringint'=> "5", 0925824 => 'toto', 'super'=>array('this column has geagaean array value, amazing')); $finalRow = new Piwik_DataTable_Row( array(Piwik_DataTable_Row::COLUMNS => $columns2)); $finalRow->sumRow($row1); $columnsWanted = array('test_int'=> 150, 'test_float'=> 150.0, 'test_float2'=> 14.5, 'test_float3'=> 1.5, 'test_stringint'=> "5", //keep the first 'super'=>array('this column has geagaean array value, amazing'), 0925824 => 'toto', ); $rowWanted = new Piwik_DataTable_Row( array(Piwik_DataTable_Row::COLUMNS => $columnsWanted)); // dump($rowWanted); // dump($finalRow); $this->assertTrue( Piwik_DataTable_Row::isEqual($rowWanted, $finalRow)); } /** * Test serialize with an infinite recursion (a row linked to a table in the parent hierarchy) * After 100 recursion must throw an exception */ function test_serializeWithInfiniteRecursion() { $table = new Piwik_DataTable; $table->addRowFromArray(array(Piwik_DataTable_Row::COLUMNS => array( 'visits'=>245,'visitors'=>245), Piwik_DataTable_Row::DATATABLE_ASSOCIATED => $table,)); try { $table->getSerialized(); $this->fail("Exception not raised."); } catch (Exception $expected) { return; } } /** * Test to filter a column with a pattern */ function test_filter_Pattern() { $table = new Piwik_DataTable; $idcol = Piwik_DataTable_Row::COLUMNS; $rows = array( array( $idcol => array('label'=>'google')), array( $idcol => array('label'=>'ask')), array( $idcol => array('label'=>'piwik')), array( $idcol => array('label'=>'yahoo')), array( $idcol => array('label'=>'amazon')), array( $idcol => array('label'=>'238975247578949')), array( $idcol => array('label'=>'Q*(%&*("$&%*(&"$*")"))'))); $table->loadFromArray( $rows ); $expectedtable = clone $table; $expectedtable->deleteRows(array(1,2,4,5,6)); $filter = new Piwik_DataTable_Filter_Pattern($table, 'label', '(oo)'); $this->assertEqual($table->getRows(), $expectedtable->getRows()); } /** * Test to filter a column with a offset, limit */ function test_filter_OffsetLimit() { $table = new Piwik_DataTable; $idcol = Piwik_DataTable_Row::COLUMNS; $rows = array( array( $idcol => array('label'=>'google')),//0 array( $idcol => array('label'=>'ask')),//1 array( $idcol => array('label'=>'piwik')),//2 array( $idcol => array('label'=>'yahoo')),//3 array( $idcol => array('label'=>'amazon')),//4 array( $idcol => array('label'=>'238975247578949')),//5 array( $idcol => array('label'=>'Q*(%&*("$&%*(&"$*")"))'))//6 ); $table->loadFromArray( $rows ); $expectedtable = clone $table; $expectedtable->deleteRows(array(0,1,6)); $filter = new Piwik_DataTable_Filter_Limit($table, 2, 4); $colAfter=$colExpected=array(); foreach($table->getRows() as $row) $colAfter[] = $row->getColumn('label'); foreach($expectedtable->getRows() as $row) $colExpected[] = $row->getColumn('label'); $this->assertEqual(array_values($table->getRows()), array_values($expectedtable->getRows()), implode(", ",array_values($colAfter)) ." does not match the expected ".implode(", ",array_values($colExpected)) ); } /** * Test to filter a column with a offset, limit off bound */ function test_filter_OffsetLimitOffbound() { $table = new Piwik_DataTable; $idcol = Piwik_DataTable_Row::COLUMNS; $rows = array( array( $idcol => array('label'=>'google')),//0 array( $idcol => array('label'=>'ask')),//1 array( $idcol => array('label'=>'piwik')),//2 array( $idcol => array('label'=>'yahoo')),//3 array( $idcol => array('label'=>'amazon')),//4 array( $idcol => array('label'=>'238975247578949')),//5 array( $idcol => array('label'=>'Q*(%&*("$&%*(&"$*")"))'))//6 ); $table->loadFromArray( $rows ); $expectedtable = clone $table; $expectedtable->deleteRows(array(0,1,3,4,5,6)); $filter = new Piwik_DataTable_Filter_Limit($table, 2, 1); $colAfter=$colExpected=array(); foreach($table->getRows() as $row) $colAfter[] = $row->getColumn('label'); foreach($expectedtable->getRows() as $row) $colExpected[] = $row->getColumn('label'); $this->assertEqual(array_values($table->getRows()), array_values($expectedtable->getRows())); } /** * Test to filter a column with a offset, limit 2 */ function test_filter_OffsetLimit2() { $table = new Piwik_DataTable; $idcol = Piwik_DataTable_Row::COLUMNS; $rows = array( array( $idcol => array('label'=>'google')),//0 array( $idcol => array('label'=>'ask')),//1 array( $idcol => array('label'=>'piwik')),//2 array( $idcol => array('label'=>'yahoo')),//3 array( $idcol => array('label'=>'amazon')),//4 array( $idcol => array('label'=>'238975247578949')),//5 array( $idcol => array('label'=>'Q*(%&*("$&%*(&"$*")"))'))//6 ); $table->loadFromArray( $rows ); $expectedtable = clone $table; $filter = new Piwik_DataTable_Filter_Limit($table, 0, 15); $colAfter=$colExpected=array(); foreach($table->getRows() as $row) $colAfter[] = $row->getColumn('label'); foreach($expectedtable->getRows() as $row) $colExpected[] = $row->getColumn('label'); $this->assertEqual(array_values($table->getRows()), array_values($expectedtable->getRows())); } /** * Test to filter a column with a offset, limit 3 */ function test_filter_OffsetLimit3() { $table = new Piwik_DataTable; $idcol = Piwik_DataTable_Row::COLUMNS; $rows = array( array( $idcol => array('label'=>'google')),//0 array( $idcol => array('label'=>'ask')),//1 array( $idcol => array('label'=>'piwik')),//2 array( $idcol => array('label'=>'yahoo')),//3 array( $idcol => array('label'=>'amazon')),//4 array( $idcol => array('label'=>'238975247578949')),//5 array( $idcol => array('label'=>'Q*(%&*("$&%*(&"$*")"))'))//6 ); $table->loadFromArray( $rows ); $expectedtable = new Piwik_DataTable; $filter = new Piwik_DataTable_Filter_Limit($table, 8, 15); $colAfter=$colExpected=array(); foreach($table->getRows() as $row) $colAfter[] = $row->getColumn('label'); foreach($expectedtable->getRows() as $row) $colExpected[] = $row->getColumn('label'); $this->assertEqual(array_values($table->getRows()), array_values($expectedtable->getRows())); } }