StatusTest::tearDown()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Test the status / completion combinations on an infolog entry
5
 *
6
 * Some statuses changes will also change the percent completed value.
7
 * For example, closed statuses will set completed to 100%, subsequently changing
8
 * to an open status will change completed to less than 100%.  Changing to new
9
 * (not-started) will set completion to 0%.
10
 *
11
 * The list of status and percentage changes is stored in a JSON file
12
 *
13
 * @link http://www.egroupware.org
14
 * @author Nathan Gray
15
 * @package infolog
16
 * @copyright (c) 2017 by Nathan Gray
17
 * @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
18
 */
19
20
21
namespace EGroupware\Infolog;
22
23
require_once realpath(__DIR__.'/../../api/tests/AppTest.php');	// Application test base
24
25
use Egroupware\Api;
26
27
class StatusTest extends \EGroupware\Api\AppTest
28
{
29
30
	private static $map_file = '/status_map.json';
31
32
	protected $bo;
33
34
	// Infolog under test
35
	protected $info_id = null;
36
37
	/**
38
	 * Create a custom status we can use to test
39
	 */
40
	public static function setUpBeforeClass() : void
41
	{
42
		parent::setUpBeforeClass();
43
44
		// Create custom status
45
		$bo = new \infolog_bo();
46
		$bo->status['task']['custom'] = 'custom';
47
48
		Api\Config::save_value('status',$bo->status,'infolog');
49
	}
50
	public static function tearDownAfterClass() : void
51
	{
52
		// Remove custom status
53
		$bo = new \infolog_bo();
54
		unset($bo->status['task']['custom']);
55
		Api\Config::save_value('status',$bo->status,'infolog');
56
57
		// Have to remove custom status first, before the DB is gone
58
		parent::tearDownAfterClass();
59
	}
60
61
	protected function setUp() : void
62
	{
63
		$this->bo = new \infolog_bo();
64
65
		$this->mockTracking($this->bo, 'infolog_tracking');
66
	}
67
68
	protected function tearDown() : void
69
	{
70
		$this->bo = null;
71
	}
72
73
	/**
74
	 * Step through the map and check each one
75
	 */
76
	public function testStatusChange()
77
	{
78
		$json = file_get_contents(realpath(__DIR__.static::$map_file));
0 ignored issues
show
Bug introduced by
Since $map_file is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $map_file to at least protected.
Loading history...
79
		// Strip the comments out of the json file, they're not valid
80
		$json = preg_replace("#(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/)|([\s\t]//.*)|(^//.*)#", '', $json);
81
		$map = json_decode($json, true);
82
83
		$this->assertNotEquals(0, count($map), 'Could not read status map ' . static::$map_file);
84
85
		foreach($map as $test => $map)
86
		{
87
			$this->checkOne($map['from'], $map['to'], $map['expected']);
88
		}
89
	}
90
91
	/**
92
	 * Check a change
93
	 *
94
	 * @param Array $from
95
	 * @param Array $to
96
	 * @param Array $expected
97
	 */
98
	protected function checkOne($from, $to, $expected)
99
	{
100
		$info = $this->getTestInfolog($from);
101
102
		// Skipping notifications - save initial state
103
		$this->info_id = $this->bo->write($info, true, true, true, true);
104
105
		foreach($to as $field => $value)
106
		{
107
			$info["info_{$field}"] = $value;
108
		}
109
110
		// Skipping notifications
111
		$this->bo->write($info, true, true, true, true);
112
113
		// Read it back to check
114
		$saved = $this->bo->read($this->info_id);
115
116
		$test_name = $this->getTestName($from, $to);
117
118
		foreach($expected as $field => $value)
119
		{
120
			$this->assertEquals($value, $saved["info_{$field}"],
121
					"$test_name failed on '$field' field");
122
		}
123
124
		// Remove infolog under test
125
		if($this->info_id)
126
		{
127
			$this->bo->delete($this->info_id, False, False, True);
128
			$this->bo->delete($this->info_id, False, False, True);
129
		}
130
	}
131
132
	/**
133
	 * Get a text representation of the change so we can tell which one went
134
	 * wrong
135
	 *
136
	 * @param Array $from
137
	 * @param Array $to
138
	 * @return String
139
	 */
140
	protected function getTestName($from, $to)
141
	{
142
		$name = array();
143
		foreach($from as $field =>  $value)
144
		{
145
			$name[] = $field . ': ' . $from[$field] . (array_key_exists($field, $to) ? ' => ' . $to[$field] : '');
146
		}
147
		return implode(', ', $name);
148
	}
149
150
	/**
151
	 * Set up a basic infolog entry for testing with the specified fields
152
	 * set.
153
	 *
154
	 * @param Array $from Fields to be set for initial conditions
155
	 * @return Array
156
	 */
157
	protected function getTestInfolog($from)
158
	{
159
		$info = array(
160
			'info_subject'     =>	'Test Infolog Entry for ' . $this->getName()
161
		);
162
163
		foreach($from as $field => $value)
164
		{
165
			$info["info_{$field}"] = $value;
166
		}
167
168
		return $info;
169
	}
170
171
	/**
172
	 * Check status changes with custom status
173
	 */
174
	public function testCustomStatus()
175
	{
176
177
		$this->checkOne(
178
				array('status' => 'custom', 'percent' => 10),
179
				array('status' => 'ongoing'),
180
				array('status' => 'ongoing', 'percent' => 10)
181
		);
182
183
184
	}
185
}
186