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_Send_EmailBuilderTest extends TikiTestCase
9{
10	protected $obj;
11
12	protected $tikilib;
13
14	protected function setUp()
15	{
16		$this->tikilib = $this->getMockBuilder('TikiLib')->getMock();
17		$this->factory = $this->createMock('Reports_Send_EmailBuilder_Factory');
18
19		$this->obj = new Reports_Send_EmailBuilder($this->tikilib, new Reports_Send_EmailBuilder_Factory);
20
21		$this->defaultReportPreferences = ['type' => 'plain', 'view' => 'detailed'];
22	}
23
24	public function testMakeEmailBody_shouldReturnStringIfNothingHappened()
25	{
26		$this->assertEquals('Nothing has happened.', $this->obj->makeEmailBody([], $this->defaultReportPreferences));
27	}
28
29	public function testMakeEmailBody_shouldReturnCalendarChangedReportInDetailedViewMode()
30	{
31		$this->tikilib->expects($this->exactly(2))->method('get_short_datetime')
32			->will($this->returnValue('2011-09-13 11:19'));
33
34		$calendarlib = $this->createMock(get_class(TikiLib::lib('calendar')));
35		$calendarlib->expects($this->exactly(2))
36			->method('get_item')
37			->will($this->returnValue(['name' => 'Calendar item name']));
38
39		$tikilib = new TestableTikiLib;
40		$tikilib->overrideLibs(['calendar' => $calendarlib]);
41
42		$this->defaultReportPreferences['view'] = 'detailed';
43
44		$reportCache = [
45			[
46				'user' => 'admin',
47				'event' => 'calendar_changed',
48				'data' => ['event' => 'calendar_changed', 'calitemId' => '2', 'user' => 'admin', 'base_url' => 'http://example.com'],
49				'time' => '2011-09-12 20:30:31',
50			],
51			[
52				'user' => 'admin',
53				'event' => 'calendar_changed',
54				'data' => ['event' => 'calendar_changed', 'calitemId' => '1', 'user' => 'admin', 'base_url' => 'http://example.com'],
55				'time' => '2011-09-13 11:19:31',
56			],
57		];
58
59		$output = $this->obj->makeEmailBody($reportCache, $this->defaultReportPreferences);
60
61		$this->assertContains('2011-09-13 11:19: admin added or updated event Calendar item name', $output);
62	}
63
64	public function testMakeEmailBody_shouldReturnTrackerItemCommentReportInDetailedViewMode()
65	{
66		$this->tikilib->expects($this->once())->method('get_short_datetime')
67			->will($this->returnValue('2011-09-12 20:30'));
68
69		$trklib = $this->createMock(get_class(TikiLib::lib('trk')));
70		$trklib->expects($this->once())
71			->method('get_tracker')
72			->will($this->returnValue(['id' => '2', 'name' => 'Test Tracker']));
73		$trklib->expects($this->once())
74			->method('get_isMain_value')
75			->will($this->returnValue('Tracker item name'));
76
77		$tikilib = new TestableTikiLib;
78		$tikilib->overrideLibs(['trk' => $trklib]);
79
80		$this->defaultReportPreferences['view'] = 'detailed';
81
82		$reportCache = [
83			[
84				'user' => 'admin',
85				'event' => 'tracker_item_comment',
86				'data' => ['event' => 'tracker_item_comment', 'trackerId' => '2', 'itemId' => '4', 'threadId' => '13', 'user' => 'admin', 'base_url' => 'http://example.com'],
87				'time' => '2011-09-12 20:30:31',
88			],
89		];
90
91		$output = $this->obj->makeEmailBody($reportCache, $this->defaultReportPreferences);
92
93		$this->assertContains('2011-09-12 20:30: admin added a new comment to Tracker item name', $output);
94	}
95
96	public function testMakeEmailBody_shouldUseCategoryChangedObject()
97	{
98		$obj = new Reports_Send_EmailBuilder($this->tikilib, $this->factory);
99
100		$reportCache = [
101			[
102				'user' => 'admin',
103				'categoryId' => 1,
104				'event' => 'category_changed',
105				'data' => ['action' => 'object entered category', 'user' => 'admin', 'objectType' => '', 'objectUrl' => '', 'objectName' => '', 'categoryId' => '', 'categoryName' => ''],
106				'time' => '2011-09-12 20:30:31',
107			],
108		];
109
110		$categoryChanged = $this->createMock('Reports_Send_EmailBuilder_CategoryChanged');
111		$categoryChanged->expects($this->once())->method('getTitle');
112		$categoryChanged->expects($this->once())->method('getOutput');
113
114		$this->factory->expects($this->once())->method('build')
115			->with('category_changed')->will($this->returnValue($categoryChanged));
116
117		$obj->makeEmailBody($reportCache, $this->defaultReportPreferences);
118	}
119}
120