|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* T3Bot. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Frank Nägler <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* @link http://www.t3bot.de |
|
8
|
|
|
* @link http://wiki.typo3.org/T3Bot |
|
9
|
|
|
*/ |
|
10
|
|
|
namespace T3Bot\Tests\Unit\Controller; |
|
11
|
|
|
|
|
12
|
|
|
use Doctrine\DBAL\Configuration; |
|
13
|
|
|
use Doctrine\DBAL\DriverManager; |
|
14
|
|
|
use T3Bot\Controller\WebHookController; |
|
15
|
|
|
use T3Bot\Slack\Message; |
|
16
|
|
|
use T3Bot\Tests\Unit\BaseTestCase; |
|
17
|
|
|
|
|
18
|
|
|
/** @noinspection LongInheritanceChainInspection */ |
|
19
|
|
|
class WebHookControllerTest extends BaseTestCase |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* |
|
23
|
|
|
*/ |
|
24
|
|
|
public function setUp() |
|
25
|
|
|
{ |
|
26
|
|
|
$GLOBALS['config']['webhook']['t3o-registrations'] = [ |
|
27
|
|
|
'securityToken' => 'SECURE_VALID_TOKEN', |
|
28
|
|
|
'channels' => ['#t3o-registrations'], |
|
29
|
|
|
]; |
|
30
|
|
|
$GLOBALS['config']['slack']['botAvatar'] = 'botty'; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @return array |
|
35
|
|
|
*/ |
|
36
|
|
|
public function validWebhookDataProvider() |
|
37
|
|
|
{ |
|
38
|
|
|
return [ |
|
39
|
|
|
'webhook' => [__DIR__ . '/../Fixtures/Valid/webhook.json'], |
|
40
|
|
|
'webhook_danger' => [__DIR__ . '/../Fixtures/Valid/webhook_danger.json'], |
|
41
|
|
|
'webhook_info' => [__DIR__ . '/../Fixtures/Valid/webhook_info.json'], |
|
42
|
|
|
'webhook_notice' => [__DIR__ . '/../Fixtures/Valid/webhook_notice.json'], |
|
43
|
|
|
'webhook_ok' => [__DIR__ . '/../Fixtures/Valid/webhook_ok.json'], |
|
44
|
|
|
'webhook_warning' => [__DIR__ . '/../Fixtures/Valid/webhook_warning.json'], |
|
45
|
|
|
]; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @test |
|
50
|
|
|
*/ |
|
51
|
|
|
public function processWebhookWithUnknownHook() |
|
52
|
|
|
{ |
|
53
|
|
|
$controller = $this->getMock(WebHookController::class, ['postToSlack'], [$GLOBALS['config']]); |
|
54
|
|
|
$controller |
|
55
|
|
|
->expects(static::never()) |
|
56
|
|
|
->method('postToSlack'); |
|
57
|
|
|
$controller->process('unknow', __DIR__ . '/../Fixtures/Valid/webhook.json'); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @test |
|
62
|
|
|
* @dataProvider validWebhookDataProvider |
|
63
|
|
|
*/ |
|
64
|
|
|
public function processWebhookWithValidJson($jsonFile) |
|
65
|
|
|
{ |
|
66
|
|
|
$controller = $this->getMock(WebHookController::class, ['postToSlack'], [$GLOBALS['config']]); |
|
67
|
|
|
$controller |
|
68
|
|
|
->expects(static::once()) |
|
69
|
|
|
->method('postToSlack'); |
|
70
|
|
|
$controller->process('t3o-registrations', $jsonFile); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @test |
|
75
|
|
|
*/ |
|
76
|
|
|
public function processWebhookWithInvalidJson() |
|
77
|
|
|
{ |
|
78
|
|
|
$controller = $this->getMock(WebHookController::class, ['postToSlack'], [$GLOBALS['config']]); |
|
79
|
|
|
$controller |
|
80
|
|
|
->expects(static::never()) |
|
81
|
|
|
->method('postToSlack'); |
|
82
|
|
|
$controller->process('t3o-registrations', __DIR__ . '/../Fixtures/Invalid/webhook.json'); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @test |
|
87
|
|
|
*/ |
|
88
|
|
|
public function processChangeMergedWithValidJsonAddEntryToMessageQueue() |
|
89
|
|
|
{ |
|
90
|
|
|
$controller = $this->getMock(WebHookController::class, ['addMessageToQueue'], [$GLOBALS['config']]); |
|
91
|
|
|
$controller |
|
92
|
|
|
->expects(static::once()) |
|
93
|
|
|
->method('addMessageToQueue'); |
|
94
|
|
|
$controller->process('t3o-registrations', __DIR__ . '/../Fixtures/Valid/webhook.json'); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @test |
|
99
|
|
|
*/ |
|
100
|
|
|
public function addMessageToQueueCreatesEntryInDatabase() |
|
101
|
|
|
{ |
|
102
|
|
|
$controller = $this->getMock(WebHookController::class, [], [$GLOBALS['config']]); |
|
103
|
|
|
$testMessage = [ |
|
104
|
|
|
'message' => 'addMessageToQueueCreatesEntryInDatabase-test', |
|
105
|
|
|
'test-id' => uniqid('addMessageToQueueCreatesEntryInDatabase-test', true), |
|
106
|
|
|
]; |
|
107
|
|
|
$result = json_encode($testMessage); |
|
108
|
|
|
$this->getDatabaseConnection()->delete('messages', ['message' => $result]); |
|
109
|
|
|
|
|
110
|
|
|
$this->invokeMethod($controller, 'addMessageToQueue', [$testMessage]); |
|
111
|
|
|
|
|
112
|
|
|
$records = $this->getDatabaseConnection()->fetchAll('SELECT * FROM messages WHERE message = ?', [$result]); |
|
113
|
|
|
|
|
114
|
|
|
static::assertGreaterThan(0, count($records)); |
|
115
|
|
|
$this->getDatabaseConnection()->delete('messages', ['message' => $result]); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @return \Doctrine\DBAL\Connection |
|
120
|
|
|
* |
|
121
|
|
|
* @throws \Doctrine\DBAL\DBALException |
|
122
|
|
|
*/ |
|
123
|
|
|
public function getDatabaseConnection() |
|
124
|
|
|
{ |
|
125
|
|
|
/* @noinspection PhpInternalEntityUsedInspection */ |
|
126
|
|
|
$config = new Configuration(); |
|
127
|
|
|
|
|
128
|
|
|
return DriverManager::getConnection($GLOBALS['config']['db'], $config); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|