1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* CakeCMS Core |
4
|
|
|
* |
5
|
|
|
* This file is part of the of the simple cms based on CakePHP 3. |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
* |
9
|
|
|
* @package Core |
10
|
|
|
* @license MIT |
11
|
|
|
* @copyright MIT License http://www.opensource.org/licenses/mit-license.php |
12
|
|
|
* @link https://github.com/CakeCMS/Core". |
13
|
|
|
* @author Sergey Kalistratov <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace Core\TestSuite; |
17
|
|
|
|
18
|
|
|
use Core\Cms; |
19
|
|
|
use Core\Plugin; |
20
|
|
|
use JBZoo\Utils\Str; |
21
|
|
|
use Cake\Cache\Cache; |
22
|
|
|
use Cake\ORM\TableRegistry; |
23
|
|
|
use Cake\TestSuite\TestCase as CakeTestCase; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Class TestCase |
27
|
|
|
* |
28
|
|
|
* @package Core\TestSuite |
29
|
|
|
*/ |
30
|
|
|
class TestCase extends CakeTestCase |
31
|
|
|
{ |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Default plugin name. |
35
|
|
|
* |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
protected $_plugin = 'Core'; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Core plugin. |
42
|
|
|
* |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
protected $_corePlugin = 'Core'; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Default table name. |
49
|
|
|
* |
50
|
|
|
* @var null|string |
51
|
|
|
*/ |
52
|
|
|
protected $_defaultTable; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Hold CMS object. |
56
|
|
|
* |
57
|
|
|
* @var Cms |
58
|
|
|
*/ |
59
|
|
|
protected $_cms; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Setup the test case, backup the static object values so they can be restored. |
63
|
|
|
* Specifically backs up the contents of Configure and paths in App if they have |
64
|
|
|
* not already been backed up. |
65
|
|
|
* |
66
|
|
|
* @return void |
67
|
|
|
*/ |
68
|
|
View Code Duplication |
public function setUp() |
|
|
|
|
69
|
|
|
{ |
70
|
|
|
parent::setUp(); |
71
|
|
|
|
72
|
|
|
if ($this->_plugin !== $this->_corePlugin) { |
73
|
|
|
$options = [ |
74
|
|
|
'bootstrap' => true, |
75
|
|
|
'routes' => true, |
76
|
|
|
'autoload' => true, |
77
|
|
|
]; |
78
|
|
|
|
79
|
|
|
Plugin::load($this->_plugin, $options); |
80
|
|
|
Plugin::routes($this->_plugin); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if (!Plugin::loaded($this->_corePlugin)) { |
84
|
|
|
$loadParams = [ |
85
|
|
|
'bootstrap' => true, |
86
|
|
|
'routes' => true, |
87
|
|
|
'path' => ROOT . DS, |
88
|
|
|
]; |
89
|
|
|
|
90
|
|
|
Plugin::load($this->_corePlugin, $loadParams); |
91
|
|
|
Plugin::routes($this->_corePlugin); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$this->_cms = Cms::getInstance(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Clears the state used for requests. |
99
|
|
|
* |
100
|
|
|
* @return void |
101
|
|
|
*/ |
102
|
|
View Code Duplication |
public function tearDown() |
|
|
|
|
103
|
|
|
{ |
104
|
|
|
parent::tearDown(); |
105
|
|
|
Plugin::unload($this->_corePlugin); |
106
|
|
|
if ($this->_plugin !== $this->_corePlugin) { |
107
|
|
|
Plugin::unload($this->_plugin); |
108
|
|
|
} |
109
|
|
|
Cache::drop('test_cached'); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Assert check is empty array. |
114
|
|
|
* |
115
|
|
|
* @param array $array |
116
|
|
|
*/ |
117
|
|
|
public static function assertIsEmptyArray(array $array) |
118
|
|
|
{ |
119
|
|
|
self::assertSame([], $array); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Check error validation. |
124
|
|
|
* |
125
|
|
|
* @param mixed $field |
126
|
|
|
* @param mixed $value |
127
|
|
|
* @param array $errorExpected |
128
|
|
|
*/ |
129
|
|
|
public function assertFieldErrorValidation($field, $value, array $errorExpected = []) |
130
|
|
|
{ |
131
|
|
|
$data = [$field => $value]; |
132
|
|
|
$table = $this->_getTable(); |
133
|
|
|
$entity = $table->newEntity($data); |
134
|
|
|
$result = $table->save($entity); |
135
|
|
|
|
136
|
|
|
self::assertFalse($result); |
137
|
|
|
self::assertTrue(is_array($entity->getError($field))); |
138
|
|
|
self::assertSame($errorExpected, $entity->getError($field)); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Get table object. |
143
|
|
|
* |
144
|
|
|
* @param null|string $name |
145
|
|
|
* @return \Cake\ORM\Table |
146
|
|
|
*/ |
147
|
|
|
protected function _getTable($name = null) |
148
|
|
|
{ |
149
|
|
|
$tableName = ($name === null) ? $this->_defaultTable : $name; |
150
|
|
|
return TableRegistry::get($this->_corePlugin . '.' . $tableName); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @param string $string |
155
|
|
|
* @return array |
156
|
|
|
*/ |
157
|
|
|
protected function _getStrArray($string) |
158
|
|
|
{ |
159
|
|
|
$output = []; |
160
|
|
|
$details = explode("\n", $string); |
161
|
|
|
foreach ($details as $string) { |
162
|
|
|
$string = Str::trim($string); |
163
|
|
|
$output[] = $string; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
return $output; |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.