Welcome to mirror list, hosted at ThFree Co, Russian Federation.

NotifierTest.php « Notification « Unit « tests - github.com/undo-ransomware/ransomware_detection.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 88d063b4fad1ad5011d95bfd54bfe78752d3adb2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php

/**
 * @copyright Copyright (c) 2017 Matthias Held <matthias.held@uni-konstanz.de>
 * @author Matthias Held <matthias.held@uni-konstanz.de>
 * @license GNU AGPL version 3 or any later version
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

namespace OCA\RansomwareDetection\tests\Unit\Notification;

use OCA\RansomwareDetection\AppInfo\Application;
use OCA\RansomwareDetection\Notification\Notifier;
use OCP\IURLGenerator;
use OCP\IUserManager;
use OCP\IL10N;
use OCP\L10N\IFactory;
use OCP\Notification\IManager;
use OCP\Notification\INotification;
use OCP\IConfig;
use Test\TestCase;

class NotifierTest extends TestCase
{
    /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
    protected $config;

    /** @var IL10N|\PHPUnit_Framework_MockObject_MockObject */
    protected $l;

    /** @var IFactory|\PHPUnit_Framework_MockObject_MockObject */
    protected $l10nFactory;

    /** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */
    protected $userManager;

    /** @var IManager|\PHPUnit_Framework_MockObject_MockObject */
    protected $notificationManager;

    /** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject */
    protected $urlGenerator;

    /** @var Notifier|\PHPUnit_Framework_MockObject_MockObject */
    protected $notifier;

    public function setUp(): void
    {
        parent::setUp();

        $this->l = $this->createMock(IL10N::class);
        $this->l->expects($this->any())
            ->method('t')
            ->willReturnCallback(function ($string, $args) {
                return vsprintf($string, $args);
            });
        $this->l10nFactory = $this->createMock(IFactory::class);
        $this->l10nFactory->expects($this->any())
            ->method('get')
            ->willReturn($this->l);
        $this->userManager = $this->createMock(IUserManager::class);
        $this->notificationManager = $this->createMock(IManager::class);
        $this->urlGenerator = $this->createMock(IURLGenerator::class);
        $this->config = $this->createMock(IConfig::class);

        $this->notifier = new Notifier($this->config, $this->l10nFactory, $this->userManager, $this->notificationManager, $this->urlGenerator, 'john');
    }

    /**
     * @expectedException \InvalidArgumentException
     * @expectedExceptionMessage Unknown app
     */
    public function testPrepareWrongApp()
    {
        /** @var INotification|\PHPUnit_Framework_MockObject_MockObject $notification */
        $notification = $this->createMock(INotification::class);

        $notification->expects($this->once())
            ->method('getApp')
            ->willReturn('notifications');
        $notification->expects($this->never())
            ->method('getSubject');

        $this->notifier->prepare($notification, 'en');
    }

    /**
     * @expectedException \InvalidArgumentException
     * @expectedExceptionMessage Unknown subject
     */
    public function testPrepareWrongSubject()
    {
        /** @var INotification|\PHPUnit_Framework_MockObject_MockObject $notification */
        $notification = $this->createMock(INotification::class);

        $notification->expects($this->once())
            ->method('getApp')
            ->willReturn(Application::APP_ID);
        $notification->expects($this->once())
            ->method('getSubject')
            ->willReturn('wrong subject');

        $this->notifier->prepare($notification, 'en');
    }

    public function dataPrepare()
    {
        return [
            ['ransomware_attack_detected', ['Detected suspicious file operations.'], ['Detected a sequence of suspicious file operations.'], true],

        ];
    }

    /**
     * @dataProvider dataPrepare
     *
     * @param string $subject
     * @param array  $subjectParams
     * @param array  $messageParams
     * @param bool   $setMessage
     */
    public function testPrepare($subject, $subjectParams, $messageParams, $setMessage)
    {
        /** @var INotification|\PHPUnit_Framework_MockObject_MockObject $notification */
        $notification = $this->createMock(INotification::class);

        $notification->expects($this->once())
            ->method('getApp')
            ->willReturn(Application::APP_ID);
        $notification->expects($this->once())
            ->method('getSubject')
            ->willReturn($subject);
        $notification->expects($this->once())
            ->method('getSubjectParameters')
            ->willReturn($subjectParams);
        $notification->expects($this->once())
            ->method('getMessageParameters')
            ->willReturn($messageParams);
        $notification->expects($this->once())
            ->method('setParsedSubject')
            ->with($subjectParams[0])
            ->willReturnSelf();
        if ($setMessage) {
            $notification->expects($this->once())
                ->method('setParsedMessage')
                ->with($messageParams[0])
                ->willReturnSelf();
        } else {
            $notification->expects($this->never())
                ->method('setParsedMessage');
        }
        $this->urlGenerator->expects($this->once())
            ->method('imagePath')
            ->with(Application::APP_ID, 'app-dark.svg')
            ->willReturn('icon-url');
        $notification->expects($this->once())
            ->method('setIcon')
            ->with('icon-url')
            ->willReturnSelf();
        $return = $this->notifier->prepare($notification, 'en');

        $this->assertEquals($notification, $return);
    }
}