[Piwik-svn] r503 - in trunk: lang modules modules/ArchiveProcessing modules/DataTable/Filter modules/LogStats/Generator modules/ViewDataTable plugins/Actions plugins/Referers plugins/VisitTime tests/modules tests/modules/DataTable tests/modules/DataTable/Filter

svnmaster at piwik.org svnmaster at piwik.org
Sun Jun 1 21:16:59 CEST 2008


Author: matt
Date: 2008-06-01 21:16:56 +0200 (Sun, 01 Jun 2008)
New Revision: 503

Added:
   trunk/tests/modules/DataTable/Filter/
   trunk/tests/modules/DataTable/Filter/AddSummaryRow.test.php
   trunk/tests/modules/DataTable/Filter/Limit.test.php
Modified:
   trunk/lang/en.php
   trunk/modules/ArchiveProcessing/Day.php
   trunk/modules/DataTable.php
   trunk/modules/DataTable/Filter/AddSummaryRow.php
   trunk/modules/DataTable/Filter/Limit.php
   trunk/modules/LogStats/Generator/LogStats.php
   trunk/modules/ViewDataTable.php
   trunk/modules/ViewDataTable/GenerateGraphData.php
   trunk/plugins/Actions/Actions.php
   trunk/plugins/Referers/Referers.php
   trunk/plugins/VisitTime/VisitTime.php
   trunk/tests/modules/DataTable.test.php
   trunk/tests/modules/UsersManager.test.php
Log:
- adding a new concept: DataTable can now have a Summary row. A big table can be truncated after 200 rows for example, and the 201st row will the be a summary row which is (label = 'Others', nb_visits = sum(nb_visits), etc.)


Modified: trunk/lang/en.php
===================================================================
--- trunk/lang/en.php	2008-05-29 04:07:59 UTC (rev 502)
+++ trunk/lang/en.php	2008-06-01 19:16:56 UTC (rev 503)
@@ -21,7 +21,8 @@
 	'General_LoadingData' => 'Loading data...',
 	'General_ErrorRequest' => 'Oops… problem during the request, please try again.',
 	'General_Next' => 'Next',
-	'General_Previous' => 'Previous',
+	'General_Previous' => 'Previous',
+	'General_Others' => 'Others',
 	'General_Table' => 'Table',
 	'General_Piechart' => 'Piechart',
 	'General_TagCloud' => 'Tag Cloud',

Modified: trunk/modules/ArchiveProcessing/Day.php
===================================================================
--- trunk/modules/ArchiveProcessing/Day.php	2008-05-29 04:07:59 UTC (rev 502)
+++ trunk/modules/ArchiveProcessing/Day.php	2008-06-01 19:16:56 UTC (rev 503)
@@ -60,7 +60,6 @@
 				 ";
 		$row = $this->db->fetchRow($query, array($this->strDateStart,$this->idsite ) );
 		
-//		echo " archiving ".$this->strDateStart;
 		if($row === false)
 		{
 			return;
@@ -244,10 +243,10 @@
 	 * @param array $array at the given format
 	 * @return array Array with one element: the serialized data table string
 	 */
-	public function getDataTableSerialized( $arrayLevel0 )
+	public function getDataTableSerialized( $array )
 	{
 		$table = new Piwik_DataTable;
-		$table->loadFromArrayLabelIsKey($arrayLevel0);
+		$table->loadFromArrayLabelIsKey($array );
 		$toReturn = $table->getSerialized();
 		return $toReturn;
 	}
@@ -289,9 +288,7 @@
 		$parentTableLevel0 = new Piwik_DataTable;
 		$parentTableLevel0->loadFromArrayLabelIsKey($subArrayLevel1ByKey, $tablesByLabel);
 
-//		echo $parentTableLevel0;
 		$toReturn = $parentTableLevel0->getSerialized();
-
 		return $toReturn;
 	}
 	

Modified: trunk/modules/DataTable/Filter/AddSummaryRow.php
===================================================================
--- trunk/modules/DataTable/Filter/AddSummaryRow.php	2008-05-29 04:07:59 UTC (rev 502)
+++ trunk/modules/DataTable/Filter/AddSummaryRow.php	2008-06-01 19:16:56 UTC (rev 503)
@@ -25,31 +25,44 @@
  * @package Piwik_DataTable
  * @subpackage Piwik_DataTable_Filter 
  */
-
 class Piwik_DataTable_Filter_AddSummaryRow extends Piwik_DataTable_Filter
 {	
-	public $labelSummaryRow = 'Others';
-	
-	public function __construct( $table, $startRowToSummarize )
+	public $labelSummaryRow = Piwik_DataTable::LABEL_SUMMARY_ROW;
+
+	public function __construct( $table, $startRowToSummarize, $columnToSortByBeforeTruncating = Piwik_Archive::INDEX_NB_VISITS )
 	{
 		parent::__construct($table);
 		$this->startRowToSummarize = $startRowToSummarize;
+		$this->columnToSortByBeforeTruncating = $columnToSortByBeforeTruncating;
 		
 		if($table->getRowsCount() > $startRowToSummarize + 1)
 		{
 			$this->filter();
 		}
 	}
-	
+
 	protected function filter()
 	{
-		$copied = clone $this->table;
-		$filter = new Piwik_DataTable_Filter_Limit($copied, $this->startRowToSummarize);
-		$newRow = new Piwik_DataTable_Row_DataTableSummary($copied);
+		$filter = new Piwik_DataTable_Filter_Sort($this->table, $this->columnToSortByBeforeTruncating, 'desc');
+
+		$rows = $this->table->getRows();
+		$count = $this->table->getRowsCount();
+		$newRow = new Piwik_DataTable_Row();
+		for($i = $this->startRowToSummarize; $i < $count; $i++)
+		{
+			if(!isset($rows[$i]))
+			{
+				// case when the last row is a summary row, it is not indexed by $cout but by Piwik_DataTable::ID_SUMMARY_ROW
+				$summaryRow = $this->table->getRowFromId(Piwik_DataTable::ID_SUMMARY_ROW);
+				$newRow->sumRow($summaryRow); 
+			}
+			else
+			{
+				$newRow->sumRow($rows[$i]);
+			}
+		}
 		$newRow->addColumn('label',$this->labelSummaryRow);
 		$filter = new Piwik_DataTable_Filter_Limit($this->table, 0, $this->startRowToSummarize);
-		$this->table->addRow($newRow);
+		$this->table->addSummaryRow($newRow);
 	}
 }
-
-

Modified: trunk/modules/DataTable/Filter/Limit.php
===================================================================
--- trunk/modules/DataTable/Filter/Limit.php	2008-05-29 04:07:59 UTC (rev 502)
+++ trunk/modules/DataTable/Filter/Limit.php	2008-06-01 19:16:56 UTC (rev 503)
@@ -22,7 +22,7 @@
 	 * Filter constructor.
 	 * 
 	 * @param Piwik_DataTable $table
-	 * @param int $offset Starting row 
+	 * @param int $offset Starting row (indexed from 0)
 	 * @param int $limit Number of rows to keep (specify -1 to keep all rows)
 	 */
 	public function __construct( $table, $offset, $limit = null )
@@ -42,16 +42,13 @@
 	protected function filter()
 	{
 		$table = $this->table;
-		
 		$rowsCount = $table->getRowsCount();
 		
-		// we have to delete
-		// - from 0 to offset
-		
-		// at this point the array has offset less elements
-		// - from limit to the end
+		// we delete from 0 to offset
 		$table->deleteRowsOffset( 0, $this->offset );
-		if( $this->limit > 0 )
+		
+		// at this point the array has offset less elements. We delete from limit to the end
+		if( $this->limit >= 0 )
 		{
 			$table->deleteRowsOffset( $this->limit );
 		}

Modified: trunk/modules/DataTable.php
===================================================================
--- trunk/modules/DataTable.php	2008-05-29 04:07:59 UTC (rev 502)
+++ trunk/modules/DataTable.php	2008-06-01 19:16:56 UTC (rev 503)
@@ -180,6 +180,14 @@
 	 */
 	protected $enableRecursiveSort = false;
 	
+	/*
+	 * @var Piwik_DataTable_Row
+	 */
+	protected $summaryRow = null;
+	
+	const ID_SUMMARY_ROW = -1; //TODO check that not possible adding a row with this ID normally
+	const LABEL_SUMMARY_ROW = -1;
+	
 	/**
 	 * Maximum nesting level
 	 * 
@@ -352,6 +360,12 @@
 			$this->rebuildIndex();
 		}
 		
+		if($label === self::LABEL_SUMMARY_ROW
+			&& !is_null($this->summaryRow))
+		{
+			return $this->summaryRow;
+		}
+		
 		$label = (string)$label;
 		if(!isset($this->rowsIndexByLabel[$label]))
 		{
@@ -359,6 +373,26 @@
 		}
 		return $this->rows[$this->rowsIndexByLabel[$label]];
 	}
+
+	/**
+	 * Rebuilds the index used to lookup a row by label
+	 *
+	 * @return void
+	 */
+	private function rebuildIndex()
+	{
+		foreach($this->rows as $id => $row)
+		{
+			$label = $row->getColumn('label');
+		
+			if($label !== false)
+			{
+				$this->rowsIndexByLabel[$label] = $id;
+			}
+		}
+		
+		$this->indexNotUpToDate = false;
+	}
 	
 	/**
 	 * Returns the ith row in the array
@@ -370,6 +404,11 @@
 	{
 		if(!isset($this->rows[$id]))
 		{
+			if($id == self::ID_SUMMARY_ROW
+				&& !is_null($this->summaryRow))
+			{
+				return $this->summaryRow;
+			}
 			return false;
 		}
 		return $this->rows[$id];
@@ -386,6 +425,11 @@
 		$this->indexNotUpToDate = true;
 	}
 	
+	public function addSummaryRow( Piwik_DataTable_Row $row )
+	{
+		$this->summaryRow = $row;
+	}
+	
 	/**
 	 * Returns the dataTable ID
 	 *
@@ -425,7 +469,14 @@
 	 */
 	public function getRows()
 	{
-		return $this->rows;
+		if(is_null($this->summaryRow))
+		{
+			return $this->rows;
+		}
+		else
+		{
+			return $this->rows + array(self::ID_SUMMARY_ROW => $this->summaryRow);
+		}
 	}
 	
 	/**
@@ -435,9 +486,17 @@
 	 */
 	public function getRowsCount()
 	{
-		return count($this->rows);
+		$count = count($this->rows);
+		if(is_null($this->summaryRow))
+		{
+			return $count;
+		}
+		else
+		{
+			return $count + 1;
+		}
 	}
-	
+
 	/**
 	 * Returns the first row of the DataTable
 	 *
@@ -447,13 +506,38 @@
 	{
 		if(count($this->rows) == 0)
 		{
+			if(!is_null($this->summaryRow))
+			{
+				return $this->summaryRow;
+			}
 			return false;
 		}
-		$row = array_slice($this->rows,0,1);
+		// TODO possible improvement => reset() and value()
+		$row = array_slice($this->rows, 0, 1);
 		return $row[0];
 	}
 	
 	/**
+	 * Returns the last row of the DataTable
+	 *
+	 * @return Piwik_DataTable_Row
+	 */
+	public function getLastRow()
+	{
+		if(!is_null($this->summaryRow))
+		{
+			return $this->summaryRow;
+		}
+		
+		if(count($this->rows) == 0)
+		{
+			return false;
+		}
+		$row = array_slice($this->rows, -1);
+		return $row[0];
+	}
+	
+	/**
 	 * Returns the sum of the number of rows of all the subtables 
 	 * 		+ the number of rows in the parent table
 	 * 
@@ -468,7 +552,7 @@
 			{
 				$subTable = Piwik_DataTable_Manager::getInstance()->getTable($idSubTable);
 				$count = $subTable->getRowsCountRecursive();
-				$totalCount+=$count;
+				$totalCount += $count;
 			}
 		}
 		
@@ -487,6 +571,10 @@
 		{
 			$row->deleteColumn($name);
 		}
+		if(!is_null($this->summaryRow))
+		{
+			$this->summaryRow->deleteColumn($name);
+		}
 	}
 	
 	/**
@@ -497,6 +585,11 @@
 	 */
 	public function deleteRow( $id )
 	{
+		if($id === self::ID_SUMMARY_ROW)
+		{
+			$this->summaryRow = null;
+			return;
+		}
 		if(!isset($this->rows[$id]))
 		{
 			throw new Exception("Trying to delete unknown row with idkey = $id");
@@ -513,13 +606,34 @@
 	 */
 	public function deleteRowsOffset( $offset, $limit = null )
 	{
+		if($limit === 0)
+		{
+			return;
+		}
+
+		$count = $this->getRowsCount();
+		if($offset >= $count)
+		{
+			return;
+		}
+
+		// if we delete until the end, we delete the summary row as well
+		if( is_null($limit)
+			|| $limit >= $count )
+		{
+			$this->summaryRow = null;
+		}
+
 		if(is_null($limit))
 		{
-			$limit = count($this->rows);
+			array_splice($this->rows, $offset);
 		}
-		array_splice($this->rows, $offset, $limit);
+		else
+		{
+			array_splice($this->rows, $offset, $limit);
+		}
 	}
-	
+
 	/**
 	 * Deletes the rows from the list of rows ID 
 	 *
@@ -562,8 +676,8 @@
 		$table1->rebuildIndex();
 		$table2->rebuildIndex();
 		
-		$countrows1 = count($rows1);
-		$countrows2 = count($rows2);
+		$countrows1 = $table1->getRowsCount();
+		$countrows2 = $table2->getRowsCount();
 		
 		if($countrows1 != $countrows2)
 		{
@@ -635,27 +749,28 @@
 			{
 				$subTable = Piwik_DataTable_Manager::getInstance()->getTable($idSubTable);
 				$depth++;
-				$serialized = $subTable->getSerialized();
+				$aSerializedDataTable = $aSerializedDataTable + $subTable->getSerialized();
 				$depth--;
-				
-				$aSerializedDataTable = $aSerializedDataTable + $serialized;
 			}
 		}
 		// we load the current Id of the DataTable
 		$forcedId = $this->getId();
 		
 		// if the datatable is the parent we force the Id at 0 (this is part of the specification)
-		if($depth==0)
+		if($depth == 0)
 		{
 			$forcedId = 0;
 		}
 		
+		//TODO limit temporary here, will be set on a per plugin basis
+		$filter = new Piwik_DataTable_Filter_AddSummaryRow($this, 500);
+		
 		// we then serialize the rows and store them in the serialized dataTable
-		$aSerializedDataTable[$forcedId] = serialize($this->rows);
+		$aSerializedDataTable[$forcedId] = serialize($this->rows + array( self::ID_SUMMARY_ROW => $this->summaryRow));
 		
 		return $aSerializedDataTable;
 	}
-	 
+	
 	 /**
 	  * Load a serialized string of a datatable.
 	  * 
@@ -697,14 +812,20 @@
 	 */
 	public function loadFromArray( $array )
 	{
-		foreach($array as $row)
+		foreach($array as $id => $row)
 		{
 			if(is_array($row))
 			{
 				$row = new Piwik_DataTable_Row($row);
 			}
-			
-			$this->addRow($row);
+			if($id == self::ID_SUMMARY_ROW)
+			{
+				$this->summaryRow = $row;
+			}
+			else 
+			{
+				$this->addRow($row);
+			}
 		}
 	}
 	
@@ -863,25 +984,6 @@
 	}
 	
 
-	/**
-	 * Rebuilds the index used to lookup a row by label
-	 *
-	 * @return void
-	 */
-	protected function rebuildIndex()
-	{
-		foreach($this->getRows() as $id => $row)
-		{
-			$label = $row->getColumn('label');
-		
-			if($label !== false)
-			{
-				$this->rowsIndexByLabel[$label] = $id;
-			}
-		}
-		
-		$this->indexNotUpToDate = false;
-	}
 	
 	/**
 	 * At destruction we try to free memory

Modified: trunk/modules/LogStats/Generator/LogStats.php
===================================================================
--- trunk/modules/LogStats/Generator/LogStats.php	2008-05-29 04:07:59 UTC (rev 502)
+++ trunk/modules/LogStats/Generator/LogStats.php	2008-06-01 19:16:56 UTC (rev 503)
@@ -28,7 +28,6 @@
 	 */
 	protected function sendHeader($header)
 	{
-	//	header($header);
 	}
 	
 	/**
@@ -38,7 +37,6 @@
 	 */
 	protected function endProcess()
 	{
-		self::disconnectDb();
 	}
 	
 	/**

Modified: trunk/modules/ViewDataTable/GenerateGraphData.php
===================================================================
--- trunk/modules/ViewDataTable/GenerateGraphData.php	2008-05-29 04:07:59 UTC (rev 502)
+++ trunk/modules/ViewDataTable/GenerateGraphData.php	2008-06-01 19:16:56 UTC (rev 503)
@@ -82,7 +82,8 @@
 		
 		// we load the data with the filters applied
 		$this->loadDataTableFromAPI();
-		$this->dataTable->queueFilter('Piwik_DataTable_Filter_AddSummaryRow',array($this->getGraphLimit()));
+		$offsetStartSummary = $this->getGraphLimit() - 1;
+		$this->dataTable->queueFilter('Piwik_DataTable_Filter_AddSummaryRow',array($offsetStartSummary));
 		$this->dataAvailable = $this->dataTable->getRowsCount() != 0;
 		
 		if(!$this->dataAvailable)

Modified: trunk/modules/ViewDataTable.php
===================================================================
--- trunk/modules/ViewDataTable.php	2008-05-29 04:07:59 UTC (rev 502)
+++ trunk/modules/ViewDataTable.php	2008-06-01 19:16:56 UTC (rev 503)
@@ -477,8 +477,6 @@
 			}
 		}
 		
-//		var_dump($javascriptVariablesToSet);exit;
-		// See 
 		foreach($_GET as $name => $value)
 		{
 			try{
@@ -501,8 +499,7 @@
 				$javascriptVariablesToSet[$name] = $value;
 			}
 		}
-		
-		
+				
 		$javascriptVariablesToSet['module'] = $this->currentControllerName;
 		$javascriptVariablesToSet['action'] = $this->currentControllerAction;
 		

Modified: trunk/plugins/Actions/Actions.php
===================================================================
--- trunk/plugins/Actions/Actions.php	2008-05-29 04:07:59 UTC (rev 502)
+++ trunk/plugins/Actions/Actions.php	2008-06-01 19:16:56 UTC (rev 503)
@@ -189,22 +189,18 @@
 		$query = $archiveProcessing->db->query($query, array( $archiveProcessing->strDateStart, $archiveProcessing->idsite ));
 				
 		$modified = $this->updateActionsTableWithRowQuery($query);
-		
-		
+
 		$dataTable = Piwik_ArchiveProcessing_Day::generateDataTable($this->actionsTablesByType[Piwik_LogStats_Action::TYPE_ACTION]);
 		$s = $dataTable->getSerialized();
 		$record = new Piwik_ArchiveProcessing_Record_BlobArray('Actions_actions', $s);
 		
-		
 		$dataTable = Piwik_ArchiveProcessing_Day::generateDataTable($this->actionsTablesByType[Piwik_LogStats_Action::TYPE_DOWNLOAD]);
 		$s = $dataTable->getSerialized();
 		$record = new Piwik_ArchiveProcessing_Record_BlobArray('Actions_downloads', $s);
 		
-		
 		$dataTable = Piwik_ArchiveProcessing_Day::generateDataTable($this->actionsTablesByType[Piwik_LogStats_Action::TYPE_OUTLINK]);
 		$s = $dataTable->getSerialized();
 		$record = new Piwik_ArchiveProcessing_Record_BlobArray('Actions_outlink', $s);
-//		echo $dataTable;
 		unset($this->actionsTablesByType);
 	}
 

Modified: trunk/plugins/Referers/Referers.php
===================================================================
--- trunk/plugins/Referers/Referers.php	2008-05-29 04:07:59 UTC (rev 502)
+++ trunk/plugins/Referers/Referers.php	2008-06-01 19:16:56 UTC (rev 503)
@@ -106,7 +106,7 @@
 								'nameTableToUse' => 'Referers_urlByPartner',
 							),
 		);
-//		var_dump($nameToCount);exit;
+
 		foreach($mappingFromArchiveName as $name => $infoMapping)
 		{
 			$typeCountToUse = $infoMapping['typeCountToUse'];
@@ -128,8 +128,8 @@
 													$countValue
 												);
 		}
-	}	
-	
+	}
+
 	/** 
 	 * 
 	 */
@@ -270,7 +270,6 @@
 		$numberOfDistinctSearchEngines = count($keywordBySearchEngine);
 		$numberOfDistinctKeywords = count($searchEngineByKeyword);
 		
-//		var_dump($interestByCampaign);exit;
 		$numberOfDistinctCampaigns = count($interestByCampaign); //TODO bug here
 		$numberOfDistinctWebsites = count($interestByWebsite[Piwik_Common::REFERER_TYPE_WEBSITE]);
 		$numberOfDistinctWebsitesUrls = count($distinctUrls[Piwik_Common::REFERER_TYPE_WEBSITE]);

Modified: trunk/plugins/VisitTime/VisitTime.php
===================================================================
--- trunk/plugins/VisitTime/VisitTime.php	2008-05-29 04:07:59 UTC (rev 502)
+++ trunk/plugins/VisitTime/VisitTime.php	2008-06-01 19:16:56 UTC (rev 503)
@@ -69,14 +69,12 @@
 		$tableLocalTime = $archiveProcessing->getDataTableInterestForLabel($labelSQL);
 		$this->makeSureAllHoursAreSet($tableLocalTime);
 		$record = new Piwik_ArchiveProcessing_Record_BlobArray($recordName, $tableLocalTime->getSerialized());
-//		echo $tableLocalTime;
 		
 		$recordName = 'VisitTime_serverTime';
 		$labelSQL = "HOUR(visit_first_action_time)";
 		$tableServerTime = $archiveProcessing->getDataTableInterestForLabel($labelSQL);
 		$this->makeSureAllHoursAreSet($tableServerTime);
 		$record = new Piwik_ArchiveProcessing_Record_BlobArray($recordName, $tableServerTime->getSerialized());
-//		echo $tableServerTime;
 	}
 	
 	private function makeSureAllHoursAreSet($table)

Added: trunk/tests/modules/DataTable/Filter/AddSummaryRow.test.php
===================================================================
--- trunk/tests/modules/DataTable/Filter/AddSummaryRow.test.php	                        (rev 0)
+++ trunk/tests/modules/DataTable/Filter/AddSummaryRow.test.php	2008-06-01 19:16:56 UTC (rev 503)
@@ -0,0 +1,142 @@
+<?php
+if(!defined("PATH_TEST_TO_ROOT")) {
+	define('PATH_TEST_TO_ROOT', '../../../..');
+}
+if(!defined('CONFIG_TEST_INCLUDED'))
+{
+	require_once "../../../../tests/config_test.php";
+}
+require_once 'DataTable.php';
+
+class Test_Piwik_DataTable_Filter_AddSummaryRow extends UnitTestCase
+{	
+	public function test_offsetIsCount_summaryRowShouldBeTheRow()
+	{
+		$table = $this->getDataTableCount5();
+		$filter = new Piwik_DataTable_Filter_AddSummaryRow($table, 5);
+		$this->assertEqual($table->getRowsCount(), 5);
+		$this->assertTrue(Piwik_DataTable_Row::isEqual($table->getLastRow(), $this->getRow4()));
+	}
+	
+	public function test_offsetIsLessThanCount_SummaryRowShouldBeTheSum()
+	{
+		$table = $this->getDataTableCount5();
+		$filter = new Piwik_DataTable_Filter_AddSummaryRow($table, 2);
+		$this->assertEqual($table->getRowsCount(), 3);
+		$expectedRow = new Piwik_DataTable_Row(array( Piwik_DataTable_Row::COLUMNS => array('label'=>Piwik_DataTable::LABEL_SUMMARY_ROW, 'nb' => 111)));
+		$this->assertTrue(Piwik_DataTable_Row::isEqual($table->getLastRow(), $expectedRow));
+	}
+	
+	public function test_offsetIsMoreThanCount_shouldNotAddSummaryRow()
+	{
+		$table = $this->getDataTableCount5();
+		$filter = new Piwik_DataTable_Filter_AddSummaryRow($table, 6);
+		$this->assertEqual($table->getRowsCount(), 5);
+		$this->assertTrue(Piwik_DataTable_Row::isEqual($table->getLastRow(), $this->getRow4()));
+	}
+	
+	public function test_whenThereIsAlreadyASummaryRow_shouldReplaceTheSummaryRow()
+	{
+		$table = $this->getDataTableCount5();
+		$filter1 = new Piwik_DataTable_Filter_AddSummaryRow($table, 3);
+		$filter2 = new Piwik_DataTable_Filter_AddSummaryRow($table, 2);
+		$this->assertEqual($table->getRowsCount(), 3);
+		$expectedRow = new Piwik_DataTable_Row(array( Piwik_DataTable_Row::COLUMNS => array('label'=>Piwik_DataTable::LABEL_SUMMARY_ROW, 'nb' => 111)));
+		$this->assertTrue(Piwik_DataTable_Row::isEqual($table->getLastRow(), $expectedRow));
+	}
+	
+	public function test_sumTablesWithSummaryRow_shouldSumTheSummaryRow()
+	{
+		// row0, row1, row2, rowSummary1
+		$table1 = $this->getDataTableCount5();
+		$filter = new Piwik_DataTable_Filter_AddSummaryRow($table1, 3);
+		
+		// row0, row1, rowSummary2
+		$table2 = $this->getDataTableCount5();
+		$filter = new Piwik_DataTable_Filter_AddSummaryRow($table2, 2);
+		
+		// we expect row0+row0, row1+row1, row2, rowSummary1+rowSummary2
+		$expectedTable = new Piwik_DataTable;
+		$expectedTable->addRow( new Piwik_DataTable_Row(array( Piwik_DataTable_Row::COLUMNS => array('label'=>'amazon', 'nb' => 20000) )));
+		$expectedTable->addRow( new Piwik_DataTable_Row(array( Piwik_DataTable_Row::COLUMNS => array('label'=>'yahoo', 'nb' => 2000) )));
+		$expectedTable->addRow( new Piwik_DataTable_Row(array( Piwik_DataTable_Row::COLUMNS => array('label'=>'piwik', 'nb' => 100) )));
+		$expectedTable->addRow( new Piwik_DataTable_Row(array( Piwik_DataTable_Row::COLUMNS => array('label'=>Piwik_DataTable::LABEL_SUMMARY_ROW, 'nb' => 122) )));
+		
+		$table1->addDataTable($table2);
+		$this->assertTrue(Piwik_DataTable::isEqual($expectedTable, $table1));
+	}
+	
+	public function test_addOneTableWithSummaryRow()
+	{
+		// row0, row1, row2, rowSummary1
+		$table1 = $this->getDataTableCount5();
+		$filter = new Piwik_DataTable_Filter_AddSummaryRow($table1, 3);
+		
+		// row0, row1, row2, row3, row4
+		$table2 = $this->getDataTableCount5();
+		
+		// we expect row0+row0, row1+row1, row2+row2, row3, row4, rowSummary1
+		$expectedTable = new Piwik_DataTable;
+		$expectedTable->addRow( new Piwik_DataTable_Row(array( Piwik_DataTable_Row::COLUMNS => array('label'=>'amazon', 'nb' => 20000) )));
+		$expectedTable->addRow( new Piwik_DataTable_Row(array( Piwik_DataTable_Row::COLUMNS => array('label'=>'yahoo', 'nb' => 2000) )));
+		$expectedTable->addRow( new Piwik_DataTable_Row(array( Piwik_DataTable_Row::COLUMNS => array('label'=>'piwik', 'nb' => 200) )));
+		$expectedTable->addRow( $this->getRow3());
+		$expectedTable->addRow( $this->getRow4());
+		$expectedTable->addRow( new Piwik_DataTable_Row(array( Piwik_DataTable_Row::COLUMNS => array('label'=>Piwik_DataTable::LABEL_SUMMARY_ROW, 'nb' => 11))));
+		
+		$table1->addDataTable($table2);
+		$this->assertTrue(Piwik_DataTable::isEqual($expectedTable, $table1));
+		
+	}
+	
+	public function test_whenRowsInRandomOrderButSortSpecified_shouldComputeSummaryRowAfterSort()
+	{
+		$table = new Piwik_DataTable;
+		$table->addRow( $this->getRow3() );
+		$table->addRow( $this->getRow2() );
+		$table->addRow( $this->getRow4() );
+		$table->addRow( $this->getRow1() );
+		$table->addRow( $this->getRow0() );
+		
+		$filter = new Piwik_DataTable_Filter_AddSummaryRow($table, 2, $columnToSortBy = 'nb');
+		$this->assertEqual($table->getRowsCount(), 3);
+		$expectedRow = new Piwik_DataTable_Row(array( Piwik_DataTable_Row::COLUMNS => array('label'=>Piwik_DataTable::LABEL_SUMMARY_ROW, 'nb' => 111)));
+		$this->assertTrue(Piwik_DataTable_Row::isEqual($table->getLastRow(), $expectedRow));
+	}
+	
+	/**
+	 * Returns table used for the tests
+	 *
+	 * @return Piwik_DataTable
+	 */
+	protected function getDataTableCount5()
+	{
+		$table = new Piwik_DataTable;
+		$table->addRow( $this->getRow0() );
+		$table->addRow( $this->getRow1() );
+		$table->addRow( $this->getRow2() );
+		$table->addRow( $this->getRow3() );
+		$table->addRow( $this->getRow4() );
+	  	return $table;
+	}
+	protected function getRow0()
+	{
+		return new Piwik_DataTable_Row(array( Piwik_DataTable_Row::COLUMNS => array('label'=>'amazon', 'nb' => 10000)));
+	}
+	protected function getRow1()
+	{
+		return new Piwik_DataTable_Row(array( Piwik_DataTable_Row::COLUMNS => array('label'=>'yahoo', 'nb' => 1000)));
+	}
+	protected function getRow2()
+	{
+		return new Piwik_DataTable_Row(array( Piwik_DataTable_Row::COLUMNS => array('label'=>'piwik', 'nb' => 100)));
+	}
+	protected function getRow3()
+	{
+		return new Piwik_DataTable_Row(array( Piwik_DataTable_Row::COLUMNS => array('label'=>'ask', 'nb' => 10)));
+	}
+	protected function getRow4()
+	{
+		return new Piwik_DataTable_Row(array( Piwik_DataTable_Row::COLUMNS => array('label'=>'google', 'nb' => 1)));
+	}
+}
\ No newline at end of file

Added: trunk/tests/modules/DataTable/Filter/Limit.test.php
===================================================================
--- trunk/tests/modules/DataTable/Filter/Limit.test.php	                        (rev 0)
+++ trunk/tests/modules/DataTable/Filter/Limit.test.php	2008-06-01 19:16:56 UTC (rev 503)
@@ -0,0 +1,142 @@
+<?php
+if(!defined("PATH_TEST_TO_ROOT")) {
+	define('PATH_TEST_TO_ROOT', '../../../..');
+}
+if(!defined('CONFIG_TEST_INCLUDED'))
+{
+	require_once "../../../../tests/config_test.php";
+}
+require_once 'DataTable.php';
+
+class Test_Piwik_DataTable_Filter_Limit extends UnitTestCase
+{	
+	/**
+	 * Returns table used for the tests
+	 *
+	 * @return Piwik_DataTable
+	 */
+	protected function getDataTableCount10()
+	{
+		$table = new Piwik_DataTable;
+		$idcol = Piwik_DataTable_Row::COLUMNS;
+	 	$rows = array(
+	  		array( $idcol => array('label'=>'google', 	'idRow' => 0)),
+	  		array( $idcol => array('label'=>'ask', 		'idRow' => 1)),
+	  		array( $idcol => array('label'=>'piwik', 	'idRow' => 2)),
+	  		array( $idcol => array('label'=>'yahoo', 	'idRow' => 3)),
+	  		array( $idcol => array('label'=>'amazon', 	'idRow' => 4)),
+	  		array( $idcol => array('label'=>'238949', 	'idRow' => 5)),
+	  		array( $idcol => array('label'=>'test', 	'idRow' => 6)),
+	  		array( $idcol => array('label'=>'amazing', 	'idRow' => 7)),
+	  		array( $idcol => array('label'=>'great', 	'idRow' => 8)),
+	  		Piwik_DataTable::ID_SUMMARY_ROW => array( $idcol => array('label'=>'summary row',	'idRow' => 9)),
+	  		);
+	  	$table->loadFromArray( $rows );
+	  	return $table;
+	}
+	
+	public function test_normal()
+	{
+		$offset = 2;
+		$limit = 3;
+		$table = $this->getDataTableCount10();
+		$filter = new Piwik_DataTable_Filter_Limit($table, $offset, $limit);
+		$this->assertEqual($table->getRowsCount(), 3);
+		$this->assertEqual($table->getFirstRow()->getColumn('idRow'), 2);
+		$this->assertEqual($table->getLastRow()->getColumn('idRow'), 4);
+	}
+	
+	public function test_limitLessThanCount_shouldReturnCountLimit()
+	{
+		$offset = 2;
+		$limit = 7;
+		$table = $this->getDataTableCount10();
+		$filter = new Piwik_DataTable_Filter_Limit($table, $offset, $limit);
+		$this->assertEqual($table->getRowsCount(), 7);
+		$this->assertEqual($table->getFirstRow()->getColumn('idRow'), 2);
+		$this->assertEqual($table->getLastRow()->getColumn('idRow'), 8);
+	}
+	
+	public function test_limitIsCount_shouldNotDeleteAnything()
+	{
+		$offset = 0;
+		$limit = 10;
+		$table = $this->getDataTableCount10();
+		$filter = new Piwik_DataTable_Filter_Limit($table, $offset, $limit);
+		$this->assertEqual($table->getRowsCount(), 10);
+		$this->assertEqual($table->getFirstRow()->getColumn('idRow'), 0);
+		$this->assertEqual($table->getLastRow()->getColumn('idRow'), 9);
+	}
+	
+	public function test_limitGreaterThanCount_shouldReturnCountUntilCount()
+	{
+		$offset = 5;
+		$limit = 20;
+		$table = $this->getDataTableCount10();
+		$filter = new Piwik_DataTable_Filter_Limit($table, $offset, $limit);
+		$this->assertEqual($table->getRowsCount(), 5);
+		$this->assertEqual($table->getFirstRow()->getColumn('idRow'), 5);
+		$this->assertEqual($table->getLastRow()->getColumn('idRow'), 9);
+	}
+	
+	public function test_limitIsNull_shouldReturnCountIsOffset()
+	{
+		$offset = 1;
+		$table = $this->getDataTableCount10();
+		$filter = new Piwik_DataTable_Filter_Limit($table, $offset);
+		$this->assertEqual($table->getRowsCount(), 9);
+		$this->assertEqual($table->getFirstRow()->getColumn('idRow'), 1);
+		$this->assertEqual($table->getLastRow()->getColumn('idRow'), 9);
+	}
+	
+	public function test_offsetJustBeforeSummaryRow_shouldJustReturnSummaryRow()
+	{
+		$offset = 9;
+		$limit = 1;
+		$table = $this->getDataTableCount10();
+		$filter = new Piwik_DataTable_Filter_Limit($table, $offset, $limit);
+		$this->assertEqual($table->getRowsCount(), 1);
+		$this->assertEqual($table->getFirstRow()->getColumn('idRow'), 9);
+		$this->assertEqual($table->getLastRow()->getColumn('idRow'), 9);
+	}
+	
+	public function test_offsetJustBeforeSummaryRowWithBigLimit_shouldJustReturnSummaryRow()
+	{
+		$offset = 9;
+		$limit = 100;
+		$table = $this->getDataTableCount10();
+		$filter = new Piwik_DataTable_Filter_Limit($table, $offset, $limit);
+		$this->assertEqual($table->getRowsCount(), 1);
+		$this->assertEqual($table->getFirstRow()->getColumn('idRow'), 9);
+		$this->assertEqual($table->getLastRow()->getColumn('idRow'), 9);
+	}
+
+	public function test_offsetBeforeSummaryRow_shouldJustReturnRowAndSummaryRow()
+	{
+		$offset = 8;
+		$limit = 3;
+		$table = $this->getDataTableCount10();
+		$filter = new Piwik_DataTable_Filter_Limit($table, $offset, $limit);
+		$this->assertEqual($table->getRowsCount(), 2);
+		$this->assertEqual($table->getFirstRow()->getColumn('idRow'), 8);
+		$this->assertEqual($table->getLastRow()->getColumn('idRow'), 9);
+	}
+	
+	public function test_offsetGreaterThanCount_shouldReturnEmptyTable()
+	{
+		$offset = 10;
+		$limit = 10;
+		$table = $this->getDataTableCount10();
+		$filter = new Piwik_DataTable_Filter_Limit($table, $offset, $limit);
+		$this->assertEqual($table->getRowsCount(), 0);
+	}
+	
+	public function test_limitIsZero_shouldReturnEmptyTable()
+	{
+		$offset = 0;
+		$limit = 0;
+		$table = $this->getDataTableCount10();
+		$filter = new Piwik_DataTable_Filter_Limit($table, $offset, $limit);
+		$this->assertEqual($table->getRowsCount(), 0);
+	}
+}
\ No newline at end of file

Modified: trunk/tests/modules/DataTable.test.php
===================================================================
--- trunk/tests/modules/DataTable.test.php	2008-05-29 04:07:59 UTC (rev 502)
+++ trunk/tests/modules/DataTable.test.php	2008-06-01 19:16:56 UTC (rev 503)
@@ -118,7 +118,6 @@
 		
 		$idsubsubtable = $subsubtable->getId();
 	  	
-	  	
 	  	$serialized = ($table->getSerialized());
 	  	
 		$this->assertEqual(array_keys($serialized), array($idsubsubtable,$idsubtable,0));
@@ -504,52 +503,7 @@
 	  	$this->assertEqual(array_values($table->getRows()), array_values($expectedtable->getRows()));
 	 }
 	 
-
-	/**
-	 * Test to summarize a given datatable's rows from N to M
-	 * Keep the row 0 to N-1
-	 * Add a new row summarizing the others
-	 */
-	 function test_filter_AddSummaryRow()
-	 {
-	 	$table = new Piwik_DataTable;
-	 	$idcol = Piwik_DataTable_Row::COLUMNS;
-	  	$rows = array(
-	  		array( $idcol => array('label'=>'google', 'hits' => 100000)),//0
-	  		array( $idcol => array('label'=>'ask', 'hits' => 10000)),//1
-	  		array( $idcol => array('label'=>'piwik', 'hits' => 1000)),//2
-	  		array( $idcol => array('label'=>'yahoo', 'hits' => 100)),//3
-	  		array( $idcol => array('label'=>'amazon', 'hits' => 10)),//4
-	  		array( $idcol => array('label'=>'238975247578949', 'hits' => 1)),//5
-	  		);
-	  	$table->loadFromArray( $rows );
-	  	
-	  	$startRow = 5;
-		$filter = new Piwik_DataTable_Filter_AddSummaryRow($table, $startRow);
-	  	$tableExpected = clone $table;
-	  	$this->assertTrue( Piwik_DataTable::isEqual($table, $tableExpected) );
-//	  	echo $table;
-//	  	echo $tableExpected;
-  		
-  		$startRow = 3;
-	  	$expected = array(
-	  		array( $idcol => array('label'=>'google', 'hits' => 100000)),//0
-	  		array( $idcol => array('label'=>'ask', 'hits' => 10000)),//1
-	  		array( $idcol => array('label'=>'piwik', 'hits' => 1000)),//2
-	  		array( $idcol => array('label'=>'Others', 'hits' => 111)),//3
-	  		);	  	
-		$filter = new Piwik_DataTable_Filter_AddSummaryRow($table, $startRow);
-
-	  	$tableExpected = new Piwik_DataTable;
-	  	$tableExpected->loadFromArray( $expected );
-//	  	echo $table;
-//	  	echo $tableExpected;
-	  	$this->assertTrue( Piwik_DataTable::isEqual($table, $tableExpected) );
-	  	
-	  	
-	}
 	
-	
 	/**
 	 * Test to sort by label
 	 */
@@ -739,18 +693,8 @@
      */
     public function test_addSimpleNoRowTable2()
 	{
-	 	$idcol = Piwik_DataTable_Row::COLUMNS;
-		
-		$rows = array(
-	  		array( $idcol => array('label'=>'google', 'visits' => 1)),
-	  		array( $idcol => array('label'=>'ask', 'visits' => 2)),
-	  		array( $idcol => array('label'=>'123', 'visits' => 2)),
-  		);	  	
-	 	$table = new Piwik_DataTable;
-	  	$table->loadFromArray( $rows );
-	  
+		$table = $this->getDataTable1ForTest();
 	 	$tableEmpty = new Piwik_DataTable;
-	  	
 	  	$tableAfter = clone $table;
 	  	$tableAfter->addDataTable($tableEmpty);
 	  	$this->assertTrue( Piwik_DataTable::isEqual($table, $tableAfter) );
@@ -761,28 +705,32 @@
      */
     public function test_addSimpleNoRowTable1()
 	{ 	
-		$idcol = Piwik_DataTable_Row::COLUMNS;
-		
-		$rows = array(
-	  		array( $idcol => array('label'=>'google', 'visits' => 1)),
-	  		array( $idcol => array('label'=>'ask', 'visits' => 2)),
-	  		array( $idcol => array('label'=>'123', 'visits' => 2)),
-  		);	  	
-	 	$table = new Piwik_DataTable;
-	  	$table->loadFromArray( $rows );
-	  
+		$table = $this->getDataTable1ForTest();
 	 	$tableEmpty = new Piwik_DataTable;
-	  	
 	  	$tableAfter = clone $tableEmpty;
 	  	$tableEmpty->addDataTable($table);
 	  	$this->assertTrue( Piwik_DataTable::isEqual($tableEmpty, $table) );
 	}
-	
+
 	/**
      * add to the datatable another datatable// they don't have any row in common
      */
     public function test_addSimpleNoCommonRow()
 	{
+		$table1 = $this->getDataTable1ForTest();
+		$table2 = $this->getDataTable2ForTest();
+	  
+	  	$table1->addDataTable($table2);
+	  
+	  	$rowsExpected = array_merge($this->getRowsDataTable1ForTest(),$this->getRowsDataTable2ForTest());
+	  	$tableExpected = new Piwik_DataTable;
+	  	$tableExpected->loadFromArray( $rowsExpected );
+	  	
+	  	$this->assertTrue( Piwik_DataTable::isEqual($table1, $tableExpected) );
+	}
+	
+	protected function getDataTable1ForTest()
+	{
 		$idcol = Piwik_DataTable_Row::COLUMNS;
 		
 		$rows = array(
@@ -792,25 +740,37 @@
   		);	  	
 	 	$table = new Piwik_DataTable;
 	  	$table->loadFromArray( $rows );
-	  	
-	  	
-		$rows2 = array(
-	  		array( $idcol => array('label'=>'test', 'visits' => 1)),
-	  		array( $idcol => array('label'=>' google ', 'visits' => 3)),
-	  		array( $idcol => array('label'=>'123a', 'visits' => 2)),
-  		);	  	
-	 	$table2 = new Piwik_DataTable;
-	  	$table2->loadFromArray( $rows2 );
-	  
-	  	$table->addDataTable($table2);
-	  
-	  	$rowsExpected = array_merge($rows,$rows2);
-	  	$tableExpected = new Piwik_DataTable;
-	  	$tableExpected->loadFromArray( $rowsExpected );
-	  	
-	  	$this->assertTrue( Piwik_DataTable::isEqual($table, $tableExpected) );
+	  	return $table;
 	}
+
+	protected function getDataTable2ForTest()
+	{
+		$rows = $this->getRowsDataTable2ForTest();	
+	 	$table = new Piwik_DataTable;
+	  	$table->loadFromArray( $rows );
+	  	return $table;
+	}
+
+	protected function getRowsDataTable2ForTest()
+	{
+		$rows = array(
+	  		array( Piwik_DataTable_Row::COLUMNS => array('label'=>'test', 'visits' => 1)),
+	  		array( Piwik_DataTable_Row::COLUMNS => array('label'=>' google ', 'visits' => 3)),
+	  		array( Piwik_DataTable_Row::COLUMNS => array('label'=>'123a', 'visits' => 2)),
+  		);
+  		return $rows;	  	
+	}
 	
+	protected function getRowsDataTable1ForTest()
+	{
+		$rows = array(
+	  		array( Piwik_DataTable_Row::COLUMNS => array('label'=>'google', 'visits' => 1)),
+	  		array( Piwik_DataTable_Row::COLUMNS => array('label'=>'ask', 'visits' => 2)),
+	  		array( Piwik_DataTable_Row::COLUMNS => array('label'=>'123', 'visits' => 2)),
+  		);
+  		return $rows;	  	
+	}
+	
 	/**
      * add 2 datatable with some common rows 
      */
@@ -941,4 +901,6 @@
 	  	
 	  	$this->assertTrue( Piwik_DataTable::isEqual($table, $tableExpected) );
 	}
+	
+
 }
\ No newline at end of file

Modified: trunk/tests/modules/UsersManager.test.php
===================================================================
--- trunk/tests/modules/UsersManager.test.php	2008-05-29 04:07:59 UTC (rev 502)
+++ trunk/tests/modules/UsersManager.test.php	2008-06-01 19:16:56 UTC (rev 503)
@@ -655,7 +655,7 @@
     function test_setUserAccess_multipleCallDistinctAccessMultipleUser()
     {
     	Piwik_UsersManager_API::addUser("user1", "geqgegagae", "tegst at tesgt.com", "alias");
-    	Piwik_UsersManager_API::addUser("user2", "geqgegagae", "tegst at tesgt.com", "alias");
+    	Piwik_UsersManager_API::addUser("user2", "geqgegagae", "tegst2 at tesgt.com", "alias");
     	$id1=Piwik_SitesManager_API::addSite("test1",array("http://piwik.net","http://piwik.com/test/"));
 		$id2=Piwik_SitesManager_API::addSite("test2",array("http://piwik.net","http://piwik.com/test/"));
 		$id3=Piwik_SitesManager_API::addSite("test2",array("http://piwik.net","http://piwik.com/test/"));



More information about the Piwik-svn mailing list