1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* @copyright 2018 Mautic Contributors. All rights reserved |
5
|
|
|
* @author Mautic |
6
|
|
|
* |
7
|
|
|
* @link http://mautic.org |
8
|
|
|
* |
9
|
|
|
* @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Mautic\ConfigBundle\Tests\Event; |
13
|
|
|
|
14
|
|
|
use Mautic\ConfigBundle\Event\ConfigEvent; |
15
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
16
|
|
|
use Symfony\Component\HttpFoundation\ParameterBag; |
17
|
|
|
|
18
|
|
|
class ConfigEventTest extends \PHPUnit\Framework\TestCase |
19
|
|
|
{ |
20
|
|
|
public function testGetSetConfig() |
21
|
|
|
{ |
22
|
|
|
// Config not defined |
23
|
|
|
$config = []; |
24
|
|
|
$paramBag = $this->createMock(ParameterBag::class); |
25
|
|
|
$event = new ConfigEvent($config, $paramBag); |
26
|
|
|
$key = 'undefined'; |
27
|
|
|
$this->assertEquals([], $event->getConfig($key)); |
28
|
|
|
|
29
|
|
|
// Config defined with setter |
30
|
|
|
$key = 'defined'; |
31
|
|
|
$config = ['config' => []]; |
32
|
|
|
$this->assertNull($event->setConfig($config, $key)); |
|
|
|
|
33
|
|
|
$this->assertEquals($config, $event->getConfig($key)); |
34
|
|
|
|
35
|
|
|
// Config not found by key so complete config returned; |
36
|
|
|
$undefinedKey = 'undefined'; |
37
|
|
|
$this->assertEquals([], $event->getConfig($undefinedKey)); |
38
|
|
|
|
39
|
|
|
// Get complete config |
40
|
|
|
$config = [$key => $config]; |
41
|
|
|
$this->assertEquals($config, $event->getConfig()); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function testGetSetPreserved() |
45
|
|
|
{ |
46
|
|
|
$config = []; |
47
|
|
|
$paramBag = $this->createMock(ParameterBag::class); |
48
|
|
|
$event = new ConfigEvent($config, $paramBag); |
49
|
|
|
|
50
|
|
|
$this->assertEquals([], $event->getPreservedFields()); |
51
|
|
|
|
52
|
|
|
$preserved = 'preserved'; |
53
|
|
|
$result = [$preserved]; |
54
|
|
|
$this->assertNull($event->unsetIfEmpty($preserved)); |
|
|
|
|
55
|
|
|
$this->assertEquals($result, $event->getPreservedFields()); |
56
|
|
|
|
57
|
|
|
$preserved = ['preserved' => 'value']; |
58
|
|
|
$result = array_merge($result, $preserved); |
59
|
|
|
$this->assertNull($event->unsetIfEmpty($preserved)); |
|
|
|
|
60
|
|
|
$this->assertEquals($result, $event->getPreservedFields()); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function testGetSetErrors() |
64
|
|
|
{ |
65
|
|
|
$config = []; |
66
|
|
|
$paramBag = $this->createMock(ParameterBag::class); |
67
|
|
|
$event = new ConfigEvent($config, $paramBag); |
68
|
|
|
|
69
|
|
|
$this->assertEquals([], $event->getErrors()); |
70
|
|
|
|
71
|
|
|
$message = 'message'; |
72
|
|
|
$messages = [$message => []]; |
73
|
|
|
$this->assertEquals($event, $event->setError($message)); |
74
|
|
|
$this->assertEquals($messages, $event->getErrors()); |
75
|
|
|
|
76
|
|
|
$message = 'message'; |
77
|
|
|
$messageVars = ['var' => 'value']; |
78
|
|
|
$messages = [$message => $messageVars]; |
79
|
|
|
$this->assertEquals($event, $event->setError($message, $messageVars)); |
80
|
|
|
$this->assertEquals($messages, $event->getErrors()); |
81
|
|
|
|
82
|
|
|
$message = 'message'; |
83
|
|
|
$messageVars = ['var' => 'value']; |
84
|
|
|
$key = 'key'; |
85
|
|
|
$field = 'field'; |
86
|
|
|
$fieldErrors[$key][$field] = [ |
|
|
|
|
87
|
|
|
$message, |
88
|
|
|
$messageVars, |
89
|
|
|
]; |
90
|
|
|
$this->assertEquals($event, $event->setError($message, $messageVars, $key, $field)); |
91
|
|
|
$this->assertEquals($fieldErrors, $event->getFieldErrors()); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function testGetFileContent() |
95
|
|
|
{ |
96
|
|
|
$config = []; |
97
|
|
|
$paramBag = $this->createMock(ParameterBag::class); |
98
|
|
|
$event = new ConfigEvent($config, $paramBag); |
99
|
|
|
|
100
|
|
|
$fileContent = 'content'; |
101
|
|
|
$fileHandler = tmpfile(); |
102
|
|
|
$realPath = stream_get_meta_data($fileHandler)['uri']; |
103
|
|
|
fwrite($fileHandler, ' '.$fileContent); |
104
|
|
|
|
105
|
|
|
$uploadedFile = $this->createMock(UploadedFile::class); |
106
|
|
|
$uploadedFile->expects($this->once()) |
107
|
|
|
->method('getRealPath') |
108
|
|
|
->willReturn($realPath); |
109
|
|
|
|
110
|
|
|
$this->assertEquals($fileContent, $event->getFileContent($uploadedFile)); |
111
|
|
|
$this->assertFalse(file_exists($realPath)); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function testEncodeFileContents() |
115
|
|
|
{ |
116
|
|
|
$config = []; |
117
|
|
|
$paramBag = $this->createMock(ParameterBag::class); |
118
|
|
|
$event = new ConfigEvent($config, $paramBag); |
119
|
|
|
|
120
|
|
|
$string = 'řčžýřžýčř'; |
121
|
|
|
$result = 'xZnEjcW+w73FmcW+w73EjcWZ'; |
122
|
|
|
$this->assertEquals($result, $event->encodeFileContents($string)); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function testNormalizedDataGetSet() |
126
|
|
|
{ |
127
|
|
|
$config = []; |
128
|
|
|
$paramBag = $this->createMock(ParameterBag::class); |
129
|
|
|
$event = new ConfigEvent($config, $paramBag); |
130
|
|
|
|
131
|
|
|
$origNormData = ['orig']; |
132
|
|
|
|
133
|
|
|
$this->assertInstanceOf(ConfigEvent::class, $event->setOriginalNormData($origNormData)); |
134
|
|
|
$this->assertEquals($origNormData, $event->getOriginalNormData()); |
135
|
|
|
|
136
|
|
|
$normData = ['norm']; |
137
|
|
|
|
138
|
|
|
$this->assertNull($event->setNormData($normData)); |
|
|
|
|
139
|
|
|
$this->assertEquals($normData, $event->getNormData()); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.