config = $this->createMock(IConfig::class); $this->logger = $this->createMock(LoggerInterface::class); $this->l10n = $this->createMock(IL10N::class); $this->view = $this->createMock(View::class); $this->util = $this->createMock(Util::class); $this->arrayCache = $this->createMock(ArrayCache::class); $this->manager = new Manager($this->config, $this->logger, $this->l10n, $this->view, $this->util, $this->arrayCache); } public function testManagerIsDisabled(): void { $this->assertFalse($this->manager->isEnabled()); } public function testManagerIsDisabledIfEnabledButNoModules(): void { $this->config->expects($this->any())->method('getAppValue')->willReturn(true); $this->assertFalse($this->manager->isEnabled()); } public function testManagerIsDisabledIfDisabledButModules(): void { $this->config->expects($this->any())->method('getAppValue')->willReturn(false); $em = $this->createMock(IEncryptionModule::class); $em->expects($this->any())->method('getId')->willReturn('id'); $em->expects($this->any())->method('getDisplayName')->willReturn('TestDummyModule0'); $this->manager->registerEncryptionModule('id', 'TestDummyModule0', function () use ($em) { return $em; }); $this->assertFalse($this->manager->isEnabled()); } public function testManagerIsEnabled(): void { $this->config->expects($this->any())->method('getSystemValueBool')->willReturn(true); $this->config->expects($this->any())->method('getAppValue')->willReturn('yes'); $this->assertTrue($this->manager->isEnabled()); } public function testModuleRegistration() { $this->config->expects($this->any())->method('getAppValue')->willReturn('yes'); $this->addNewEncryptionModule($this->manager, 0); $this->assertCount(1, $this->manager->getEncryptionModules()); return $this->manager; } /** * @depends testModuleRegistration */ public function testModuleReRegistration($manager): void { $this->expectException(\OC\Encryption\Exceptions\ModuleAlreadyExistsException::class); $this->expectExceptionMessage('Id "ID0" already used by encryption module "TestDummyModule0"'); $this->addNewEncryptionModule($manager, 0); } public function testModuleUnRegistration(): void { $this->config->expects($this->any())->method('getAppValue')->willReturn(true); $this->addNewEncryptionModule($this->manager, 0); $this->assertCount(1, $this->manager->getEncryptionModules()); $this->manager->unregisterEncryptionModule('ID0'); $this->assertEmpty($this->manager->getEncryptionModules()); } public function testGetEncryptionModuleUnknown(): void { $this->expectException(\OC\Encryption\Exceptions\ModuleDoesNotExistsException::class); $this->expectExceptionMessage('Module with ID: unknown does not exist.'); $this->config->expects($this->any())->method('getAppValue')->willReturn(true); $this->addNewEncryptionModule($this->manager, 0); $this->assertCount(1, $this->manager->getEncryptionModules()); $this->manager->getEncryptionModule('unknown'); } public function testGetEncryptionModuleEmpty(): void { global $defaultId; $defaultId = null; $this->config->expects($this->any()) ->method('getAppValue') ->with('core', 'default_encryption_module') ->willReturnCallback(function () { global $defaultId; return $defaultId; }); $this->addNewEncryptionModule($this->manager, 0); $this->assertCount(1, $this->manager->getEncryptionModules()); $this->addNewEncryptionModule($this->manager, 1); $this->assertCount(2, $this->manager->getEncryptionModules()); // Should return the default module $defaultId = 'ID0'; $this->assertEquals('ID0', $this->manager->getEncryptionModule()->getId()); $defaultId = 'ID1'; $this->assertEquals('ID1', $this->manager->getEncryptionModule()->getId()); } public function testGetEncryptionModule(): void { global $defaultId; $defaultId = null; $this->config->expects($this->any()) ->method('getAppValue') ->with('core', 'default_encryption_module') ->willReturnCallback(function () { global $defaultId; return $defaultId; }); $this->addNewEncryptionModule($this->manager, 0); $defaultId = 'ID0'; $this->assertCount(1, $this->manager->getEncryptionModules()); $en0 = $this->manager->getEncryptionModule('ID0'); $this->assertEquals('ID0', $en0->getId()); $en0 = self::invokePrivate($this->manager, 'getDefaultEncryptionModule'); $this->assertEquals('ID0', $en0->getId()); $this->assertEquals('ID0', $this->manager->getDefaultEncryptionModuleId()); } public function testSetDefaultEncryptionModule(): void { global $defaultId; $defaultId = null; $this->config->expects($this->any()) ->method('getAppValue') ->with('core', 'default_encryption_module') ->willReturnCallback(function () { global $defaultId; return $defaultId; }); $this->addNewEncryptionModule($this->manager, 0); $this->assertCount(1, $this->manager->getEncryptionModules()); $this->addNewEncryptionModule($this->manager, 1); $this->assertCount(2, $this->manager->getEncryptionModules()); // Default module is the first we set $defaultId = 'ID0'; $this->assertEquals('ID0', $this->manager->getDefaultEncryptionModuleId()); // Set to an existing module $this->config->expects($this->once()) ->method('setAppValue') ->with('core', 'default_encryption_module', 'ID1'); $this->assertTrue($this->manager->setDefaultEncryptionModule('ID1')); $defaultId = 'ID1'; $this->assertEquals('ID1', $this->manager->getDefaultEncryptionModuleId()); // Set to an unexisting module $this->assertFalse($this->manager->setDefaultEncryptionModule('ID2')); $this->assertEquals('ID1', $this->manager->getDefaultEncryptionModuleId()); } // /** // * @expectedException \OC\Encryption\Exceptions\ModuleAlreadyExistsException // * @expectedExceptionMessage Id "0" already used by encryption module "TestDummyModule0" // */ // public function testModuleRegistration() { // $config = $this->createMock(IConfig::class); // $config->expects($this->any())->method('getSystemValueBool')->willReturn(true); // $em = $this->createMock(IEncryptionModule::class); // $em->expects($this->any())->method('getId')->willReturn(0); // $em->expects($this->any())->method('getDisplayName')->willReturn('TestDummyModule0'); // $m = new Manager($config); // $m->registerEncryptionModule($em); // $this->assertTrue($m->isEnabled()); // $m->registerEncryptionModule($em); // } // // public function testModuleUnRegistration() { // $config = $this->createMock(IConfig::class); // $config->expects($this->any())->method('getSystemValueBool')->willReturn(true); // $em = $this->createMock(IEncryptionModule::class); // $em->expects($this->any())->method('getId')->willReturn(0); // $em->expects($this->any())->method('getDisplayName')->willReturn('TestDummyModule0'); // $m = new Manager($config); // $m->registerEncryptionModule($em); // $this->assertTrue($m->isEnabled()); // $m->unregisterEncryptionModule($em); // $this->assertFalse($m->isEnabled()); // } // // /** // * @expectedException \OC\Encryption\Exceptions\ModuleDoesNotExistsException // * @expectedExceptionMessage Module with ID: unknown does not exist. // */ // public function testGetEncryptionModuleUnknown() { // $config = $this->createMock(IConfig::class); // $config->expects($this->any())->method('getSystemValueBool')->willReturn(true); // $em = $this->createMock(IEncryptionModule::class); // $em->expects($this->any())->method('getId')->willReturn(0); // $em->expects($this->any())->method('getDisplayName')->willReturn('TestDummyModule0'); // $m = new Manager($config); // $m->registerEncryptionModule($em); // $this->assertTrue($m->isEnabled()); // $m->getEncryptionModule('unknown'); // } // // public function testGetEncryptionModule() { // $config = $this->createMock(IConfig::class); // $config->expects($this->any())->method('getSystemValueBool')->willReturn(true); // $em = $this->createMock(IEncryptionModule::class); // $em->expects($this->any())->method('getId')->willReturn(0); // $em->expects($this->any())->method('getDisplayName')->willReturn('TestDummyModule0'); // $m = new Manager($config); // $m->registerEncryptionModule($em); // $this->assertTrue($m->isEnabled()); // $en0 = $m->getEncryptionModule(0); // $this->assertEquals(0, $en0->getId()); // } protected function addNewEncryptionModule(Manager $manager, $id) { $encryptionModule = $this->createMock(IEncryptionModule::class); $encryptionModule->expects($this->any()) ->method('getId') ->willReturn('ID' . $id); $encryptionModule->expects($this->any()) ->method('getDisplayName') ->willReturn('TestDummyModule' . $id); /** @var \OCP\Encryption\IEncryptionModule $encryptionModule */ $manager->registerEncryptionModule('ID' . $id, 'TestDummyModule' . $id, function () use ($encryptionModule) { return $encryptionModule; }); } } > 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