1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Saito - The Threaded Web Forum |
7
|
|
|
* |
8
|
|
|
* @copyright Copyright (c) the Saito Project Developers |
9
|
|
|
* @link https://github.com/Schlaefer/Saito |
10
|
|
|
* @license http://opensource.org/licenses/MIT |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Saito\Test; |
14
|
|
|
|
15
|
|
|
use Cake\Core\Configure; |
16
|
|
|
use Cake\Event\EventManager; |
17
|
|
|
use Cake\Filesystem\File; |
18
|
|
|
use Cake\I18n\I18n; |
19
|
|
|
use Cake\Mailer\TransportFactory; |
20
|
|
|
use Cake\ORM\TableRegistry; |
21
|
|
|
use Cake\Utility\Inflector; |
22
|
|
|
use Saito\App\Registry; |
23
|
|
|
use Saito\Cache\CacheSupport; |
24
|
|
|
|
25
|
|
|
trait TestCaseTrait |
26
|
|
|
{ |
27
|
|
|
private $saitoSettings; |
28
|
|
|
|
29
|
|
|
protected $saitoPermissions; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* set-up saito |
33
|
|
|
* |
34
|
|
|
* @return void |
35
|
|
|
*/ |
36
|
|
|
protected function setUpSaito() |
37
|
|
|
{ |
38
|
|
|
Registry::initialize(); |
39
|
|
|
|
40
|
|
|
$this->_storeSettings(); |
41
|
|
|
$this->mockMailTransporter(); |
42
|
|
|
$this->_clearCaches(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* tear down saito |
47
|
|
|
* |
48
|
|
|
* @return void |
49
|
|
|
*/ |
50
|
|
|
protected function tearDownSaito() |
51
|
|
|
{ |
52
|
|
|
$this->_restoreSettings(); |
53
|
|
|
$this->_clearCaches(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* clear caches |
58
|
|
|
* |
59
|
|
|
* @return void |
60
|
|
|
*/ |
61
|
|
|
protected function _clearCaches() |
62
|
|
|
{ |
63
|
|
|
$CacheSupport = new CacheSupport(); |
64
|
|
|
$CacheSupport->clear(); |
65
|
|
|
EventManager::instance()->off($CacheSupport); |
66
|
|
|
unset($CacheSupport); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* store global settings |
71
|
|
|
* |
72
|
|
|
* @return void |
73
|
|
|
*/ |
74
|
|
|
protected function _storeSettings() |
75
|
|
|
{ |
76
|
|
|
$this->saitoSettings = Configure::read('Saito.Settings'); |
77
|
|
|
$this->saitoPermissions = clone(Configure::read('Saito.Permission.Resources')); |
78
|
|
|
$this->setI18n('en'); |
79
|
|
|
Configure::write('Saito.Settings.ParserPlugin', \Plugin\BbcodeParser\src\Lib\Markup::class); |
80
|
|
|
Configure::write('Saito.Settings.uploader', clone($this->saitoSettings['uploader'])); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* restore global settings |
85
|
|
|
* |
86
|
|
|
* @return void |
87
|
|
|
*/ |
88
|
|
|
protected function _restoreSettings() |
89
|
|
|
{ |
90
|
|
|
Configure::write('Saito.Settings', $this->saitoSettings); |
91
|
|
|
Configure::write('Saito.Permission.Resources', $this->saitoPermissions); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Set the current translation language |
96
|
|
|
* |
97
|
|
|
* @param string $lang language code |
98
|
|
|
* @return void |
99
|
|
|
*/ |
100
|
|
|
public function setI18n(string $lang): void |
101
|
|
|
{ |
102
|
|
|
Configure::write('Saito.language', $lang); |
103
|
|
|
I18n::setLocale($lang); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Mock table |
108
|
|
|
* |
109
|
|
|
* @param string $table table |
110
|
|
|
* @param array $methods methods to mock |
111
|
|
|
* @return mixed |
112
|
|
|
*/ |
113
|
|
|
public function getMockForTable($table, array $methods = []) |
114
|
|
|
{ |
115
|
|
|
$tableName = Inflector::underscore($table); |
116
|
|
|
$Mock = $this->getMockForModel( |
|
|
|
|
117
|
|
|
$table, |
118
|
|
|
$methods, |
119
|
|
|
['table' => strtolower($tableName)] |
120
|
|
|
); |
121
|
|
|
|
122
|
|
|
return $Mock; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Insert categories into permissions |
127
|
|
|
* |
128
|
|
|
* @return void |
129
|
|
|
*/ |
130
|
|
|
protected function insertCategoryPermissions(): void |
131
|
|
|
{ |
132
|
|
|
Registry::get('Permissions') |
133
|
|
|
->buildCategories(TableRegistry::getTableLocator()->get('Categories')); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Mock mailtransporter |
138
|
|
|
* |
139
|
|
|
* @return mixed |
140
|
|
|
*/ |
141
|
|
|
protected function mockMailTransporter() |
142
|
|
|
{ |
143
|
|
|
$mock = $this->createMock('Cake\Mailer\Transport\DebugTransport'); |
|
|
|
|
144
|
|
|
TransportFactory::drop('saito'); |
145
|
|
|
TransportFactory::setConfig('saito', $mock); |
146
|
|
|
|
147
|
|
|
return $mock; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Creates a mock image file in $file |
152
|
|
|
* |
153
|
|
|
* @param File $file File with extension. |
154
|
|
|
* |
155
|
|
|
* Mime type is taken from extension. Allowed extensions: png, jpeg, jpg |
156
|
|
|
* |
157
|
|
|
* @param int $size size of the mock image in kB |
158
|
|
|
* @return void |
159
|
|
|
*/ |
160
|
|
|
protected function mockMediaFile(File $file, int $size = 100): void |
161
|
|
|
{ |
162
|
|
|
//// Create single pixel image |
163
|
|
|
$Image = imagecreatetruecolor(1, 1); |
164
|
|
|
imagesetpixel($Image, 0, 0, imagecolorallocate($Image, 0, 0, 0)); |
|
|
|
|
165
|
|
|
|
166
|
|
|
switch ($file->ext()) { |
167
|
|
|
case 'jpeg': |
168
|
|
|
case 'jpg': |
169
|
|
|
imagejpeg($Image, $file->path); |
|
|
|
|
170
|
|
|
break; |
171
|
|
|
case 'png': |
172
|
|
|
imagepng($Image, $file->path); |
|
|
|
|
173
|
|
|
break; |
174
|
|
|
default: |
175
|
|
|
throw new \InvalidArgumentException(); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
// pad to saze with garbage data |
179
|
|
|
$file->append(str_repeat('0', $size * 1024)); |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.