From d13f6eb2292046408b7f60f8b94d483a3be1733e Mon Sep 17 00:00:00 2001 From: Matthias Held Date: Thu, 13 Sep 2018 23:30:16 +0200 Subject: Add tests Signed-off-by: Matthias Held --- tests/Unit/Settings/AdminSectionTest.php | 75 +++++++++++++++++++++++++++ tests/Unit/Settings/AdminTest.php | 24 +++++++-- tests/Unit/Settings/PersonalSectionTest.php | 75 +++++++++++++++++++++++++++ tests/Unit/Settings/PersonalTest.php | 78 +++++++++++++++++++++++++++++ 4 files changed, 249 insertions(+), 3 deletions(-) create mode 100644 tests/Unit/Settings/AdminSectionTest.php create mode 100644 tests/Unit/Settings/PersonalSectionTest.php create mode 100644 tests/Unit/Settings/PersonalTest.php (limited to 'tests/Unit') diff --git a/tests/Unit/Settings/AdminSectionTest.php b/tests/Unit/Settings/AdminSectionTest.php new file mode 100644 index 0000000..d2b32a4 --- /dev/null +++ b/tests/Unit/Settings/AdminSectionTest.php @@ -0,0 +1,75 @@ + + * @author Matthias Held + * @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 . + */ + +namespace OCA\RansomwareDetection\tests\Unit\Settings; + +use OCA\RansomwareDetection\AppInfo\Application; +use OCA\RansomwareDetection\Settings\AdminSection; +use OCP\IURLGenerator; +use OCP\IL10N; +use Test\TestCase; + +class AdminSectionTest extends TestCase { + + /** @var AdminSection */ + private $adminSection; + + /** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject */ + protected $urlGenerator; + + /** @var IL10N|\PHPUnit_Framework_MockObject_MockObject */ + protected $l; + + public function setUp() { + parent::setUp(); + + $this->urlGenerator = $this->getMockForAbstractClass( IURLGenerator::class, array(), '', FALSE, TRUE, TRUE, array( 'imagePath' ) ); + $this->l = $this->getMockForAbstractClass( IL10N::class, array(), '', FALSE, TRUE, TRUE, array( 't' ) ); + + $this->adminSection = new AdminSection($this->urlGenerator, $this->l); + } + + public function testGetIcon() { + $this->urlGenerator->expects($this->once()) + ->method('imagePath') + ->with('ransomware_detection', 'app-dark.svg') + ->willReturn('app-dark.svg'); + + $this->assertSame('app-dark.svg', $this->adminSection->getIcon()); + } + + public function testGetID() { + $this->assertSame(Application::APP_ID, $this->adminSection->getID()); + } + + public function testGetName() { + $this->l->expects($this->once()) + ->method('t') + ->with('Ransomware detection') + ->willReturn('Ransomware detection'); + + $this->assertSame('Ransomware detection', $this->adminSection->getName()); + } + + public function testGetPriority() { + $this->assertSame(15, $this->adminSection->getPriority()); + } +} diff --git a/tests/Unit/Settings/AdminTest.php b/tests/Unit/Settings/AdminTest.php index 4dc7940..440445a 100644 --- a/tests/Unit/Settings/AdminTest.php +++ b/tests/Unit/Settings/AdminTest.php @@ -43,15 +43,33 @@ class AdminTest extends TestCase { $this->admin = new Admin($this->config); } - public function testGetForm() { + public function dataGetForm() + { + return [ + ['suspicionLevel' => 2, 'minimumSequenceLength' => 5, 'expireDays' => 7, 'activeSuspicionLevel' => ['code' => 2, 'name' => 'Suspicious'], 'suspicionLevels' => [['code' => 1, 'name' => 'Maybe suspicious']]], + ['suspicionLevel' => 1, 'minimumSequenceLength' => 5, 'expireDays' => 7, 'activeSuspicionLevel' => ['code' => 1, 'name' => 'Maybe suspicious'], 'suspicionLevels' => [['code' => 2, 'name' => 'Suspicious']]], + ['suspicionLevel' => 3, 'minimumSequenceLength' => 5, 'expireDays' => 7, 'activeSuspicionLevel' => [], 'suspicionLevels' => [['code' => 1, 'name' => 'Maybe suspicious'], ['code' => 2, 'name' => 'Suspicious']]], + ]; + } + + /** + * @dataProvider dataGetForm + * + * @param int $suspicionLevel + * @param int $minimumSequenceLength + * @param int $expireDays + * @param array $activeSuspicionLevel + * @param array $suspicionLevels + */ + public function testGetForm($suspicionLevel, $minimumSequenceLength, $expireDays, $activeSuspicionLevel, $suspicionLevels) { $this->config->expects($this->any()) ->method('getAppValue') ->withConsecutive([Application::APP_ID, 'suspicion_level', $this->anything()], [Application::APP_ID, 'minimum_sequence_length', $this->anything()], [Application::APP_ID, 'expire_days', $this->anything()]) - ->willReturnOnConsecutiveCalls(2, 5, 7); + ->willReturnOnConsecutiveCalls($suspicionLevel, $minimumSequenceLength, $expireDays); $expected = new TemplateResponse(Application::APP_ID, 'admin', - ['minimum_sequence_length' => 5, 'active_suspicion_level' => ['code' => 2, 'name' => 'Suspicious'], 'suspicion_levels' => [['code' => 1, 'name' => 'Maybe suspicious']], 'expire_days' => 7], ''); + ['minimum_sequence_length' => $minimumSequenceLength, 'active_suspicion_level' => $activeSuspicionLevel, 'suspicion_levels' => $suspicionLevels, 'expire_days' => $expireDays], ''); $this->assertEquals($expected, $this->admin->getForm()); } diff --git a/tests/Unit/Settings/PersonalSectionTest.php b/tests/Unit/Settings/PersonalSectionTest.php new file mode 100644 index 0000000..3e04a18 --- /dev/null +++ b/tests/Unit/Settings/PersonalSectionTest.php @@ -0,0 +1,75 @@ + + * @author Matthias Held + * @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 . + */ + +namespace OCA\RansomwareDetection\tests\Unit\Settings; + +use OCA\RansomwareDetection\AppInfo\Application; +use OCA\RansomwareDetection\Settings\PersonalSection; +use OCP\IURLGenerator; +use OCP\IL10N; +use Test\TestCase; + +class PersonalSectionTest extends TestCase { + + /** @var PersonalSection */ + private $personalSection; + + /** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject */ + protected $urlGenerator; + + /** @var IL10N|\PHPUnit_Framework_MockObject_MockObject */ + protected $l; + + public function setUp() { + parent::setUp(); + + $this->urlGenerator = $this->getMockForAbstractClass( IURLGenerator::class, array(), '', FALSE, TRUE, TRUE, array( 'imagePath' ) ); + $this->l = $this->getMockForAbstractClass( IL10N::class, array(), '', FALSE, TRUE, TRUE, array( 't' ) ); + + $this->personalSection = new PersonalSection($this->urlGenerator, $this->l); + } + + public function testGetIcon() { + $this->urlGenerator->expects($this->once()) + ->method('imagePath') + ->with('ransomware_detection', 'app-dark.svg') + ->willReturn('app-dark.svg'); + + $this->assertSame('app-dark.svg', $this->personalSection->getIcon()); + } + + public function testGetID() { + $this->assertSame(Application::APP_ID, $this->personalSection->getID()); + } + + public function testGetName() { + $this->l->expects($this->once()) + ->method('t') + ->with('Ransomware detection') + ->willReturn('Ransomware detection'); + + $this->assertSame('Ransomware detection', $this->personalSection->getName()); + } + + public function testGetPriority() { + $this->assertSame(15, $this->personalSection->getPriority()); + } +} diff --git a/tests/Unit/Settings/PersonalTest.php b/tests/Unit/Settings/PersonalTest.php new file mode 100644 index 0000000..e05d47b --- /dev/null +++ b/tests/Unit/Settings/PersonalTest.php @@ -0,0 +1,78 @@ + + * @author Matthias Held + * @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 . + */ + +namespace OCA\RansomwareDetection\tests\Unit\Settings; + +use OCA\RansomwareDetection\AppInfo\Application; +use OCA\RansomwareDetection\Settings\Personal; +use OCP\AppFramework\Http\TemplateResponse; +use OCP\IConfig; +use Test\TestCase; + +class PersonalTest extends TestCase { + + /** @var Personal */ + private $personal; + + /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */ + protected $config; + + public function setUp() { + parent::setUp(); + + $this->config = $this->getMockForAbstractClass( IConfig::class, array(), '', FALSE, TRUE, TRUE, array( 'getUserValue' ) ); + + $this->personal = new Personal($this->config, 'john'); + } + + public function dataGetForm() { + return [ + ['colorMode' => 0, 'colorActive' => ['code' => 0, 'name' => 'Normal'], 'color' => ['code' => 1, 'name' => 'Color blind']], + ['colorMode' => 1, 'colorActive' => ['code' => 1, 'name' => 'Color blind'], 'color' => ['code' => 0, 'name' => 'Normal']], + ]; + } + + /** + * @dataProvider dataGetForm + * + * @param int $colorMode + * @param array $colorActive + * @param array $color + */ + public function testGetForm($colorMode, $colorActive, $color) { + $this->config->expects($this->once()) + ->method('getUserValue') + ->willReturn($colorMode); + + $expected = new TemplateResponse(Application::APP_ID, 'personal', + ['color_active' => $colorActive, 'color' => $color,], ''); + + $this->assertEquals($expected, $this->personal->getForm()); + } + + public function testGetSection() { + $this->assertSame(Application::APP_ID, $this->personal->getSection()); + } + + public function testGetPriority() { + $this->assertSame(40, $this->personal->getPriority()); + } +} -- cgit v1.2.3