1<?php
2// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
3//
4// All Rights Reserved. See copyright.txt for details and a complete list of authors.
5// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
6// $Id$
7
8class Reports_CacheTest extends TikiDatabaseTestCase
9{
10	protected $obj;
11
12	protected function setUp()
13	{
14		$db = TikiDb::get();
15		$dt = new DateTime();
16		$dt->setTimezone(new DateTimeZone('UTC'));
17		$dt->setTimestamp('1326990210');
18		$this->obj = new Reports_Cache($db, $dt);
19
20		parent::setUp();
21	}
22
23	public function getDataSet()
24	{
25		return $this->createMySQLXMLDataSet(__DIR__ . '/fixtures/reports_cache_dataset.xml');
26	}
27
28	public function testDelete_shouldDeleteAllEntriesForAUser()
29	{
30		$expectedTable = $this->createMySQLXmlDataSet(__DIR__ . '/fixtures/reports_cache_dataset_delete.xml')
31			->getTable('tiki_user_reports_cache');
32
33		$this->obj->delete('admin');
34
35		$queryTable = $this->getConnection()->createQueryTable('tiki_user_reports_cache', 'SELECT * FROM tiki_user_reports_cache');
36
37		$this->assertTablesEqual($expectedTable, $queryTable);
38	}
39
40	public function testGet_shouldReturnEntriesForGivenUser()
41	{
42		$expectedResult = [['user' => 'test', 'event' => 'wiki_page_changed',
43			'data' =>
44				[
45					'event' => 'wiki_page_changed', 'pageName' => 'test', 'object' => 'test',
46					'editUser' => 'test', 'editComment' => '', 'oldVer' => 2
47				],
48			'time' => '2012-01-19 16:23:30'
49		]];
50
51		$entries = $this->obj->get('test');
52
53		$this->assertEquals($expectedResult, $entries);
54	}
55
56	/**
57	 * @group marked-as-skipped
58	 */
59	public function testAdd_shouldAddInformationAboutChangedObjectToCache()
60	{
61		$this->markTestSkipped("As of 2013-09-30, this test is broken. Skipping it for now.");
62
63		$expectedTable = $this->createMySQLXmlDataSet(__DIR__ . '/fixtures/reports_cache_dataset_add.xml')
64			->getTable('tiki_user_reports_cache');
65
66		$users = ['admin', 'test'];
67
68		$watches = [
69			['user' => 'admin'],
70			['user' => 'test'],
71			['user' => 'notUsingPeriodicReports']
72		];
73
74		$expectedResult = array_slice($watches, 2, 1, true);
75
76		$cacheData = ['event' => 'wiki_page_changed'];
77
78		$this->obj->add($watches, $cacheData, $users);
79
80		$queryTable = $this->getConnection()->createQueryTable('tiki_user_reports_cache', 'SELECT * FROM tiki_user_reports_cache');
81
82		$this->assertTablesEqual($expectedTable, $queryTable);
83
84		$this->assertEquals($expectedResult, $watches);
85	}
86}
87