ContactTest::testLinkedEntry()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 38
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 22
nc 1
nop 0
dl 0
loc 38
rs 9.568
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Test the contact field - If there is a free-text in info_contact, its content
5
 * would be stored in info_from. If info_link_id is greater then 0, link-title
6
 * of that id would be stored in info_from allowing regular search to find the
7
 * entry.
8
 *
9
 * @link http://www.egroupware.org
10
 * @author Nathan Gray
11
 * @package infolog
12
 * @copyright (c) 2017  Nathan Gray
13
 * @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
14
 */
15
16
namespace EGroupware\Infolog;
17
18
require_once realpath(__DIR__.'/../../api/tests/AppTest.php');	// Application test base
19
20
use Egroupware\Api\Etemplate;
21
22
class ContactTest extends \EGroupware\Api\AppTest
23
{
24
25
	protected $ui;
26
	protected $bo;
27
28
	// Infolog under test
29
	protected $info_id = null;
30
31
	protected function setUp() : void
32
	{
33
		$this->ui = new \infolog_ui();
34
35
		$this->ui->tmpl = $this->createPartialMock(Etemplate::class, array('exec', 'read'));
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createPartialMock... array('exec', 'read')) of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type EGroupware\Api\Etemplate of property $tmpl.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
36
37
		$this->bo = $this->ui->bo;
38
39
		$this->mockTracking($this->bo, 'infolog_tracking');
40
	}
41
42
	protected function tearDown() : void
43
	{
44
		// Double delete to make sure it's gone, not preserved due to history setting
45
		if($this->info_id)
46
		{
47
			$this->bo->delete($this->info_id);
48
			$this->bo->delete($this->info_id);
49
		}
50
		$this->bo = null;
51
	}
52
53
	/**
54
	 * Test that free text in the info_contact field winds up in info_from, and
55
	 * when loaded again it is put into the search of info_contact for display.
56
	 */
57
	public function testFreeText()
58
	{
59
		$content = array(
60
			'contact' => array(
61
				'app'   =>	'addressbook',
62
				'id'    =>	Null,
63
				'search'=>	'Free text'
64
			)
65
		);
66
67
		$info = $this->getTestInfolog($content);
68
69
		// Skipping notifications - save initial state
70
		$this->info_id = $this->bo->write($info, true, true, true, true);
71
72
		// Read it back to check
73
		$saved = $this->bo->read($this->info_id);
74
75
		$this->assertEquals($content['contact']['search'], $saved['info_from']);
76
		$this->assertEquals(0, $saved['info_link_id']);
77
78
		// Mock the etemplate call to check the results
79
		$this->ui->tmpl->expects($this->once())
80
			->method('exec')
81
			->will(
82
				$this->returnCallback(function($method, $info) {
83
					$this->assertNotNull($info['info_id']);
84
					$this->assertEquals('Free text', $info['info_contact']['title']);
85
					return true;
86
				})
87
			);
88
89
		// Make a call to edit, looks like initial load
90
		$_REQUEST['info_id'] = $this->info_id;
91
		$this->ui->edit();
92
93
		// Change it
94
		$saved['info_contact']['search'] =  'Totally different';
95
96
		// Skipping notifications - save initial state
97
		$this->bo->write($saved, true, true, true, true);
98
99
		// Read it back to check
100
		$resaved = $this->bo->read($this->info_id);
101
		$this->assertEquals('Totally different', $resaved['info_from'], 'Did not change free text');
102
		$this->assertEquals(0, $resaved['info_link_id']);
103
104
		// Now clear it
105
		$saved = $resaved;
106
		$saved['info_contact']['search'] = '';
107
108
		// Skipping notifications - save initial state
109
		$this->bo->write($saved, false, false);
110
111
		// Read it back to check
112
		$resaved = $this->bo->read($this->info_id);
113
		$this->assertEquals('', $resaved['info_from'], 'Did not clear free text');
114
		$this->assertEquals(0, $resaved['info_link_id']);
115
	}
116
117
	/**
118
	 * Test that a selected entry is put into info_link_id, and its link title
119
	 * is put into info_from (not the search text)
120
	 */
121
	public function testLinkedEntry()
122
	{
123
		$content = array(
124
			'contact' => array(
125
				'app'   =>	'addressbook',
126
				// Linking to current user's contact
127
				'id'    =>	$GLOBALS['egw_info']['user']['person_id'],
128
				'search'=>	'Free text'
129
			)
130
		);
131
		$link_title = $GLOBALS['egw']->contacts->link_title($content['contact']['id']);
132
		$info = $this->getTestInfolog($content);
133
134
		// Skipping notifications - save initial state
135
		$this->info_id = $this->bo->write($info, true, true, true, true);
136
137
		// Read it back to check
138
		$saved = $this->bo->read($this->info_id);
139
140
		$this->assertEquals($link_title, $saved['info_contact']['title'], 'Link title was missing');
141
		$this->assertNotEquals(0, $saved['info_link_id']);
142
143
		// Mock the etemplate call to check the results
144
		$this->ui->tmpl->expects($this->once())
145
			->method('exec')
146
			->will(
147
				$this->returnCallback(function($method, $info) use($link_title) {
148
					$this->assertNotNull($info['info_id']);
149
					$this->assertEquals('', $info['contact']['search']);
150
					$this->assertEquals($GLOBALS['egw_info']['user']['person_id'], $info['info_contact']['id']);
151
					$this->assertEquals($link_title, $info['info_contact']['title']);
152
				})
153
			);
154
155
		// Make a call to edit, looks like initial load
156
		$_REQUEST['info_id'] = $this->info_id;
157
		$this->ui->edit();
158
		unset($_REQUEST['info_id']);
159
	}
160
161
	/**
162
	 * Test that creating a sub-infolog keeps info_contact on the parent
163
	 *
164
	 * @ticket 24920
165
	 */
166
	public function testSubEntry()
167
	{
168
		// Parent needs a project & contact for this
169
		$content = array(
170
			'contact' => array(
171
				'app'   =>	'addressbook',
172
				'id'    =>	Null,
173
				'search'=>	'Free text'
174
			)
175
		);
176
		$parent = $this->getTestInfolog($content);
177
178
		// Skipping notifications - save initial state
179
		$parent_id = $this->bo->write($parent, true, true, true, true);
180
181
		// Mock the etemplate call to check sub gets parent's contact
182
		$sub = array();
183
		$this->ui->tmpl->expects($this->once())
184
			->method('exec')
185
			->will(
186
				$this->returnCallback(function($method, $info) use($parent, &$sub) {
187
					$this->assertNull($info['info_id']);
188
					$this->assertEquals($parent['info_id'], $info['info_id_parent']);
189
					$this->assertEquals($parent['info_contact']['id'], $info['info_contact']['id']);
190
					$this->assertEquals($parent['info_contact']['app'], $info['info_contact']['app']);
191
					$this->assertEquals($parent['info_from'], $info['info_from']);
192
					$sub = $info;
193
					return true;
194
				})
195
			);
196
197
		// Make a sub-entry
198
		$_REQUEST['action'] = 'sp';
199
		$_REQUEST['action_id'] = $parent['info_id'];
200
		$this->ui->edit();
201
202
		// Skipping notifications - save initial state
203
		$this->info_id = $this->bo->write($sub, true, true, true, true);
204
205
		// Read it back to check
206
		$saved = $this->bo->read($this->info_id);
207
208
		$this->assertEquals($parent['pm_id'], $saved['pm_id']);
209
		$this->assertEquals($parent['info_from'], $saved['info_from']);
210
		$this->assertEquals(json_encode($parent['info_contact']), json_encode($saved['info_contact']));
211
		$this->assertEquals($parent_id, $saved['info_id_parent']);
212
213
		// Check parent
214
		$parent_reload = $this->bo->read($parent_id);
215
216
		$this->assertEquals($parent['pm_id'], $parent_reload['pm_id']);
217
		$this->assertEquals($parent['info_from'], $parent_reload['info_from']);
218
		$this->assertEquals($parent['info_contact'], $parent_reload['info_contact']);
219
220
		// Remove parent (twice, for history preservation)
221
		$this->bo->delete($parent_id);
222
		$this->bo->delete($parent_id);
223
	}
224
225
	/**
226
	 * Set up a basic infolog entry for testing with the specified fields
227
	 * set.
228
	 *
229
	 * @param Array $fields Fields to be set for initial conditions
230
	 * @return Array
231
	 */
232
	protected function getTestInfolog($fields)
233
	{
234
		$info = array(
235
			'info_subject'     =>	'Test Infolog Entry for ' . $this->getName()
236
		);
237
238
		foreach($fields as $field => $value)
239
		{
240
			$info["info_{$field}"] = $value;
241
		}
242
243
		return $info;
244
	}
245
246
}
247