@@ -20,117 +20,117 @@ |
||
| 20 | 20 | use function array_shift; |
| 21 | 21 | |
| 22 | 22 | class Registry implements IRegistry { |
| 23 | - /** @var string[] */ |
|
| 24 | - private $lazyReporters = []; |
|
| 25 | - |
|
| 26 | - /** @var IReporter[] */ |
|
| 27 | - private $reporters = []; |
|
| 28 | - |
|
| 29 | - /** @var IServerContainer */ |
|
| 30 | - private $serverContainer; |
|
| 31 | - |
|
| 32 | - public function __construct(IServerContainer $serverContainer) { |
|
| 33 | - $this->serverContainer = $serverContainer; |
|
| 34 | - } |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * Register a reporter instance |
|
| 38 | - * |
|
| 39 | - * @param IReporter $reporter |
|
| 40 | - */ |
|
| 41 | - public function register(IReporter $reporter): void { |
|
| 42 | - $this->reporters[] = $reporter; |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - public function registerLazy(string $class): void { |
|
| 46 | - $this->lazyReporters[] = $class; |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * Delegate breadcrumb collection to all registered reporters |
|
| 51 | - * |
|
| 52 | - * @param string $message |
|
| 53 | - * @param string $category |
|
| 54 | - * @param array $context |
|
| 55 | - * |
|
| 56 | - * @since 15.0.0 |
|
| 57 | - */ |
|
| 58 | - public function delegateBreadcrumb(string $message, string $category, array $context = []): void { |
|
| 59 | - $this->loadLazyProviders(); |
|
| 60 | - |
|
| 61 | - foreach ($this->reporters as $reporter) { |
|
| 62 | - if ($reporter instanceof ICollectBreadcrumbs) { |
|
| 63 | - $reporter->collect($message, $category, $context); |
|
| 64 | - } |
|
| 65 | - } |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * Delegate crash reporting to all registered reporters |
|
| 70 | - * |
|
| 71 | - * @param Exception|Throwable $exception |
|
| 72 | - * @param array $context |
|
| 73 | - */ |
|
| 74 | - public function delegateReport($exception, array $context = []): void { |
|
| 75 | - $this->loadLazyProviders(); |
|
| 76 | - |
|
| 77 | - foreach ($this->reporters as $reporter) { |
|
| 78 | - $reporter->report($exception, $context); |
|
| 79 | - } |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * Delegate a message to all reporters that implement IMessageReporter |
|
| 84 | - * |
|
| 85 | - * @param string $message |
|
| 86 | - * @param array $context |
|
| 87 | - * |
|
| 88 | - * @return void |
|
| 89 | - */ |
|
| 90 | - public function delegateMessage(string $message, array $context = []): void { |
|
| 91 | - $this->loadLazyProviders(); |
|
| 92 | - |
|
| 93 | - foreach ($this->reporters as $reporter) { |
|
| 94 | - if ($reporter instanceof IMessageReporter) { |
|
| 95 | - $reporter->reportMessage($message, $context); |
|
| 96 | - } |
|
| 97 | - } |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - private function loadLazyProviders(): void { |
|
| 101 | - while (($class = array_shift($this->lazyReporters)) !== null) { |
|
| 102 | - try { |
|
| 103 | - /** @var IReporter $reporter */ |
|
| 104 | - $reporter = $this->serverContainer->query($class); |
|
| 105 | - } catch (QueryException $e) { |
|
| 106 | - /* |
|
| 23 | + /** @var string[] */ |
|
| 24 | + private $lazyReporters = []; |
|
| 25 | + |
|
| 26 | + /** @var IReporter[] */ |
|
| 27 | + private $reporters = []; |
|
| 28 | + |
|
| 29 | + /** @var IServerContainer */ |
|
| 30 | + private $serverContainer; |
|
| 31 | + |
|
| 32 | + public function __construct(IServerContainer $serverContainer) { |
|
| 33 | + $this->serverContainer = $serverContainer; |
|
| 34 | + } |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * Register a reporter instance |
|
| 38 | + * |
|
| 39 | + * @param IReporter $reporter |
|
| 40 | + */ |
|
| 41 | + public function register(IReporter $reporter): void { |
|
| 42 | + $this->reporters[] = $reporter; |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + public function registerLazy(string $class): void { |
|
| 46 | + $this->lazyReporters[] = $class; |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * Delegate breadcrumb collection to all registered reporters |
|
| 51 | + * |
|
| 52 | + * @param string $message |
|
| 53 | + * @param string $category |
|
| 54 | + * @param array $context |
|
| 55 | + * |
|
| 56 | + * @since 15.0.0 |
|
| 57 | + */ |
|
| 58 | + public function delegateBreadcrumb(string $message, string $category, array $context = []): void { |
|
| 59 | + $this->loadLazyProviders(); |
|
| 60 | + |
|
| 61 | + foreach ($this->reporters as $reporter) { |
|
| 62 | + if ($reporter instanceof ICollectBreadcrumbs) { |
|
| 63 | + $reporter->collect($message, $category, $context); |
|
| 64 | + } |
|
| 65 | + } |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * Delegate crash reporting to all registered reporters |
|
| 70 | + * |
|
| 71 | + * @param Exception|Throwable $exception |
|
| 72 | + * @param array $context |
|
| 73 | + */ |
|
| 74 | + public function delegateReport($exception, array $context = []): void { |
|
| 75 | + $this->loadLazyProviders(); |
|
| 76 | + |
|
| 77 | + foreach ($this->reporters as $reporter) { |
|
| 78 | + $reporter->report($exception, $context); |
|
| 79 | + } |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * Delegate a message to all reporters that implement IMessageReporter |
|
| 84 | + * |
|
| 85 | + * @param string $message |
|
| 86 | + * @param array $context |
|
| 87 | + * |
|
| 88 | + * @return void |
|
| 89 | + */ |
|
| 90 | + public function delegateMessage(string $message, array $context = []): void { |
|
| 91 | + $this->loadLazyProviders(); |
|
| 92 | + |
|
| 93 | + foreach ($this->reporters as $reporter) { |
|
| 94 | + if ($reporter instanceof IMessageReporter) { |
|
| 95 | + $reporter->reportMessage($message, $context); |
|
| 96 | + } |
|
| 97 | + } |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + private function loadLazyProviders(): void { |
|
| 101 | + while (($class = array_shift($this->lazyReporters)) !== null) { |
|
| 102 | + try { |
|
| 103 | + /** @var IReporter $reporter */ |
|
| 104 | + $reporter = $this->serverContainer->query($class); |
|
| 105 | + } catch (QueryException $e) { |
|
| 106 | + /* |
|
| 107 | 107 | * There is a circular dependency between the logger and the registry, so |
| 108 | 108 | * we can not inject it. Thus the static call. |
| 109 | 109 | */ |
| 110 | - \OC::$server->get(LoggerInterface::class)->critical('Could not load lazy crash reporter: ' . $e->getMessage(), [ |
|
| 111 | - 'exception' => $e, |
|
| 112 | - ]); |
|
| 113 | - return; |
|
| 114 | - } |
|
| 115 | - /** |
|
| 116 | - * Try to register the loaded reporter. Theoretically it could be of a wrong |
|
| 117 | - * type, so we might get a TypeError here that we should catch. |
|
| 118 | - */ |
|
| 119 | - try { |
|
| 120 | - $this->register($reporter); |
|
| 121 | - } catch (Throwable $e) { |
|
| 122 | - /* |
|
| 110 | + \OC::$server->get(LoggerInterface::class)->critical('Could not load lazy crash reporter: ' . $e->getMessage(), [ |
|
| 111 | + 'exception' => $e, |
|
| 112 | + ]); |
|
| 113 | + return; |
|
| 114 | + } |
|
| 115 | + /** |
|
| 116 | + * Try to register the loaded reporter. Theoretically it could be of a wrong |
|
| 117 | + * type, so we might get a TypeError here that we should catch. |
|
| 118 | + */ |
|
| 119 | + try { |
|
| 120 | + $this->register($reporter); |
|
| 121 | + } catch (Throwable $e) { |
|
| 122 | + /* |
|
| 123 | 123 | * There is a circular dependency between the logger and the registry, so |
| 124 | 124 | * we can not inject it. Thus the static call. |
| 125 | 125 | */ |
| 126 | - \OC::$server->get(LoggerInterface::class)->critical('Could not register lazy crash reporter: ' . $e->getMessage(), [ |
|
| 127 | - 'exception' => $e, |
|
| 128 | - ]); |
|
| 129 | - } |
|
| 130 | - } |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - public function hasReporters(): bool { |
|
| 134 | - return !empty($this->lazyReporters) || !empty($this->reporters); |
|
| 135 | - } |
|
| 126 | + \OC::$server->get(LoggerInterface::class)->critical('Could not register lazy crash reporter: ' . $e->getMessage(), [ |
|
| 127 | + 'exception' => $e, |
|
| 128 | + ]); |
|
| 129 | + } |
|
| 130 | + } |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + public function hasReporters(): bool { |
|
| 134 | + return !empty($this->lazyReporters) || !empty($this->reporters); |
|
| 135 | + } |
|
| 136 | 136 | } |
@@ -107,7 +107,7 @@ discard block |
||
| 107 | 107 | * There is a circular dependency between the logger and the registry, so |
| 108 | 108 | * we can not inject it. Thus the static call. |
| 109 | 109 | */ |
| 110 | - \OC::$server->get(LoggerInterface::class)->critical('Could not load lazy crash reporter: ' . $e->getMessage(), [ |
|
| 110 | + \OC::$server->get(LoggerInterface::class)->critical('Could not load lazy crash reporter: '.$e->getMessage(), [ |
|
| 111 | 111 | 'exception' => $e, |
| 112 | 112 | ]); |
| 113 | 113 | return; |
@@ -123,7 +123,7 @@ discard block |
||
| 123 | 123 | * There is a circular dependency between the logger and the registry, so |
| 124 | 124 | * we can not inject it. Thus the static call. |
| 125 | 125 | */ |
| 126 | - \OC::$server->get(LoggerInterface::class)->critical('Could not register lazy crash reporter: ' . $e->getMessage(), [ |
|
| 126 | + \OC::$server->get(LoggerInterface::class)->critical('Could not register lazy crash reporter: '.$e->getMessage(), [ |
|
| 127 | 127 | 'exception' => $e, |
| 128 | 128 | ]); |
| 129 | 129 | } |
@@ -22,153 +22,153 @@ |
||
| 22 | 22 | * @group DB |
| 23 | 23 | */ |
| 24 | 24 | class WebhookListenerMapperTest extends TestCase { |
| 25 | - private IDBConnection $connection; |
|
| 26 | - private WebhookListenerMapper $mapper; |
|
| 27 | - private ICacheFactory $cacheFactory; |
|
| 28 | - |
|
| 29 | - protected function setUp(): void { |
|
| 30 | - parent::setUp(); |
|
| 31 | - |
|
| 32 | - $this->connection = Server::get(IDBConnection::class); |
|
| 33 | - $this->cacheFactory = Server::get(ICacheFactory::class); |
|
| 34 | - $this->pruneTables(); |
|
| 35 | - |
|
| 36 | - $this->mapper = new WebhookListenerMapper( |
|
| 37 | - $this->connection, |
|
| 38 | - $this->cacheFactory, |
|
| 39 | - ); |
|
| 40 | - } |
|
| 41 | - |
|
| 42 | - protected function tearDown(): void { |
|
| 43 | - $this->pruneTables(); |
|
| 44 | - parent::tearDown(); |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - protected function pruneTables(): void { |
|
| 48 | - $query = $this->connection->getQueryBuilder(); |
|
| 49 | - $query->delete(WebhookListenerMapper::TABLE_NAME)->executeStatement(); |
|
| 50 | - } |
|
| 51 | - |
|
| 52 | - public function testInsertListenerWithNotSupportedEvent(): void { |
|
| 53 | - $this->expectException(\UnexpectedValueException::class); |
|
| 54 | - $listener1 = $this->mapper->addWebhookListener( |
|
| 55 | - null, |
|
| 56 | - 'bob', |
|
| 57 | - 'POST', |
|
| 58 | - 'https://webhook.example.com/endpoint', |
|
| 59 | - UserCreatedEvent::class, |
|
| 60 | - null, |
|
| 61 | - null, |
|
| 62 | - null, |
|
| 63 | - AuthMethod::None, |
|
| 64 | - null, |
|
| 65 | - ); |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - public function testInsertListenerAndGetIt(): void { |
|
| 69 | - $listener1 = $this->mapper->addWebhookListener( |
|
| 70 | - null, |
|
| 71 | - 'bob', |
|
| 72 | - 'POST', |
|
| 73 | - 'https://webhook.example.com/endpoint', |
|
| 74 | - NodeWrittenEvent::class, |
|
| 75 | - null, |
|
| 76 | - null, |
|
| 77 | - null, |
|
| 78 | - AuthMethod::None, |
|
| 79 | - null, |
|
| 80 | - ); |
|
| 81 | - |
|
| 82 | - $listener2 = $this->mapper->getById($listener1->getId()); |
|
| 83 | - |
|
| 84 | - $listener1->resetUpdatedFields(); |
|
| 85 | - $this->assertEquals($listener1, $listener2); |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - public function testInsertListenerAndGetItByUri(): void { |
|
| 89 | - $uri = 'https://webhook.example.com/endpoint'; |
|
| 90 | - $listener1 = $this->mapper->addWebhookListener( |
|
| 91 | - null, |
|
| 92 | - 'bob', |
|
| 93 | - 'POST', |
|
| 94 | - $uri, |
|
| 95 | - NodeWrittenEvent::class, |
|
| 96 | - null, |
|
| 97 | - null, |
|
| 98 | - null, |
|
| 99 | - AuthMethod::None, |
|
| 100 | - null, |
|
| 101 | - ); |
|
| 102 | - |
|
| 103 | - $listeners = $this->mapper->getByUri($uri); |
|
| 104 | - |
|
| 105 | - $listener1->resetUpdatedFields(); |
|
| 106 | - $this->assertContains($listener1->getId(), array_map(fn ($listener) => $listener->getId(), $listeners)); |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - public function testInsertListenerAndGetItWithAuthData(): void { |
|
| 110 | - $listener1 = $this->mapper->addWebhookListener( |
|
| 111 | - null, |
|
| 112 | - 'bob', |
|
| 113 | - 'POST', |
|
| 114 | - 'https://webhook.example.com/endpoint', |
|
| 115 | - NodeWrittenEvent::class, |
|
| 116 | - null, |
|
| 117 | - null, |
|
| 118 | - null, |
|
| 119 | - AuthMethod::Header, |
|
| 120 | - ['secretHeader' => 'header'], |
|
| 121 | - ); |
|
| 122 | - |
|
| 123 | - $listener2 = $this->mapper->getById($listener1->getId()); |
|
| 124 | - |
|
| 125 | - $listener1->resetUpdatedFields(); |
|
| 126 | - $this->assertEquals($listener1, $listener2); |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - public function testInsertListenerAndGetItByEventAndUser(): void { |
|
| 130 | - $listener1 = $this->mapper->addWebhookListener( |
|
| 131 | - null, |
|
| 132 | - 'bob', |
|
| 133 | - 'POST', |
|
| 134 | - 'https://webhook.example.com/endpoint', |
|
| 135 | - NodeWrittenEvent::class, |
|
| 136 | - null, |
|
| 137 | - 'alice', |
|
| 138 | - null, |
|
| 139 | - AuthMethod::None, |
|
| 140 | - null, |
|
| 141 | - ); |
|
| 142 | - $listener1->resetUpdatedFields(); |
|
| 143 | - |
|
| 144 | - $this->assertEquals([NodeWrittenEvent::class], $this->mapper->getAllConfiguredEvents('alice')); |
|
| 145 | - $this->assertEquals([], $this->mapper->getAllConfiguredEvents('')); |
|
| 146 | - $this->assertEquals([], $this->mapper->getAllConfiguredEvents('otherUser')); |
|
| 147 | - |
|
| 148 | - $this->assertEquals([$listener1], $this->mapper->getByEvent(NodeWrittenEvent::class, 'alice')); |
|
| 149 | - $this->assertEquals([], $this->mapper->getByEvent(NodeWrittenEvent::class, '')); |
|
| 150 | - $this->assertEquals([], $this->mapper->getByEvent(NodeWrittenEvent::class, 'otherUser')); |
|
| 151 | - |
|
| 152 | - /* Add a second listener with no user filter */ |
|
| 153 | - $listener2 = $this->mapper->addWebhookListener( |
|
| 154 | - null, |
|
| 155 | - 'bob', |
|
| 156 | - 'POST', |
|
| 157 | - 'https://webhook.example.com/endpoint', |
|
| 158 | - NodeWrittenEvent::class, |
|
| 159 | - null, |
|
| 160 | - '', |
|
| 161 | - null, |
|
| 162 | - AuthMethod::None, |
|
| 163 | - null, |
|
| 164 | - ); |
|
| 165 | - $listener2->resetUpdatedFields(); |
|
| 166 | - |
|
| 167 | - $this->assertEquals([NodeWrittenEvent::class], $this->mapper->getAllConfiguredEvents('alice')); |
|
| 168 | - $this->assertEquals([NodeWrittenEvent::class], $this->mapper->getAllConfiguredEvents('')); |
|
| 169 | - |
|
| 170 | - $this->assertEquals([$listener1, $listener2], $this->mapper->getByEvent(NodeWrittenEvent::class, 'alice')); |
|
| 171 | - $this->assertEquals([$listener2], $this->mapper->getByEvent(NodeWrittenEvent::class, 'otherUser')); |
|
| 172 | - $this->assertEquals([$listener2], $this->mapper->getByEvent(NodeWrittenEvent::class)); |
|
| 173 | - } |
|
| 25 | + private IDBConnection $connection; |
|
| 26 | + private WebhookListenerMapper $mapper; |
|
| 27 | + private ICacheFactory $cacheFactory; |
|
| 28 | + |
|
| 29 | + protected function setUp(): void { |
|
| 30 | + parent::setUp(); |
|
| 31 | + |
|
| 32 | + $this->connection = Server::get(IDBConnection::class); |
|
| 33 | + $this->cacheFactory = Server::get(ICacheFactory::class); |
|
| 34 | + $this->pruneTables(); |
|
| 35 | + |
|
| 36 | + $this->mapper = new WebhookListenerMapper( |
|
| 37 | + $this->connection, |
|
| 38 | + $this->cacheFactory, |
|
| 39 | + ); |
|
| 40 | + } |
|
| 41 | + |
|
| 42 | + protected function tearDown(): void { |
|
| 43 | + $this->pruneTables(); |
|
| 44 | + parent::tearDown(); |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + protected function pruneTables(): void { |
|
| 48 | + $query = $this->connection->getQueryBuilder(); |
|
| 49 | + $query->delete(WebhookListenerMapper::TABLE_NAME)->executeStatement(); |
|
| 50 | + } |
|
| 51 | + |
|
| 52 | + public function testInsertListenerWithNotSupportedEvent(): void { |
|
| 53 | + $this->expectException(\UnexpectedValueException::class); |
|
| 54 | + $listener1 = $this->mapper->addWebhookListener( |
|
| 55 | + null, |
|
| 56 | + 'bob', |
|
| 57 | + 'POST', |
|
| 58 | + 'https://webhook.example.com/endpoint', |
|
| 59 | + UserCreatedEvent::class, |
|
| 60 | + null, |
|
| 61 | + null, |
|
| 62 | + null, |
|
| 63 | + AuthMethod::None, |
|
| 64 | + null, |
|
| 65 | + ); |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + public function testInsertListenerAndGetIt(): void { |
|
| 69 | + $listener1 = $this->mapper->addWebhookListener( |
|
| 70 | + null, |
|
| 71 | + 'bob', |
|
| 72 | + 'POST', |
|
| 73 | + 'https://webhook.example.com/endpoint', |
|
| 74 | + NodeWrittenEvent::class, |
|
| 75 | + null, |
|
| 76 | + null, |
|
| 77 | + null, |
|
| 78 | + AuthMethod::None, |
|
| 79 | + null, |
|
| 80 | + ); |
|
| 81 | + |
|
| 82 | + $listener2 = $this->mapper->getById($listener1->getId()); |
|
| 83 | + |
|
| 84 | + $listener1->resetUpdatedFields(); |
|
| 85 | + $this->assertEquals($listener1, $listener2); |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + public function testInsertListenerAndGetItByUri(): void { |
|
| 89 | + $uri = 'https://webhook.example.com/endpoint'; |
|
| 90 | + $listener1 = $this->mapper->addWebhookListener( |
|
| 91 | + null, |
|
| 92 | + 'bob', |
|
| 93 | + 'POST', |
|
| 94 | + $uri, |
|
| 95 | + NodeWrittenEvent::class, |
|
| 96 | + null, |
|
| 97 | + null, |
|
| 98 | + null, |
|
| 99 | + AuthMethod::None, |
|
| 100 | + null, |
|
| 101 | + ); |
|
| 102 | + |
|
| 103 | + $listeners = $this->mapper->getByUri($uri); |
|
| 104 | + |
|
| 105 | + $listener1->resetUpdatedFields(); |
|
| 106 | + $this->assertContains($listener1->getId(), array_map(fn ($listener) => $listener->getId(), $listeners)); |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + public function testInsertListenerAndGetItWithAuthData(): void { |
|
| 110 | + $listener1 = $this->mapper->addWebhookListener( |
|
| 111 | + null, |
|
| 112 | + 'bob', |
|
| 113 | + 'POST', |
|
| 114 | + 'https://webhook.example.com/endpoint', |
|
| 115 | + NodeWrittenEvent::class, |
|
| 116 | + null, |
|
| 117 | + null, |
|
| 118 | + null, |
|
| 119 | + AuthMethod::Header, |
|
| 120 | + ['secretHeader' => 'header'], |
|
| 121 | + ); |
|
| 122 | + |
|
| 123 | + $listener2 = $this->mapper->getById($listener1->getId()); |
|
| 124 | + |
|
| 125 | + $listener1->resetUpdatedFields(); |
|
| 126 | + $this->assertEquals($listener1, $listener2); |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + public function testInsertListenerAndGetItByEventAndUser(): void { |
|
| 130 | + $listener1 = $this->mapper->addWebhookListener( |
|
| 131 | + null, |
|
| 132 | + 'bob', |
|
| 133 | + 'POST', |
|
| 134 | + 'https://webhook.example.com/endpoint', |
|
| 135 | + NodeWrittenEvent::class, |
|
| 136 | + null, |
|
| 137 | + 'alice', |
|
| 138 | + null, |
|
| 139 | + AuthMethod::None, |
|
| 140 | + null, |
|
| 141 | + ); |
|
| 142 | + $listener1->resetUpdatedFields(); |
|
| 143 | + |
|
| 144 | + $this->assertEquals([NodeWrittenEvent::class], $this->mapper->getAllConfiguredEvents('alice')); |
|
| 145 | + $this->assertEquals([], $this->mapper->getAllConfiguredEvents('')); |
|
| 146 | + $this->assertEquals([], $this->mapper->getAllConfiguredEvents('otherUser')); |
|
| 147 | + |
|
| 148 | + $this->assertEquals([$listener1], $this->mapper->getByEvent(NodeWrittenEvent::class, 'alice')); |
|
| 149 | + $this->assertEquals([], $this->mapper->getByEvent(NodeWrittenEvent::class, '')); |
|
| 150 | + $this->assertEquals([], $this->mapper->getByEvent(NodeWrittenEvent::class, 'otherUser')); |
|
| 151 | + |
|
| 152 | + /* Add a second listener with no user filter */ |
|
| 153 | + $listener2 = $this->mapper->addWebhookListener( |
|
| 154 | + null, |
|
| 155 | + 'bob', |
|
| 156 | + 'POST', |
|
| 157 | + 'https://webhook.example.com/endpoint', |
|
| 158 | + NodeWrittenEvent::class, |
|
| 159 | + null, |
|
| 160 | + '', |
|
| 161 | + null, |
|
| 162 | + AuthMethod::None, |
|
| 163 | + null, |
|
| 164 | + ); |
|
| 165 | + $listener2->resetUpdatedFields(); |
|
| 166 | + |
|
| 167 | + $this->assertEquals([NodeWrittenEvent::class], $this->mapper->getAllConfiguredEvents('alice')); |
|
| 168 | + $this->assertEquals([NodeWrittenEvent::class], $this->mapper->getAllConfiguredEvents('')); |
|
| 169 | + |
|
| 170 | + $this->assertEquals([$listener1, $listener2], $this->mapper->getByEvent(NodeWrittenEvent::class, 'alice')); |
|
| 171 | + $this->assertEquals([$listener2], $this->mapper->getByEvent(NodeWrittenEvent::class, 'otherUser')); |
|
| 172 | + $this->assertEquals([$listener2], $this->mapper->getByEvent(NodeWrittenEvent::class)); |
|
| 173 | + } |
|
| 174 | 174 | } |
@@ -22,63 +22,63 @@ |
||
| 22 | 22 | use Test\TestCase; |
| 23 | 23 | |
| 24 | 24 | class AdminControllerTest extends TestCase { |
| 25 | - private IRequest&MockObject $request; |
|
| 26 | - private IJobList&MockObject $jobList; |
|
| 27 | - private ISecureRandom&MockObject $secureRandom; |
|
| 28 | - private IConfig&MockObject $config; |
|
| 29 | - private ITimeFactory&MockObject $timeFactory; |
|
| 30 | - private IL10N&MockObject $l10n; |
|
| 31 | - private IAppConfig&MockObject $appConfig; |
|
| 25 | + private IRequest&MockObject $request; |
|
| 26 | + private IJobList&MockObject $jobList; |
|
| 27 | + private ISecureRandom&MockObject $secureRandom; |
|
| 28 | + private IConfig&MockObject $config; |
|
| 29 | + private ITimeFactory&MockObject $timeFactory; |
|
| 30 | + private IL10N&MockObject $l10n; |
|
| 31 | + private IAppConfig&MockObject $appConfig; |
|
| 32 | 32 | |
| 33 | - private AdminController $adminController; |
|
| 33 | + private AdminController $adminController; |
|
| 34 | 34 | |
| 35 | - protected function setUp(): void { |
|
| 36 | - parent::setUp(); |
|
| 35 | + protected function setUp(): void { |
|
| 36 | + parent::setUp(); |
|
| 37 | 37 | |
| 38 | - $this->request = $this->createMock(IRequest::class); |
|
| 39 | - $this->jobList = $this->createMock(IJobList::class); |
|
| 40 | - $this->secureRandom = $this->createMock(ISecureRandom::class); |
|
| 41 | - $this->config = $this->createMock(IConfig::class); |
|
| 42 | - $this->appConfig = $this->createMock(IAppConfig::class); |
|
| 43 | - $this->timeFactory = $this->createMock(ITimeFactory::class); |
|
| 44 | - $this->l10n = $this->createMock(IL10N::class); |
|
| 38 | + $this->request = $this->createMock(IRequest::class); |
|
| 39 | + $this->jobList = $this->createMock(IJobList::class); |
|
| 40 | + $this->secureRandom = $this->createMock(ISecureRandom::class); |
|
| 41 | + $this->config = $this->createMock(IConfig::class); |
|
| 42 | + $this->appConfig = $this->createMock(IAppConfig::class); |
|
| 43 | + $this->timeFactory = $this->createMock(ITimeFactory::class); |
|
| 44 | + $this->l10n = $this->createMock(IL10N::class); |
|
| 45 | 45 | |
| 46 | - $this->adminController = new AdminController( |
|
| 47 | - 'updatenotification', |
|
| 48 | - $this->request, |
|
| 49 | - $this->jobList, |
|
| 50 | - $this->secureRandom, |
|
| 51 | - $this->config, |
|
| 52 | - $this->appConfig, |
|
| 53 | - $this->timeFactory, |
|
| 54 | - $this->l10n |
|
| 55 | - ); |
|
| 56 | - } |
|
| 46 | + $this->adminController = new AdminController( |
|
| 47 | + 'updatenotification', |
|
| 48 | + $this->request, |
|
| 49 | + $this->jobList, |
|
| 50 | + $this->secureRandom, |
|
| 51 | + $this->config, |
|
| 52 | + $this->appConfig, |
|
| 53 | + $this->timeFactory, |
|
| 54 | + $this->l10n |
|
| 55 | + ); |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - public function testCreateCredentials(): void { |
|
| 59 | - $this->jobList |
|
| 60 | - ->expects($this->once()) |
|
| 61 | - ->method('add') |
|
| 62 | - ->with(ResetToken::class); |
|
| 63 | - $this->secureRandom |
|
| 64 | - ->expects($this->once()) |
|
| 65 | - ->method('generate') |
|
| 66 | - ->with(64) |
|
| 67 | - ->willReturn('MyGeneratedToken'); |
|
| 68 | - $this->config |
|
| 69 | - ->expects($this->once()) |
|
| 70 | - ->method('setSystemValue') |
|
| 71 | - ->with('updater.secret'); |
|
| 72 | - $this->timeFactory |
|
| 73 | - ->expects($this->once()) |
|
| 74 | - ->method('getTime') |
|
| 75 | - ->willReturn(12345); |
|
| 76 | - $this->appConfig |
|
| 77 | - ->expects($this->once()) |
|
| 78 | - ->method('setValueInt') |
|
| 79 | - ->with('core', 'updater.secret.created', 12345); |
|
| 58 | + public function testCreateCredentials(): void { |
|
| 59 | + $this->jobList |
|
| 60 | + ->expects($this->once()) |
|
| 61 | + ->method('add') |
|
| 62 | + ->with(ResetToken::class); |
|
| 63 | + $this->secureRandom |
|
| 64 | + ->expects($this->once()) |
|
| 65 | + ->method('generate') |
|
| 66 | + ->with(64) |
|
| 67 | + ->willReturn('MyGeneratedToken'); |
|
| 68 | + $this->config |
|
| 69 | + ->expects($this->once()) |
|
| 70 | + ->method('setSystemValue') |
|
| 71 | + ->with('updater.secret'); |
|
| 72 | + $this->timeFactory |
|
| 73 | + ->expects($this->once()) |
|
| 74 | + ->method('getTime') |
|
| 75 | + ->willReturn(12345); |
|
| 76 | + $this->appConfig |
|
| 77 | + ->expects($this->once()) |
|
| 78 | + ->method('setValueInt') |
|
| 79 | + ->with('core', 'updater.secret.created', 12345); |
|
| 80 | 80 | |
| 81 | - $expected = new DataResponse('MyGeneratedToken'); |
|
| 82 | - $this->assertEquals($expected, $this->adminController->createCredentials()); |
|
| 83 | - } |
|
| 81 | + $expected = new DataResponse('MyGeneratedToken'); |
|
| 82 | + $this->assertEquals($expected, $this->adminController->createCredentials()); |
|
| 83 | + } |
|
| 84 | 84 | } |
@@ -29,441 +29,441 @@ |
||
| 29 | 29 | use Test\TestCase; |
| 30 | 30 | |
| 31 | 31 | class AdminTest extends TestCase { |
| 32 | - private IFactory&MockObject $l10nFactory; |
|
| 33 | - private IConfig&MockObject $config; |
|
| 34 | - private IAppConfig&MockObject $appConfig; |
|
| 35 | - private UpdateChecker&MockObject $updateChecker; |
|
| 36 | - private IGroupManager&MockObject $groupManager; |
|
| 37 | - private IDateTimeFormatter&MockObject $dateTimeFormatter; |
|
| 38 | - private IRegistry&MockObject $subscriptionRegistry; |
|
| 39 | - private IUserManager&MockObject $userManager; |
|
| 40 | - private LoggerInterface&MockObject $logger; |
|
| 41 | - private IInitialState&MockObject $initialState; |
|
| 42 | - private ServerVersion&MockObject $serverVersion; |
|
| 43 | - private Admin $admin; |
|
| 44 | - |
|
| 45 | - protected function setUp(): void { |
|
| 46 | - parent::setUp(); |
|
| 47 | - |
|
| 48 | - $this->config = $this->createMock(IConfig::class); |
|
| 49 | - $this->appConfig = $this->createMock(IAppConfig::class); |
|
| 50 | - $this->updateChecker = $this->createMock(UpdateChecker::class); |
|
| 51 | - $this->groupManager = $this->createMock(IGroupManager::class); |
|
| 52 | - $this->dateTimeFormatter = $this->createMock(IDateTimeFormatter::class); |
|
| 53 | - $this->l10nFactory = $this->createMock(IFactory::class); |
|
| 54 | - $this->subscriptionRegistry = $this->createMock(IRegistry::class); |
|
| 55 | - $this->userManager = $this->createMock(IUserManager::class); |
|
| 56 | - $this->logger = $this->createMock(LoggerInterface::class); |
|
| 57 | - $this->initialState = $this->createMock(IInitialState::class); |
|
| 58 | - $this->serverVersion = $this->createMock(ServerVersion::class); |
|
| 59 | - |
|
| 60 | - $this->admin = new Admin( |
|
| 61 | - $this->config, |
|
| 62 | - $this->appConfig, |
|
| 63 | - $this->updateChecker, |
|
| 64 | - $this->groupManager, |
|
| 65 | - $this->dateTimeFormatter, |
|
| 66 | - $this->l10nFactory, |
|
| 67 | - $this->subscriptionRegistry, |
|
| 68 | - $this->userManager, |
|
| 69 | - $this->logger, |
|
| 70 | - $this->initialState, |
|
| 71 | - $this->serverVersion, |
|
| 72 | - ); |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - public function testGetFormWithUpdate(): void { |
|
| 76 | - $this->serverVersion->expects(self::atLeastOnce()) |
|
| 77 | - ->method('getChannel') |
|
| 78 | - ->willReturn('daily'); |
|
| 79 | - $this->userManager |
|
| 80 | - ->expects($this->once()) |
|
| 81 | - ->method('countUsersTotal') |
|
| 82 | - ->willReturn(5); |
|
| 83 | - $channels = [ |
|
| 84 | - 'daily', |
|
| 85 | - 'beta', |
|
| 86 | - 'stable', |
|
| 87 | - 'production', |
|
| 88 | - ]; |
|
| 89 | - $this->appConfig |
|
| 90 | - ->expects($this->once()) |
|
| 91 | - ->method('getValueInt') |
|
| 92 | - ->with('core', 'lastupdatedat', 0) |
|
| 93 | - ->willReturn(12345); |
|
| 94 | - $this->appConfig |
|
| 95 | - ->expects($this->once()) |
|
| 96 | - ->method('getValueArray') |
|
| 97 | - ->with(Application::APP_NAME, 'notify_groups', ['admin']) |
|
| 98 | - ->willReturn(['admin']); |
|
| 99 | - $this->config |
|
| 100 | - ->method('getSystemValue') |
|
| 101 | - ->willReturnMap([ |
|
| 102 | - ['updater.server.url', 'https://updates.nextcloud.com/updater_server/', 'https://updates.nextcloud.com/updater_server/'], |
|
| 103 | - ['upgrade.disable-web', false, false], |
|
| 104 | - ]); |
|
| 105 | - $this->config |
|
| 106 | - ->expects(self::any()) |
|
| 107 | - ->method('getSystemValueBool') |
|
| 108 | - ->with('updatechecker', true) |
|
| 109 | - ->willReturn(true); |
|
| 110 | - $this->dateTimeFormatter |
|
| 111 | - ->expects($this->once()) |
|
| 112 | - ->method('formatDateTime') |
|
| 113 | - ->with(12345) |
|
| 114 | - ->willReturn('LastCheckedReturnValue'); |
|
| 115 | - $this->updateChecker |
|
| 116 | - ->expects($this->once()) |
|
| 117 | - ->method('getUpdateState') |
|
| 118 | - ->willReturn([ |
|
| 119 | - 'updateAvailable' => true, |
|
| 120 | - 'updateVersion' => '8.1.2', |
|
| 121 | - 'updateVersionString' => 'Nextcloud 8.1.2', |
|
| 122 | - 'downloadLink' => 'https://downloads.nextcloud.org/server', |
|
| 123 | - 'changes' => [], |
|
| 124 | - 'updaterEnabled' => true, |
|
| 125 | - 'versionIsEol' => false, |
|
| 126 | - ]); |
|
| 127 | - |
|
| 128 | - $group = $this->createMock(IGroup::class); |
|
| 129 | - $group->expects($this->any()) |
|
| 130 | - ->method('getDisplayName') |
|
| 131 | - ->willReturn('Administrators'); |
|
| 132 | - $group->expects($this->any()) |
|
| 133 | - ->method('getGID') |
|
| 134 | - ->willReturn('admin'); |
|
| 135 | - $this->groupManager->expects($this->once()) |
|
| 136 | - ->method('get') |
|
| 137 | - ->with('admin') |
|
| 138 | - ->willReturn($group); |
|
| 139 | - |
|
| 140 | - $this->subscriptionRegistry |
|
| 141 | - ->expects($this->once()) |
|
| 142 | - ->method('delegateHasValidSubscription') |
|
| 143 | - ->willReturn(true); |
|
| 144 | - |
|
| 145 | - $this->initialState->expects($this->once()) |
|
| 146 | - ->method('provideInitialState') |
|
| 147 | - ->with('data', [ |
|
| 148 | - 'isNewVersionAvailable' => true, |
|
| 149 | - 'isUpdateChecked' => true, |
|
| 150 | - 'lastChecked' => 'LastCheckedReturnValue', |
|
| 151 | - 'currentChannel' => 'daily', |
|
| 152 | - 'channels' => $channels, |
|
| 153 | - 'newVersion' => '8.1.2', |
|
| 154 | - 'newVersionString' => 'Nextcloud 8.1.2', |
|
| 155 | - 'downloadLink' => 'https://downloads.nextcloud.org/server', |
|
| 156 | - 'changes' => [], |
|
| 157 | - 'webUpdaterEnabled' => true, |
|
| 158 | - 'isWebUpdaterRecommended' => true, |
|
| 159 | - 'updaterEnabled' => true, |
|
| 160 | - 'versionIsEol' => false, |
|
| 161 | - 'isDefaultUpdateServerURL' => true, |
|
| 162 | - 'updateServerURL' => 'https://updates.nextcloud.com/updater_server/', |
|
| 163 | - 'notifyGroups' => [ |
|
| 164 | - ['id' => 'admin', 'displayname' => 'Administrators'], |
|
| 165 | - ], |
|
| 166 | - 'hasValidSubscription' => true, |
|
| 167 | - ]); |
|
| 168 | - |
|
| 169 | - $expected = new TemplateResponse(Application::APP_NAME, 'admin', [], ''); |
|
| 170 | - $this->assertEquals($expected, $this->admin->getForm()); |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - public function testGetFormWithUpdateAndChangedUpdateServer(): void { |
|
| 174 | - $this->serverVersion->expects(self::atLeastOnce()) |
|
| 175 | - ->method('getChannel') |
|
| 176 | - ->willReturn('beta'); |
|
| 177 | - $this->userManager |
|
| 178 | - ->expects($this->once()) |
|
| 179 | - ->method('countUsersTotal') |
|
| 180 | - ->willReturn(5); |
|
| 181 | - $channels = [ |
|
| 182 | - 'daily', |
|
| 183 | - 'beta', |
|
| 184 | - 'stable', |
|
| 185 | - 'production', |
|
| 186 | - ]; |
|
| 187 | - |
|
| 188 | - $this->appConfig |
|
| 189 | - ->expects($this->once()) |
|
| 190 | - ->method('getValueInt') |
|
| 191 | - ->with('core', 'lastupdatedat', 0) |
|
| 192 | - ->willReturn(12345); |
|
| 193 | - $this->config |
|
| 194 | - ->expects(self::any()) |
|
| 195 | - ->method('getSystemValueBool') |
|
| 196 | - ->with('updatechecker', true) |
|
| 197 | - ->willReturn(true); |
|
| 198 | - $this->appConfig |
|
| 199 | - ->expects($this->once()) |
|
| 200 | - ->method('getValueArray') |
|
| 201 | - ->with(Application::APP_NAME, 'notify_groups', ['admin']) |
|
| 202 | - ->willReturn(['admin']); |
|
| 203 | - $this->config |
|
| 204 | - ->method('getSystemValue') |
|
| 205 | - ->willReturnMap([ |
|
| 206 | - ['updater.server.url', 'https://updates.nextcloud.com/updater_server/', 'https://updates.nextcloud.com/updater_server_changed/'], |
|
| 207 | - ['upgrade.disable-web', false, true], |
|
| 208 | - ]); |
|
| 209 | - $this->dateTimeFormatter |
|
| 210 | - ->expects($this->once()) |
|
| 211 | - ->method('formatDateTime') |
|
| 212 | - ->with('12345') |
|
| 213 | - ->willReturn('LastCheckedReturnValue'); |
|
| 214 | - $this->updateChecker |
|
| 215 | - ->expects($this->once()) |
|
| 216 | - ->method('getUpdateState') |
|
| 217 | - ->willReturn([ |
|
| 218 | - 'updateAvailable' => true, |
|
| 219 | - 'updateVersion' => '8.1.2', |
|
| 220 | - 'updateVersionString' => 'Nextcloud 8.1.2', |
|
| 221 | - 'downloadLink' => 'https://downloads.nextcloud.org/server', |
|
| 222 | - 'changes' => [], |
|
| 223 | - 'updaterEnabled' => true, |
|
| 224 | - 'versionIsEol' => false, |
|
| 225 | - ]); |
|
| 226 | - |
|
| 227 | - $group = $this->createMock(IGroup::class); |
|
| 228 | - $group->expects($this->any()) |
|
| 229 | - ->method('getDisplayName') |
|
| 230 | - ->willReturn('Administrators'); |
|
| 231 | - $group->expects($this->any()) |
|
| 232 | - ->method('getGID') |
|
| 233 | - ->willReturn('admin'); |
|
| 234 | - $this->groupManager->expects($this->once()) |
|
| 235 | - ->method('get') |
|
| 236 | - ->with('admin') |
|
| 237 | - ->willReturn($group); |
|
| 238 | - |
|
| 239 | - $this->subscriptionRegistry |
|
| 240 | - ->expects($this->once()) |
|
| 241 | - ->method('delegateHasValidSubscription') |
|
| 242 | - ->willReturn(true); |
|
| 243 | - |
|
| 244 | - $this->initialState->expects($this->once()) |
|
| 245 | - ->method('provideInitialState') |
|
| 246 | - ->with('data', [ |
|
| 247 | - 'isNewVersionAvailable' => true, |
|
| 248 | - 'isUpdateChecked' => true, |
|
| 249 | - 'lastChecked' => 'LastCheckedReturnValue', |
|
| 250 | - 'currentChannel' => 'beta', |
|
| 251 | - 'channels' => $channels, |
|
| 252 | - 'newVersion' => '8.1.2', |
|
| 253 | - 'newVersionString' => 'Nextcloud 8.1.2', |
|
| 254 | - 'downloadLink' => 'https://downloads.nextcloud.org/server', |
|
| 255 | - 'changes' => [], |
|
| 256 | - 'webUpdaterEnabled' => false, |
|
| 257 | - 'isWebUpdaterRecommended' => true, |
|
| 258 | - 'updaterEnabled' => true, |
|
| 259 | - 'versionIsEol' => false, |
|
| 260 | - 'isDefaultUpdateServerURL' => false, |
|
| 261 | - 'updateServerURL' => 'https://updates.nextcloud.com/updater_server_changed/', |
|
| 262 | - 'notifyGroups' => [ |
|
| 263 | - ['id' => 'admin', 'displayname' => 'Administrators'], |
|
| 264 | - ], |
|
| 265 | - 'hasValidSubscription' => true, |
|
| 266 | - ]); |
|
| 267 | - |
|
| 268 | - $expected = new TemplateResponse(Application::APP_NAME, 'admin', [], ''); |
|
| 269 | - $this->assertEquals($expected, $this->admin->getForm()); |
|
| 270 | - } |
|
| 271 | - |
|
| 272 | - public function testGetFormWithUpdateAndCustomersUpdateServer(): void { |
|
| 273 | - $this->serverVersion->expects(self::atLeastOnce()) |
|
| 274 | - ->method('getChannel') |
|
| 275 | - ->willReturn('production'); |
|
| 276 | - $this->userManager |
|
| 277 | - ->expects($this->once()) |
|
| 278 | - ->method('countUsersTotal') |
|
| 279 | - ->willReturn(5); |
|
| 280 | - $channels = [ |
|
| 281 | - 'daily', |
|
| 282 | - 'beta', |
|
| 283 | - 'stable', |
|
| 284 | - 'production', |
|
| 285 | - ]; |
|
| 286 | - |
|
| 287 | - $this->appConfig |
|
| 288 | - ->expects($this->once()) |
|
| 289 | - ->method('getValueInt') |
|
| 290 | - ->with('core', 'lastupdatedat', 0) |
|
| 291 | - ->willReturn(12345); |
|
| 292 | - $this->config |
|
| 293 | - ->expects(self::any()) |
|
| 294 | - ->method('getSystemValueBool') |
|
| 295 | - ->with('updatechecker', true) |
|
| 296 | - ->willReturn(true); |
|
| 297 | - $this->appConfig |
|
| 298 | - ->expects(self::once()) |
|
| 299 | - ->method('getValueArray') |
|
| 300 | - ->with(Application::APP_NAME, 'notify_groups', ['admin']) |
|
| 301 | - ->willReturn(['admin']); |
|
| 302 | - $this->config |
|
| 303 | - ->method('getSystemValue') |
|
| 304 | - ->willReturnMap([ |
|
| 305 | - ['updater.server.url', 'https://updates.nextcloud.com/updater_server/', 'https://updates.nextcloud.com/customers/ABC-DEF/'], |
|
| 306 | - ['upgrade.disable-web', false, false], |
|
| 307 | - ]); |
|
| 308 | - $this->dateTimeFormatter |
|
| 309 | - ->expects($this->once()) |
|
| 310 | - ->method('formatDateTime') |
|
| 311 | - ->with('12345') |
|
| 312 | - ->willReturn('LastCheckedReturnValue'); |
|
| 313 | - $this->updateChecker |
|
| 314 | - ->expects($this->once()) |
|
| 315 | - ->method('getUpdateState') |
|
| 316 | - ->willReturn([ |
|
| 317 | - 'updateAvailable' => true, |
|
| 318 | - 'updateVersion' => '8.1.2', |
|
| 319 | - 'updateVersionString' => 'Nextcloud 8.1.2', |
|
| 320 | - 'downloadLink' => 'https://downloads.nextcloud.org/server', |
|
| 321 | - 'changes' => [], |
|
| 322 | - 'updaterEnabled' => true, |
|
| 323 | - 'versionIsEol' => false, |
|
| 324 | - ]); |
|
| 325 | - |
|
| 326 | - $group = $this->createMock(IGroup::class); |
|
| 327 | - $group->expects($this->any()) |
|
| 328 | - ->method('getDisplayName') |
|
| 329 | - ->willReturn('Administrators'); |
|
| 330 | - $group->expects($this->any()) |
|
| 331 | - ->method('getGID') |
|
| 332 | - ->willReturn('admin'); |
|
| 333 | - $this->groupManager->expects($this->once()) |
|
| 334 | - ->method('get') |
|
| 335 | - ->with('admin') |
|
| 336 | - ->willReturn($group); |
|
| 337 | - |
|
| 338 | - $this->subscriptionRegistry |
|
| 339 | - ->expects($this->once()) |
|
| 340 | - ->method('delegateHasValidSubscription') |
|
| 341 | - ->willReturn(true); |
|
| 342 | - |
|
| 343 | - $this->initialState->expects($this->once()) |
|
| 344 | - ->method('provideInitialState') |
|
| 345 | - ->with('data', [ |
|
| 346 | - 'isNewVersionAvailable' => true, |
|
| 347 | - 'isUpdateChecked' => true, |
|
| 348 | - 'lastChecked' => 'LastCheckedReturnValue', |
|
| 349 | - 'currentChannel' => 'production', |
|
| 350 | - 'channels' => $channels, |
|
| 351 | - 'newVersion' => '8.1.2', |
|
| 352 | - 'newVersionString' => 'Nextcloud 8.1.2', |
|
| 353 | - 'downloadLink' => 'https://downloads.nextcloud.org/server', |
|
| 354 | - 'changes' => [], |
|
| 355 | - 'webUpdaterEnabled' => true, |
|
| 356 | - 'isWebUpdaterRecommended' => true, |
|
| 357 | - 'updaterEnabled' => true, |
|
| 358 | - 'versionIsEol' => false, |
|
| 359 | - 'isDefaultUpdateServerURL' => true, |
|
| 360 | - 'updateServerURL' => 'https://updates.nextcloud.com/customers/ABC-DEF/', |
|
| 361 | - 'notifyGroups' => [ |
|
| 362 | - ['id' => 'admin', 'displayname' => 'Administrators'], |
|
| 363 | - ], |
|
| 364 | - 'hasValidSubscription' => true, |
|
| 365 | - ]); |
|
| 366 | - |
|
| 367 | - $expected = new TemplateResponse(Application::APP_NAME, 'admin', [], ''); |
|
| 368 | - $this->assertEquals($expected, $this->admin->getForm()); |
|
| 369 | - } |
|
| 370 | - |
|
| 371 | - |
|
| 372 | - public function testGetSection(): void { |
|
| 373 | - $this->config |
|
| 374 | - ->expects(self::atLeastOnce()) |
|
| 375 | - ->method('getSystemValueBool') |
|
| 376 | - ->with('updatechecker', true) |
|
| 377 | - ->willReturn(true); |
|
| 378 | - |
|
| 379 | - $this->assertSame('overview', $this->admin->getSection()); |
|
| 380 | - } |
|
| 381 | - |
|
| 382 | - public function testGetSectionDisabled(): void { |
|
| 383 | - $this->config |
|
| 384 | - ->expects(self::atLeastOnce()) |
|
| 385 | - ->method('getSystemValueBool') |
|
| 386 | - ->with('updatechecker', true) |
|
| 387 | - ->willReturn(false); |
|
| 388 | - |
|
| 389 | - $this->assertNull($this->admin->getSection()); |
|
| 390 | - } |
|
| 391 | - |
|
| 392 | - public function testGetPriority(): void { |
|
| 393 | - $this->assertSame(11, $this->admin->getPriority()); |
|
| 394 | - } |
|
| 395 | - |
|
| 396 | - public static function changesProvider(): array { |
|
| 397 | - return [ |
|
| 398 | - [ #0, all info, en |
|
| 399 | - [ |
|
| 400 | - 'changelogURL' => 'https://go.to.changelog', |
|
| 401 | - 'whatsNew' => [ |
|
| 402 | - 'en' => [ |
|
| 403 | - 'regular' => ['content'], |
|
| 404 | - ], |
|
| 405 | - 'de' => [ |
|
| 406 | - 'regular' => ['inhalt'], |
|
| 407 | - ] |
|
| 408 | - ], |
|
| 409 | - ], |
|
| 410 | - 'en', |
|
| 411 | - [ |
|
| 412 | - 'changelogURL' => 'https://go.to.changelog', |
|
| 413 | - 'whatsNew' => [ |
|
| 414 | - 'regular' => ['content'], |
|
| 415 | - ], |
|
| 416 | - ] |
|
| 417 | - ], |
|
| 418 | - [ #1, all info, de |
|
| 419 | - [ |
|
| 420 | - 'changelogURL' => 'https://go.to.changelog', |
|
| 421 | - 'whatsNew' => [ |
|
| 422 | - 'en' => [ |
|
| 423 | - 'regular' => ['content'], |
|
| 424 | - ], |
|
| 425 | - 'de' => [ |
|
| 426 | - 'regular' => ['inhalt'], |
|
| 427 | - ] |
|
| 428 | - ], |
|
| 429 | - ], |
|
| 430 | - 'de', |
|
| 431 | - [ |
|
| 432 | - 'changelogURL' => 'https://go.to.changelog', |
|
| 433 | - 'whatsNew' => [ |
|
| 434 | - 'regular' => ['inhalt'], |
|
| 435 | - ] |
|
| 436 | - ], |
|
| 437 | - ], |
|
| 438 | - [ #2, just changelog |
|
| 439 | - [ 'changelogURL' => 'https://go.to.changelog' ], |
|
| 440 | - 'en', |
|
| 441 | - [ 'changelogURL' => 'https://go.to.changelog' ], |
|
| 442 | - ], |
|
| 443 | - [ #3 nothing |
|
| 444 | - [], |
|
| 445 | - 'ru', |
|
| 446 | - [] |
|
| 447 | - ] |
|
| 448 | - ]; |
|
| 449 | - } |
|
| 450 | - |
|
| 451 | - /** |
|
| 452 | - * @dataProvider changesProvider |
|
| 453 | - */ |
|
| 454 | - public function testFilterChanges($changes, $userLang, $expectation): void { |
|
| 455 | - $iterator = $this->createMock(ILanguageIterator::class); |
|
| 456 | - $iterator->expects($this->any()) |
|
| 457 | - ->method('current') |
|
| 458 | - ->willReturnOnConsecutiveCalls('es', $userLang, 'it', 'en'); |
|
| 459 | - $iterator->expects($this->any()) |
|
| 460 | - ->method('valid') |
|
| 461 | - ->willReturn(true); |
|
| 462 | - |
|
| 463 | - $this->l10nFactory->expects($this->atMost(1)) |
|
| 464 | - ->method('getLanguageIterator') |
|
| 465 | - ->willReturn($iterator); |
|
| 466 | - $result = $this->invokePrivate($this->admin, 'filterChanges', [$changes]); |
|
| 467 | - $this->assertSame($expectation, $result); |
|
| 468 | - } |
|
| 32 | + private IFactory&MockObject $l10nFactory; |
|
| 33 | + private IConfig&MockObject $config; |
|
| 34 | + private IAppConfig&MockObject $appConfig; |
|
| 35 | + private UpdateChecker&MockObject $updateChecker; |
|
| 36 | + private IGroupManager&MockObject $groupManager; |
|
| 37 | + private IDateTimeFormatter&MockObject $dateTimeFormatter; |
|
| 38 | + private IRegistry&MockObject $subscriptionRegistry; |
|
| 39 | + private IUserManager&MockObject $userManager; |
|
| 40 | + private LoggerInterface&MockObject $logger; |
|
| 41 | + private IInitialState&MockObject $initialState; |
|
| 42 | + private ServerVersion&MockObject $serverVersion; |
|
| 43 | + private Admin $admin; |
|
| 44 | + |
|
| 45 | + protected function setUp(): void { |
|
| 46 | + parent::setUp(); |
|
| 47 | + |
|
| 48 | + $this->config = $this->createMock(IConfig::class); |
|
| 49 | + $this->appConfig = $this->createMock(IAppConfig::class); |
|
| 50 | + $this->updateChecker = $this->createMock(UpdateChecker::class); |
|
| 51 | + $this->groupManager = $this->createMock(IGroupManager::class); |
|
| 52 | + $this->dateTimeFormatter = $this->createMock(IDateTimeFormatter::class); |
|
| 53 | + $this->l10nFactory = $this->createMock(IFactory::class); |
|
| 54 | + $this->subscriptionRegistry = $this->createMock(IRegistry::class); |
|
| 55 | + $this->userManager = $this->createMock(IUserManager::class); |
|
| 56 | + $this->logger = $this->createMock(LoggerInterface::class); |
|
| 57 | + $this->initialState = $this->createMock(IInitialState::class); |
|
| 58 | + $this->serverVersion = $this->createMock(ServerVersion::class); |
|
| 59 | + |
|
| 60 | + $this->admin = new Admin( |
|
| 61 | + $this->config, |
|
| 62 | + $this->appConfig, |
|
| 63 | + $this->updateChecker, |
|
| 64 | + $this->groupManager, |
|
| 65 | + $this->dateTimeFormatter, |
|
| 66 | + $this->l10nFactory, |
|
| 67 | + $this->subscriptionRegistry, |
|
| 68 | + $this->userManager, |
|
| 69 | + $this->logger, |
|
| 70 | + $this->initialState, |
|
| 71 | + $this->serverVersion, |
|
| 72 | + ); |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + public function testGetFormWithUpdate(): void { |
|
| 76 | + $this->serverVersion->expects(self::atLeastOnce()) |
|
| 77 | + ->method('getChannel') |
|
| 78 | + ->willReturn('daily'); |
|
| 79 | + $this->userManager |
|
| 80 | + ->expects($this->once()) |
|
| 81 | + ->method('countUsersTotal') |
|
| 82 | + ->willReturn(5); |
|
| 83 | + $channels = [ |
|
| 84 | + 'daily', |
|
| 85 | + 'beta', |
|
| 86 | + 'stable', |
|
| 87 | + 'production', |
|
| 88 | + ]; |
|
| 89 | + $this->appConfig |
|
| 90 | + ->expects($this->once()) |
|
| 91 | + ->method('getValueInt') |
|
| 92 | + ->with('core', 'lastupdatedat', 0) |
|
| 93 | + ->willReturn(12345); |
|
| 94 | + $this->appConfig |
|
| 95 | + ->expects($this->once()) |
|
| 96 | + ->method('getValueArray') |
|
| 97 | + ->with(Application::APP_NAME, 'notify_groups', ['admin']) |
|
| 98 | + ->willReturn(['admin']); |
|
| 99 | + $this->config |
|
| 100 | + ->method('getSystemValue') |
|
| 101 | + ->willReturnMap([ |
|
| 102 | + ['updater.server.url', 'https://updates.nextcloud.com/updater_server/', 'https://updates.nextcloud.com/updater_server/'], |
|
| 103 | + ['upgrade.disable-web', false, false], |
|
| 104 | + ]); |
|
| 105 | + $this->config |
|
| 106 | + ->expects(self::any()) |
|
| 107 | + ->method('getSystemValueBool') |
|
| 108 | + ->with('updatechecker', true) |
|
| 109 | + ->willReturn(true); |
|
| 110 | + $this->dateTimeFormatter |
|
| 111 | + ->expects($this->once()) |
|
| 112 | + ->method('formatDateTime') |
|
| 113 | + ->with(12345) |
|
| 114 | + ->willReturn('LastCheckedReturnValue'); |
|
| 115 | + $this->updateChecker |
|
| 116 | + ->expects($this->once()) |
|
| 117 | + ->method('getUpdateState') |
|
| 118 | + ->willReturn([ |
|
| 119 | + 'updateAvailable' => true, |
|
| 120 | + 'updateVersion' => '8.1.2', |
|
| 121 | + 'updateVersionString' => 'Nextcloud 8.1.2', |
|
| 122 | + 'downloadLink' => 'https://downloads.nextcloud.org/server', |
|
| 123 | + 'changes' => [], |
|
| 124 | + 'updaterEnabled' => true, |
|
| 125 | + 'versionIsEol' => false, |
|
| 126 | + ]); |
|
| 127 | + |
|
| 128 | + $group = $this->createMock(IGroup::class); |
|
| 129 | + $group->expects($this->any()) |
|
| 130 | + ->method('getDisplayName') |
|
| 131 | + ->willReturn('Administrators'); |
|
| 132 | + $group->expects($this->any()) |
|
| 133 | + ->method('getGID') |
|
| 134 | + ->willReturn('admin'); |
|
| 135 | + $this->groupManager->expects($this->once()) |
|
| 136 | + ->method('get') |
|
| 137 | + ->with('admin') |
|
| 138 | + ->willReturn($group); |
|
| 139 | + |
|
| 140 | + $this->subscriptionRegistry |
|
| 141 | + ->expects($this->once()) |
|
| 142 | + ->method('delegateHasValidSubscription') |
|
| 143 | + ->willReturn(true); |
|
| 144 | + |
|
| 145 | + $this->initialState->expects($this->once()) |
|
| 146 | + ->method('provideInitialState') |
|
| 147 | + ->with('data', [ |
|
| 148 | + 'isNewVersionAvailable' => true, |
|
| 149 | + 'isUpdateChecked' => true, |
|
| 150 | + 'lastChecked' => 'LastCheckedReturnValue', |
|
| 151 | + 'currentChannel' => 'daily', |
|
| 152 | + 'channels' => $channels, |
|
| 153 | + 'newVersion' => '8.1.2', |
|
| 154 | + 'newVersionString' => 'Nextcloud 8.1.2', |
|
| 155 | + 'downloadLink' => 'https://downloads.nextcloud.org/server', |
|
| 156 | + 'changes' => [], |
|
| 157 | + 'webUpdaterEnabled' => true, |
|
| 158 | + 'isWebUpdaterRecommended' => true, |
|
| 159 | + 'updaterEnabled' => true, |
|
| 160 | + 'versionIsEol' => false, |
|
| 161 | + 'isDefaultUpdateServerURL' => true, |
|
| 162 | + 'updateServerURL' => 'https://updates.nextcloud.com/updater_server/', |
|
| 163 | + 'notifyGroups' => [ |
|
| 164 | + ['id' => 'admin', 'displayname' => 'Administrators'], |
|
| 165 | + ], |
|
| 166 | + 'hasValidSubscription' => true, |
|
| 167 | + ]); |
|
| 168 | + |
|
| 169 | + $expected = new TemplateResponse(Application::APP_NAME, 'admin', [], ''); |
|
| 170 | + $this->assertEquals($expected, $this->admin->getForm()); |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + public function testGetFormWithUpdateAndChangedUpdateServer(): void { |
|
| 174 | + $this->serverVersion->expects(self::atLeastOnce()) |
|
| 175 | + ->method('getChannel') |
|
| 176 | + ->willReturn('beta'); |
|
| 177 | + $this->userManager |
|
| 178 | + ->expects($this->once()) |
|
| 179 | + ->method('countUsersTotal') |
|
| 180 | + ->willReturn(5); |
|
| 181 | + $channels = [ |
|
| 182 | + 'daily', |
|
| 183 | + 'beta', |
|
| 184 | + 'stable', |
|
| 185 | + 'production', |
|
| 186 | + ]; |
|
| 187 | + |
|
| 188 | + $this->appConfig |
|
| 189 | + ->expects($this->once()) |
|
| 190 | + ->method('getValueInt') |
|
| 191 | + ->with('core', 'lastupdatedat', 0) |
|
| 192 | + ->willReturn(12345); |
|
| 193 | + $this->config |
|
| 194 | + ->expects(self::any()) |
|
| 195 | + ->method('getSystemValueBool') |
|
| 196 | + ->with('updatechecker', true) |
|
| 197 | + ->willReturn(true); |
|
| 198 | + $this->appConfig |
|
| 199 | + ->expects($this->once()) |
|
| 200 | + ->method('getValueArray') |
|
| 201 | + ->with(Application::APP_NAME, 'notify_groups', ['admin']) |
|
| 202 | + ->willReturn(['admin']); |
|
| 203 | + $this->config |
|
| 204 | + ->method('getSystemValue') |
|
| 205 | + ->willReturnMap([ |
|
| 206 | + ['updater.server.url', 'https://updates.nextcloud.com/updater_server/', 'https://updates.nextcloud.com/updater_server_changed/'], |
|
| 207 | + ['upgrade.disable-web', false, true], |
|
| 208 | + ]); |
|
| 209 | + $this->dateTimeFormatter |
|
| 210 | + ->expects($this->once()) |
|
| 211 | + ->method('formatDateTime') |
|
| 212 | + ->with('12345') |
|
| 213 | + ->willReturn('LastCheckedReturnValue'); |
|
| 214 | + $this->updateChecker |
|
| 215 | + ->expects($this->once()) |
|
| 216 | + ->method('getUpdateState') |
|
| 217 | + ->willReturn([ |
|
| 218 | + 'updateAvailable' => true, |
|
| 219 | + 'updateVersion' => '8.1.2', |
|
| 220 | + 'updateVersionString' => 'Nextcloud 8.1.2', |
|
| 221 | + 'downloadLink' => 'https://downloads.nextcloud.org/server', |
|
| 222 | + 'changes' => [], |
|
| 223 | + 'updaterEnabled' => true, |
|
| 224 | + 'versionIsEol' => false, |
|
| 225 | + ]); |
|
| 226 | + |
|
| 227 | + $group = $this->createMock(IGroup::class); |
|
| 228 | + $group->expects($this->any()) |
|
| 229 | + ->method('getDisplayName') |
|
| 230 | + ->willReturn('Administrators'); |
|
| 231 | + $group->expects($this->any()) |
|
| 232 | + ->method('getGID') |
|
| 233 | + ->willReturn('admin'); |
|
| 234 | + $this->groupManager->expects($this->once()) |
|
| 235 | + ->method('get') |
|
| 236 | + ->with('admin') |
|
| 237 | + ->willReturn($group); |
|
| 238 | + |
|
| 239 | + $this->subscriptionRegistry |
|
| 240 | + ->expects($this->once()) |
|
| 241 | + ->method('delegateHasValidSubscription') |
|
| 242 | + ->willReturn(true); |
|
| 243 | + |
|
| 244 | + $this->initialState->expects($this->once()) |
|
| 245 | + ->method('provideInitialState') |
|
| 246 | + ->with('data', [ |
|
| 247 | + 'isNewVersionAvailable' => true, |
|
| 248 | + 'isUpdateChecked' => true, |
|
| 249 | + 'lastChecked' => 'LastCheckedReturnValue', |
|
| 250 | + 'currentChannel' => 'beta', |
|
| 251 | + 'channels' => $channels, |
|
| 252 | + 'newVersion' => '8.1.2', |
|
| 253 | + 'newVersionString' => 'Nextcloud 8.1.2', |
|
| 254 | + 'downloadLink' => 'https://downloads.nextcloud.org/server', |
|
| 255 | + 'changes' => [], |
|
| 256 | + 'webUpdaterEnabled' => false, |
|
| 257 | + 'isWebUpdaterRecommended' => true, |
|
| 258 | + 'updaterEnabled' => true, |
|
| 259 | + 'versionIsEol' => false, |
|
| 260 | + 'isDefaultUpdateServerURL' => false, |
|
| 261 | + 'updateServerURL' => 'https://updates.nextcloud.com/updater_server_changed/', |
|
| 262 | + 'notifyGroups' => [ |
|
| 263 | + ['id' => 'admin', 'displayname' => 'Administrators'], |
|
| 264 | + ], |
|
| 265 | + 'hasValidSubscription' => true, |
|
| 266 | + ]); |
|
| 267 | + |
|
| 268 | + $expected = new TemplateResponse(Application::APP_NAME, 'admin', [], ''); |
|
| 269 | + $this->assertEquals($expected, $this->admin->getForm()); |
|
| 270 | + } |
|
| 271 | + |
|
| 272 | + public function testGetFormWithUpdateAndCustomersUpdateServer(): void { |
|
| 273 | + $this->serverVersion->expects(self::atLeastOnce()) |
|
| 274 | + ->method('getChannel') |
|
| 275 | + ->willReturn('production'); |
|
| 276 | + $this->userManager |
|
| 277 | + ->expects($this->once()) |
|
| 278 | + ->method('countUsersTotal') |
|
| 279 | + ->willReturn(5); |
|
| 280 | + $channels = [ |
|
| 281 | + 'daily', |
|
| 282 | + 'beta', |
|
| 283 | + 'stable', |
|
| 284 | + 'production', |
|
| 285 | + ]; |
|
| 286 | + |
|
| 287 | + $this->appConfig |
|
| 288 | + ->expects($this->once()) |
|
| 289 | + ->method('getValueInt') |
|
| 290 | + ->with('core', 'lastupdatedat', 0) |
|
| 291 | + ->willReturn(12345); |
|
| 292 | + $this->config |
|
| 293 | + ->expects(self::any()) |
|
| 294 | + ->method('getSystemValueBool') |
|
| 295 | + ->with('updatechecker', true) |
|
| 296 | + ->willReturn(true); |
|
| 297 | + $this->appConfig |
|
| 298 | + ->expects(self::once()) |
|
| 299 | + ->method('getValueArray') |
|
| 300 | + ->with(Application::APP_NAME, 'notify_groups', ['admin']) |
|
| 301 | + ->willReturn(['admin']); |
|
| 302 | + $this->config |
|
| 303 | + ->method('getSystemValue') |
|
| 304 | + ->willReturnMap([ |
|
| 305 | + ['updater.server.url', 'https://updates.nextcloud.com/updater_server/', 'https://updates.nextcloud.com/customers/ABC-DEF/'], |
|
| 306 | + ['upgrade.disable-web', false, false], |
|
| 307 | + ]); |
|
| 308 | + $this->dateTimeFormatter |
|
| 309 | + ->expects($this->once()) |
|
| 310 | + ->method('formatDateTime') |
|
| 311 | + ->with('12345') |
|
| 312 | + ->willReturn('LastCheckedReturnValue'); |
|
| 313 | + $this->updateChecker |
|
| 314 | + ->expects($this->once()) |
|
| 315 | + ->method('getUpdateState') |
|
| 316 | + ->willReturn([ |
|
| 317 | + 'updateAvailable' => true, |
|
| 318 | + 'updateVersion' => '8.1.2', |
|
| 319 | + 'updateVersionString' => 'Nextcloud 8.1.2', |
|
| 320 | + 'downloadLink' => 'https://downloads.nextcloud.org/server', |
|
| 321 | + 'changes' => [], |
|
| 322 | + 'updaterEnabled' => true, |
|
| 323 | + 'versionIsEol' => false, |
|
| 324 | + ]); |
|
| 325 | + |
|
| 326 | + $group = $this->createMock(IGroup::class); |
|
| 327 | + $group->expects($this->any()) |
|
| 328 | + ->method('getDisplayName') |
|
| 329 | + ->willReturn('Administrators'); |
|
| 330 | + $group->expects($this->any()) |
|
| 331 | + ->method('getGID') |
|
| 332 | + ->willReturn('admin'); |
|
| 333 | + $this->groupManager->expects($this->once()) |
|
| 334 | + ->method('get') |
|
| 335 | + ->with('admin') |
|
| 336 | + ->willReturn($group); |
|
| 337 | + |
|
| 338 | + $this->subscriptionRegistry |
|
| 339 | + ->expects($this->once()) |
|
| 340 | + ->method('delegateHasValidSubscription') |
|
| 341 | + ->willReturn(true); |
|
| 342 | + |
|
| 343 | + $this->initialState->expects($this->once()) |
|
| 344 | + ->method('provideInitialState') |
|
| 345 | + ->with('data', [ |
|
| 346 | + 'isNewVersionAvailable' => true, |
|
| 347 | + 'isUpdateChecked' => true, |
|
| 348 | + 'lastChecked' => 'LastCheckedReturnValue', |
|
| 349 | + 'currentChannel' => 'production', |
|
| 350 | + 'channels' => $channels, |
|
| 351 | + 'newVersion' => '8.1.2', |
|
| 352 | + 'newVersionString' => 'Nextcloud 8.1.2', |
|
| 353 | + 'downloadLink' => 'https://downloads.nextcloud.org/server', |
|
| 354 | + 'changes' => [], |
|
| 355 | + 'webUpdaterEnabled' => true, |
|
| 356 | + 'isWebUpdaterRecommended' => true, |
|
| 357 | + 'updaterEnabled' => true, |
|
| 358 | + 'versionIsEol' => false, |
|
| 359 | + 'isDefaultUpdateServerURL' => true, |
|
| 360 | + 'updateServerURL' => 'https://updates.nextcloud.com/customers/ABC-DEF/', |
|
| 361 | + 'notifyGroups' => [ |
|
| 362 | + ['id' => 'admin', 'displayname' => 'Administrators'], |
|
| 363 | + ], |
|
| 364 | + 'hasValidSubscription' => true, |
|
| 365 | + ]); |
|
| 366 | + |
|
| 367 | + $expected = new TemplateResponse(Application::APP_NAME, 'admin', [], ''); |
|
| 368 | + $this->assertEquals($expected, $this->admin->getForm()); |
|
| 369 | + } |
|
| 370 | + |
|
| 371 | + |
|
| 372 | + public function testGetSection(): void { |
|
| 373 | + $this->config |
|
| 374 | + ->expects(self::atLeastOnce()) |
|
| 375 | + ->method('getSystemValueBool') |
|
| 376 | + ->with('updatechecker', true) |
|
| 377 | + ->willReturn(true); |
|
| 378 | + |
|
| 379 | + $this->assertSame('overview', $this->admin->getSection()); |
|
| 380 | + } |
|
| 381 | + |
|
| 382 | + public function testGetSectionDisabled(): void { |
|
| 383 | + $this->config |
|
| 384 | + ->expects(self::atLeastOnce()) |
|
| 385 | + ->method('getSystemValueBool') |
|
| 386 | + ->with('updatechecker', true) |
|
| 387 | + ->willReturn(false); |
|
| 388 | + |
|
| 389 | + $this->assertNull($this->admin->getSection()); |
|
| 390 | + } |
|
| 391 | + |
|
| 392 | + public function testGetPriority(): void { |
|
| 393 | + $this->assertSame(11, $this->admin->getPriority()); |
|
| 394 | + } |
|
| 395 | + |
|
| 396 | + public static function changesProvider(): array { |
|
| 397 | + return [ |
|
| 398 | + [ #0, all info, en |
|
| 399 | + [ |
|
| 400 | + 'changelogURL' => 'https://go.to.changelog', |
|
| 401 | + 'whatsNew' => [ |
|
| 402 | + 'en' => [ |
|
| 403 | + 'regular' => ['content'], |
|
| 404 | + ], |
|
| 405 | + 'de' => [ |
|
| 406 | + 'regular' => ['inhalt'], |
|
| 407 | + ] |
|
| 408 | + ], |
|
| 409 | + ], |
|
| 410 | + 'en', |
|
| 411 | + [ |
|
| 412 | + 'changelogURL' => 'https://go.to.changelog', |
|
| 413 | + 'whatsNew' => [ |
|
| 414 | + 'regular' => ['content'], |
|
| 415 | + ], |
|
| 416 | + ] |
|
| 417 | + ], |
|
| 418 | + [ #1, all info, de |
|
| 419 | + [ |
|
| 420 | + 'changelogURL' => 'https://go.to.changelog', |
|
| 421 | + 'whatsNew' => [ |
|
| 422 | + 'en' => [ |
|
| 423 | + 'regular' => ['content'], |
|
| 424 | + ], |
|
| 425 | + 'de' => [ |
|
| 426 | + 'regular' => ['inhalt'], |
|
| 427 | + ] |
|
| 428 | + ], |
|
| 429 | + ], |
|
| 430 | + 'de', |
|
| 431 | + [ |
|
| 432 | + 'changelogURL' => 'https://go.to.changelog', |
|
| 433 | + 'whatsNew' => [ |
|
| 434 | + 'regular' => ['inhalt'], |
|
| 435 | + ] |
|
| 436 | + ], |
|
| 437 | + ], |
|
| 438 | + [ #2, just changelog |
|
| 439 | + [ 'changelogURL' => 'https://go.to.changelog' ], |
|
| 440 | + 'en', |
|
| 441 | + [ 'changelogURL' => 'https://go.to.changelog' ], |
|
| 442 | + ], |
|
| 443 | + [ #3 nothing |
|
| 444 | + [], |
|
| 445 | + 'ru', |
|
| 446 | + [] |
|
| 447 | + ] |
|
| 448 | + ]; |
|
| 449 | + } |
|
| 450 | + |
|
| 451 | + /** |
|
| 452 | + * @dataProvider changesProvider |
|
| 453 | + */ |
|
| 454 | + public function testFilterChanges($changes, $userLang, $expectation): void { |
|
| 455 | + $iterator = $this->createMock(ILanguageIterator::class); |
|
| 456 | + $iterator->expects($this->any()) |
|
| 457 | + ->method('current') |
|
| 458 | + ->willReturnOnConsecutiveCalls('es', $userLang, 'it', 'en'); |
|
| 459 | + $iterator->expects($this->any()) |
|
| 460 | + ->method('valid') |
|
| 461 | + ->willReturn(true); |
|
| 462 | + |
|
| 463 | + $this->l10nFactory->expects($this->atMost(1)) |
|
| 464 | + ->method('getLanguageIterator') |
|
| 465 | + ->willReturn($iterator); |
|
| 466 | + $result = $this->invokePrivate($this->admin, 'filterChanges', [$changes]); |
|
| 467 | + $this->assertSame($expectation, $result); |
|
| 468 | + } |
|
| 469 | 469 | } |
@@ -395,7 +395,7 @@ discard block |
||
| 395 | 395 | |
| 396 | 396 | public static function changesProvider(): array { |
| 397 | 397 | return [ |
| 398 | - [ #0, all info, en |
|
| 398 | + [#0, all info, en |
|
| 399 | 399 | [ |
| 400 | 400 | 'changelogURL' => 'https://go.to.changelog', |
| 401 | 401 | 'whatsNew' => [ |
@@ -415,7 +415,7 @@ discard block |
||
| 415 | 415 | ], |
| 416 | 416 | ] |
| 417 | 417 | ], |
| 418 | - [ #1, all info, de |
|
| 418 | + [#1, all info, de |
|
| 419 | 419 | [ |
| 420 | 420 | 'changelogURL' => 'https://go.to.changelog', |
| 421 | 421 | 'whatsNew' => [ |
@@ -435,12 +435,12 @@ discard block |
||
| 435 | 435 | ] |
| 436 | 436 | ], |
| 437 | 437 | ], |
| 438 | - [ #2, just changelog |
|
| 439 | - [ 'changelogURL' => 'https://go.to.changelog' ], |
|
| 438 | + [#2, just changelog |
|
| 439 | + ['changelogURL' => 'https://go.to.changelog'], |
|
| 440 | 440 | 'en', |
| 441 | - [ 'changelogURL' => 'https://go.to.changelog' ], |
|
| 441 | + ['changelogURL' => 'https://go.to.changelog'], |
|
| 442 | 442 | ], |
| 443 | - [ #3 nothing |
|
| 443 | + [#3 nothing |
|
| 444 | 444 | [], |
| 445 | 445 | 'ru', |
| 446 | 446 | [] |
@@ -25,87 +25,87 @@ |
||
| 25 | 25 | |
| 26 | 26 | class NotifierTest extends TestCase { |
| 27 | 27 | |
| 28 | - protected IURLGenerator&MockObject $urlGenerator; |
|
| 29 | - protected IAppConfig&MockObject $appConfig; |
|
| 30 | - protected IManager&MockObject $notificationManager; |
|
| 31 | - protected IFactory&MockObject $l10nFactory; |
|
| 32 | - protected IUserSession&MockObject $userSession; |
|
| 33 | - protected IGroupManager&MockObject $groupManager; |
|
| 34 | - protected IAppManager&MockObject $appManager; |
|
| 35 | - protected ServerVersion&MockObject $serverVersion; |
|
| 28 | + protected IURLGenerator&MockObject $urlGenerator; |
|
| 29 | + protected IAppConfig&MockObject $appConfig; |
|
| 30 | + protected IManager&MockObject $notificationManager; |
|
| 31 | + protected IFactory&MockObject $l10nFactory; |
|
| 32 | + protected IUserSession&MockObject $userSession; |
|
| 33 | + protected IGroupManager&MockObject $groupManager; |
|
| 34 | + protected IAppManager&MockObject $appManager; |
|
| 35 | + protected ServerVersion&MockObject $serverVersion; |
|
| 36 | 36 | |
| 37 | - protected function setUp(): void { |
|
| 38 | - parent::setUp(); |
|
| 37 | + protected function setUp(): void { |
|
| 38 | + parent::setUp(); |
|
| 39 | 39 | |
| 40 | - $this->urlGenerator = $this->createMock(IURLGenerator::class); |
|
| 41 | - $this->appConfig = $this->createMock(IAppConfig::class); |
|
| 42 | - $this->notificationManager = $this->createMock(IManager::class); |
|
| 43 | - $this->l10nFactory = $this->createMock(IFactory::class); |
|
| 44 | - $this->userSession = $this->createMock(IUserSession::class); |
|
| 45 | - $this->groupManager = $this->createMock(IGroupManager::class); |
|
| 46 | - $this->appManager = $this->createMock(IAppManager::class); |
|
| 47 | - $this->serverVersion = $this->createMock(ServerVersion::class); |
|
| 48 | - } |
|
| 40 | + $this->urlGenerator = $this->createMock(IURLGenerator::class); |
|
| 41 | + $this->appConfig = $this->createMock(IAppConfig::class); |
|
| 42 | + $this->notificationManager = $this->createMock(IManager::class); |
|
| 43 | + $this->l10nFactory = $this->createMock(IFactory::class); |
|
| 44 | + $this->userSession = $this->createMock(IUserSession::class); |
|
| 45 | + $this->groupManager = $this->createMock(IGroupManager::class); |
|
| 46 | + $this->appManager = $this->createMock(IAppManager::class); |
|
| 47 | + $this->serverVersion = $this->createMock(ServerVersion::class); |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * @param array $methods |
|
| 52 | - * @return Notifier|MockObject |
|
| 53 | - */ |
|
| 54 | - protected function getNotifier(array $methods = []): Notifier { |
|
| 55 | - if (empty($methods)) { |
|
| 56 | - return new Notifier( |
|
| 57 | - $this->urlGenerator, |
|
| 58 | - $this->appConfig, |
|
| 59 | - $this->notificationManager, |
|
| 60 | - $this->l10nFactory, |
|
| 61 | - $this->userSession, |
|
| 62 | - $this->groupManager, |
|
| 63 | - $this->appManager, |
|
| 64 | - $this->serverVersion, |
|
| 65 | - ); |
|
| 66 | - } |
|
| 67 | - { |
|
| 68 | - return $this->getMockBuilder(Notifier::class) |
|
| 69 | - ->setConstructorArgs([ |
|
| 70 | - $this->urlGenerator, |
|
| 71 | - $this->appConfig, |
|
| 72 | - $this->notificationManager, |
|
| 73 | - $this->l10nFactory, |
|
| 74 | - $this->userSession, |
|
| 75 | - $this->groupManager, |
|
| 76 | - $this->appManager, |
|
| 77 | - $this->serverVersion, |
|
| 78 | - ]) |
|
| 79 | - ->onlyMethods($methods) |
|
| 80 | - ->getMock(); |
|
| 81 | - } |
|
| 82 | - } |
|
| 50 | + /** |
|
| 51 | + * @param array $methods |
|
| 52 | + * @return Notifier|MockObject |
|
| 53 | + */ |
|
| 54 | + protected function getNotifier(array $methods = []): Notifier { |
|
| 55 | + if (empty($methods)) { |
|
| 56 | + return new Notifier( |
|
| 57 | + $this->urlGenerator, |
|
| 58 | + $this->appConfig, |
|
| 59 | + $this->notificationManager, |
|
| 60 | + $this->l10nFactory, |
|
| 61 | + $this->userSession, |
|
| 62 | + $this->groupManager, |
|
| 63 | + $this->appManager, |
|
| 64 | + $this->serverVersion, |
|
| 65 | + ); |
|
| 66 | + } |
|
| 67 | + { |
|
| 68 | + return $this->getMockBuilder(Notifier::class) |
|
| 69 | + ->setConstructorArgs([ |
|
| 70 | + $this->urlGenerator, |
|
| 71 | + $this->appConfig, |
|
| 72 | + $this->notificationManager, |
|
| 73 | + $this->l10nFactory, |
|
| 74 | + $this->userSession, |
|
| 75 | + $this->groupManager, |
|
| 76 | + $this->appManager, |
|
| 77 | + $this->serverVersion, |
|
| 78 | + ]) |
|
| 79 | + ->onlyMethods($methods) |
|
| 80 | + ->getMock(); |
|
| 81 | + } |
|
| 82 | + } |
|
| 83 | 83 | |
| 84 | - public static function dataUpdateAlreadyInstalledCheck(): array { |
|
| 85 | - return [ |
|
| 86 | - ['1.1.0', '1.0.0', false], |
|
| 87 | - ['1.1.0', '1.1.0', true], |
|
| 88 | - ['1.1.0', '1.2.0', true], |
|
| 89 | - ]; |
|
| 90 | - } |
|
| 84 | + public static function dataUpdateAlreadyInstalledCheck(): array { |
|
| 85 | + return [ |
|
| 86 | + ['1.1.0', '1.0.0', false], |
|
| 87 | + ['1.1.0', '1.1.0', true], |
|
| 88 | + ['1.1.0', '1.2.0', true], |
|
| 89 | + ]; |
|
| 90 | + } |
|
| 91 | 91 | |
| 92 | - /** |
|
| 93 | - * @dataProvider dataUpdateAlreadyInstalledCheck |
|
| 94 | - */ |
|
| 95 | - public function testUpdateAlreadyInstalledCheck(string $versionNotification, string $versionInstalled, bool $exception): void { |
|
| 96 | - $notifier = $this->getNotifier(); |
|
| 92 | + /** |
|
| 93 | + * @dataProvider dataUpdateAlreadyInstalledCheck |
|
| 94 | + */ |
|
| 95 | + public function testUpdateAlreadyInstalledCheck(string $versionNotification, string $versionInstalled, bool $exception): void { |
|
| 96 | + $notifier = $this->getNotifier(); |
|
| 97 | 97 | |
| 98 | - $notification = $this->createMock(INotification::class); |
|
| 99 | - $notification->expects($this->once()) |
|
| 100 | - ->method('getObjectId') |
|
| 101 | - ->willReturn($versionNotification); |
|
| 98 | + $notification = $this->createMock(INotification::class); |
|
| 99 | + $notification->expects($this->once()) |
|
| 100 | + ->method('getObjectId') |
|
| 101 | + ->willReturn($versionNotification); |
|
| 102 | 102 | |
| 103 | - try { |
|
| 104 | - self::invokePrivate($notifier, 'updateAlreadyInstalledCheck', [$notification, $versionInstalled]); |
|
| 105 | - $this->assertFalse($exception); |
|
| 106 | - } catch (\Exception $e) { |
|
| 107 | - $this->assertTrue($exception); |
|
| 108 | - $this->assertInstanceOf(AlreadyProcessedException::class, $e); |
|
| 109 | - } |
|
| 110 | - } |
|
| 103 | + try { |
|
| 104 | + self::invokePrivate($notifier, 'updateAlreadyInstalledCheck', [$notification, $versionInstalled]); |
|
| 105 | + $this->assertFalse($exception); |
|
| 106 | + } catch (\Exception $e) { |
|
| 107 | + $this->assertTrue($exception); |
|
| 108 | + $this->assertInstanceOf(AlreadyProcessedException::class, $e); |
|
| 109 | + } |
|
| 110 | + } |
|
| 111 | 111 | } |
@@ -16,78 +16,78 @@ |
||
| 16 | 16 | use Test\TestCase; |
| 17 | 17 | |
| 18 | 18 | class ResetTokenTest extends TestCase { |
| 19 | - private IConfig&MockObject $config; |
|
| 20 | - private IAppConfig&MockObject $appConfig; |
|
| 21 | - private ITimeFactory&MockObject $timeFactory; |
|
| 22 | - private BackgroundJobResetToken $resetTokenBackgroundJob; |
|
| 19 | + private IConfig&MockObject $config; |
|
| 20 | + private IAppConfig&MockObject $appConfig; |
|
| 21 | + private ITimeFactory&MockObject $timeFactory; |
|
| 22 | + private BackgroundJobResetToken $resetTokenBackgroundJob; |
|
| 23 | 23 | |
| 24 | - protected function setUp(): void { |
|
| 25 | - parent::setUp(); |
|
| 26 | - $this->appConfig = $this->createMock(IAppConfig::class); |
|
| 27 | - $this->config = $this->createMock(IConfig::class); |
|
| 28 | - $this->timeFactory = $this->createMock(ITimeFactory::class); |
|
| 29 | - $this->resetTokenBackgroundJob = new BackgroundJobResetToken( |
|
| 30 | - $this->timeFactory, |
|
| 31 | - $this->config, |
|
| 32 | - $this->appConfig, |
|
| 33 | - ); |
|
| 34 | - } |
|
| 24 | + protected function setUp(): void { |
|
| 25 | + parent::setUp(); |
|
| 26 | + $this->appConfig = $this->createMock(IAppConfig::class); |
|
| 27 | + $this->config = $this->createMock(IConfig::class); |
|
| 28 | + $this->timeFactory = $this->createMock(ITimeFactory::class); |
|
| 29 | + $this->resetTokenBackgroundJob = new BackgroundJobResetToken( |
|
| 30 | + $this->timeFactory, |
|
| 31 | + $this->config, |
|
| 32 | + $this->appConfig, |
|
| 33 | + ); |
|
| 34 | + } |
|
| 35 | 35 | |
| 36 | - public function testRunWithNotExpiredToken(): void { |
|
| 37 | - $this->timeFactory |
|
| 38 | - ->expects($this->atLeastOnce()) |
|
| 39 | - ->method('getTime') |
|
| 40 | - ->willReturn(123); |
|
| 41 | - $this->appConfig |
|
| 42 | - ->expects($this->once()) |
|
| 43 | - ->method('getValueInt') |
|
| 44 | - ->with('core', 'updater.secret.created', 123); |
|
| 45 | - $this->config |
|
| 46 | - ->expects($this->once()) |
|
| 47 | - ->method('getSystemValueBool') |
|
| 48 | - ->with('config_is_read_only') |
|
| 49 | - ->willReturn(false); |
|
| 50 | - $this->config |
|
| 51 | - ->expects($this->never()) |
|
| 52 | - ->method('deleteSystemValue'); |
|
| 36 | + public function testRunWithNotExpiredToken(): void { |
|
| 37 | + $this->timeFactory |
|
| 38 | + ->expects($this->atLeastOnce()) |
|
| 39 | + ->method('getTime') |
|
| 40 | + ->willReturn(123); |
|
| 41 | + $this->appConfig |
|
| 42 | + ->expects($this->once()) |
|
| 43 | + ->method('getValueInt') |
|
| 44 | + ->with('core', 'updater.secret.created', 123); |
|
| 45 | + $this->config |
|
| 46 | + ->expects($this->once()) |
|
| 47 | + ->method('getSystemValueBool') |
|
| 48 | + ->with('config_is_read_only') |
|
| 49 | + ->willReturn(false); |
|
| 50 | + $this->config |
|
| 51 | + ->expects($this->never()) |
|
| 52 | + ->method('deleteSystemValue'); |
|
| 53 | 53 | |
| 54 | - static::invokePrivate($this->resetTokenBackgroundJob, 'run', [null]); |
|
| 55 | - } |
|
| 54 | + static::invokePrivate($this->resetTokenBackgroundJob, 'run', [null]); |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | - public function testRunWithExpiredToken(): void { |
|
| 58 | - $this->timeFactory |
|
| 59 | - ->expects($this->once()) |
|
| 60 | - ->method('getTime') |
|
| 61 | - ->willReturn(1455045234); |
|
| 62 | - $this->appConfig |
|
| 63 | - ->expects($this->once()) |
|
| 64 | - ->method('getValueInt') |
|
| 65 | - ->with('core', 'updater.secret.created', 1455045234) |
|
| 66 | - ->willReturn(2 * 24 * 60 * 60 + 1); // over 2 days |
|
| 67 | - $this->config |
|
| 68 | - ->expects($this->once()) |
|
| 69 | - ->method('deleteSystemValue') |
|
| 70 | - ->with('updater.secret'); |
|
| 57 | + public function testRunWithExpiredToken(): void { |
|
| 58 | + $this->timeFactory |
|
| 59 | + ->expects($this->once()) |
|
| 60 | + ->method('getTime') |
|
| 61 | + ->willReturn(1455045234); |
|
| 62 | + $this->appConfig |
|
| 63 | + ->expects($this->once()) |
|
| 64 | + ->method('getValueInt') |
|
| 65 | + ->with('core', 'updater.secret.created', 1455045234) |
|
| 66 | + ->willReturn(2 * 24 * 60 * 60 + 1); // over 2 days |
|
| 67 | + $this->config |
|
| 68 | + ->expects($this->once()) |
|
| 69 | + ->method('deleteSystemValue') |
|
| 70 | + ->with('updater.secret'); |
|
| 71 | 71 | |
| 72 | - static::invokePrivate($this->resetTokenBackgroundJob, 'run', [null]); |
|
| 73 | - } |
|
| 72 | + static::invokePrivate($this->resetTokenBackgroundJob, 'run', [null]); |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - public function testRunWithExpiredTokenAndReadOnlyConfigFile(): void { |
|
| 76 | - $this->timeFactory |
|
| 77 | - ->expects($this->never()) |
|
| 78 | - ->method('getTime'); |
|
| 79 | - $this->appConfig |
|
| 80 | - ->expects($this->never()) |
|
| 81 | - ->method('getValueInt'); |
|
| 82 | - $this->config |
|
| 83 | - ->expects($this->once()) |
|
| 84 | - ->method('getSystemValueBool') |
|
| 85 | - ->with('config_is_read_only') |
|
| 86 | - ->willReturn(true); |
|
| 87 | - $this->config |
|
| 88 | - ->expects($this->never()) |
|
| 89 | - ->method('deleteSystemValue'); |
|
| 75 | + public function testRunWithExpiredTokenAndReadOnlyConfigFile(): void { |
|
| 76 | + $this->timeFactory |
|
| 77 | + ->expects($this->never()) |
|
| 78 | + ->method('getTime'); |
|
| 79 | + $this->appConfig |
|
| 80 | + ->expects($this->never()) |
|
| 81 | + ->method('getValueInt'); |
|
| 82 | + $this->config |
|
| 83 | + ->expects($this->once()) |
|
| 84 | + ->method('getSystemValueBool') |
|
| 85 | + ->with('config_is_read_only') |
|
| 86 | + ->willReturn(true); |
|
| 87 | + $this->config |
|
| 88 | + ->expects($this->never()) |
|
| 89 | + ->method('deleteSystemValue'); |
|
| 90 | 90 | |
| 91 | - static::invokePrivate($this->resetTokenBackgroundJob, 'run', [null]); |
|
| 92 | - } |
|
| 91 | + static::invokePrivate($this->resetTokenBackgroundJob, 'run', [null]); |
|
| 92 | + } |
|
| 93 | 93 | } |
@@ -25,426 +25,426 @@ |
||
| 25 | 25 | use Test\TestCase; |
| 26 | 26 | |
| 27 | 27 | class UpdateAvailableNotificationsTest extends TestCase { |
| 28 | - private ServerVersion&MockObject $serverVersion; |
|
| 29 | - private IConfig&MockObject $config; |
|
| 30 | - private IManager&MockObject $notificationManager; |
|
| 31 | - private IGroupManager&MockObject $groupManager; |
|
| 32 | - private IAppManager&MockObject $appManager; |
|
| 33 | - private IAppConfig&MockObject $appConfig; |
|
| 34 | - private ITimeFactory&MockObject $timeFactory; |
|
| 35 | - private Installer&MockObject $installer; |
|
| 36 | - private VersionCheck&MockObject $versionCheck; |
|
| 37 | - |
|
| 38 | - protected function setUp(): void { |
|
| 39 | - parent::setUp(); |
|
| 40 | - |
|
| 41 | - $this->serverVersion = $this->createMock(ServerVersion::class); |
|
| 42 | - $this->config = $this->createMock(IConfig::class); |
|
| 43 | - $this->appConfig = $this->createMock(IAppConfig::class); |
|
| 44 | - $this->notificationManager = $this->createMock(IManager::class); |
|
| 45 | - $this->groupManager = $this->createMock(IGroupManager::class); |
|
| 46 | - $this->appManager = $this->createMock(IAppManager::class); |
|
| 47 | - $this->timeFactory = $this->createMock(ITimeFactory::class); |
|
| 48 | - $this->installer = $this->createMock(Installer::class); |
|
| 49 | - $this->versionCheck = $this->createMock(VersionCheck::class); |
|
| 50 | - } |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * @return UpdateAvailableNotifications|MockObject |
|
| 54 | - */ |
|
| 55 | - protected function getJob(array $methods = []): UpdateAvailableNotifications { |
|
| 56 | - if (empty($methods)) { |
|
| 57 | - return new UpdateAvailableNotifications( |
|
| 58 | - $this->timeFactory, |
|
| 59 | - $this->serverVersion, |
|
| 60 | - $this->config, |
|
| 61 | - $this->appConfig, |
|
| 62 | - $this->notificationManager, |
|
| 63 | - $this->groupManager, |
|
| 64 | - $this->appManager, |
|
| 65 | - $this->installer, |
|
| 66 | - $this->versionCheck, |
|
| 67 | - ); |
|
| 68 | - } |
|
| 69 | - { |
|
| 70 | - return $this->getMockBuilder(UpdateAvailableNotifications::class) |
|
| 71 | - ->setConstructorArgs([ |
|
| 72 | - $this->timeFactory, |
|
| 73 | - $this->serverVersion, |
|
| 74 | - $this->config, |
|
| 75 | - $this->appConfig, |
|
| 76 | - $this->notificationManager, |
|
| 77 | - $this->groupManager, |
|
| 78 | - $this->appManager, |
|
| 79 | - $this->installer, |
|
| 80 | - $this->versionCheck, |
|
| 81 | - ]) |
|
| 82 | - ->onlyMethods($methods) |
|
| 83 | - ->getMock(); |
|
| 84 | - } |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - public function testRun(): void { |
|
| 88 | - $job = $this->getJob([ |
|
| 89 | - 'checkCoreUpdate', |
|
| 90 | - 'checkAppUpdates', |
|
| 91 | - ]); |
|
| 92 | - |
|
| 93 | - $job->expects($this->once()) |
|
| 94 | - ->method('checkCoreUpdate'); |
|
| 95 | - $job->expects($this->once()) |
|
| 96 | - ->method('checkAppUpdates'); |
|
| 97 | - |
|
| 98 | - $this->config->expects(self::exactly(2)) |
|
| 99 | - ->method('getSystemValueBool') |
|
| 100 | - ->willReturnMap([ |
|
| 101 | - ['debug', false, true], |
|
| 102 | - ['has_internet_connection', true, true], |
|
| 103 | - ]); |
|
| 104 | - self::invokePrivate($job, 'run', [null]); |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - public function testRunNoInternet(): void { |
|
| 108 | - $job = $this->getJob([ |
|
| 109 | - 'checkCoreUpdate', |
|
| 110 | - 'checkAppUpdates', |
|
| 111 | - ]); |
|
| 112 | - |
|
| 113 | - $job->expects($this->never()) |
|
| 114 | - ->method('checkCoreUpdate'); |
|
| 115 | - $job->expects($this->never()) |
|
| 116 | - ->method('checkAppUpdates'); |
|
| 117 | - |
|
| 118 | - $this->config |
|
| 119 | - ->expects(self::once()) |
|
| 120 | - ->method('getSystemValueBool') |
|
| 121 | - ->with('has_internet_connection', true) |
|
| 122 | - ->willReturn(false); |
|
| 123 | - |
|
| 124 | - self::invokePrivate($job, 'run', [null]); |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - public static function dataCheckCoreUpdate(): array { |
|
| 128 | - return [ |
|
| 129 | - ['daily', null, null, null, null], |
|
| 130 | - ['git', null, null, null, null], |
|
| 131 | - ['beta', [], null, null, null], |
|
| 132 | - ['beta', false, false, null, null], |
|
| 133 | - ['beta', false, false, null, 13], |
|
| 134 | - ['beta', [ |
|
| 135 | - 'version' => '9.2.0', |
|
| 136 | - 'versionstring' => 'Nextcloud 11.0.0', |
|
| 137 | - ], '9.2.0', 'Nextcloud 11.0.0', null], |
|
| 138 | - ['stable', [], null, null, null], |
|
| 139 | - ['stable', false, false, null, null], |
|
| 140 | - ['stable', false, false, null, 6], |
|
| 141 | - ['stable', [ |
|
| 142 | - 'version' => '9.2.0', |
|
| 143 | - 'versionstring' => 'Nextcloud 11.0.0', |
|
| 144 | - ], '9.2.0', 'Nextcloud 11.0.0', null], |
|
| 145 | - ['production', [], null, null, null], |
|
| 146 | - ['production', false, false, null, null], |
|
| 147 | - ['production', false, false, null, 2], |
|
| 148 | - ['production', [ |
|
| 149 | - 'version' => '9.2.0', |
|
| 150 | - 'versionstring' => 'Nextcloud 11.0.0', |
|
| 151 | - ], '9.2.0', 'Nextcloud 11.0.0', null], |
|
| 152 | - ]; |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - /** |
|
| 156 | - * @dataProvider dataCheckCoreUpdate |
|
| 157 | - */ |
|
| 158 | - public function testCheckCoreUpdate(string $channel, mixed $versionCheck, mixed $version, ?string $readableVersion, ?int $errorDays): void { |
|
| 159 | - $job = $this->getJob([ |
|
| 160 | - 'createNotifications', |
|
| 161 | - 'clearErrorNotifications', |
|
| 162 | - 'sendErrorNotifications', |
|
| 163 | - ]); |
|
| 164 | - |
|
| 165 | - $this->serverVersion->expects($this->once()) |
|
| 166 | - ->method('getChannel') |
|
| 167 | - ->willReturn($channel); |
|
| 168 | - |
|
| 169 | - if ($versionCheck === null) { |
|
| 170 | - $this->versionCheck->expects($this->never()) |
|
| 171 | - ->method('check'); |
|
| 172 | - } else { |
|
| 173 | - $this->versionCheck->expects($this->once()) |
|
| 174 | - ->method('check') |
|
| 175 | - ->willReturn($versionCheck); |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - if ($version === null) { |
|
| 179 | - $job->expects($this->never()) |
|
| 180 | - ->method('createNotifications'); |
|
| 181 | - $job->expects($versionCheck === null ? $this->never() : $this->once()) |
|
| 182 | - ->method('clearErrorNotifications'); |
|
| 183 | - } elseif ($version === false) { |
|
| 184 | - $job->expects($this->never()) |
|
| 185 | - ->method('createNotifications'); |
|
| 186 | - $job->expects($this->never()) |
|
| 187 | - ->method('clearErrorNotifications'); |
|
| 188 | - |
|
| 189 | - $this->appConfig->expects($this->once()) |
|
| 190 | - ->method('getAppValueInt') |
|
| 191 | - ->willReturn($errorDays ?? 0); |
|
| 192 | - $this->appConfig->expects($this->once()) |
|
| 193 | - ->method('setAppValueInt') |
|
| 194 | - ->with('update_check_errors', $errorDays + 1); |
|
| 195 | - $job->expects($errorDays !== null ? $this->once() : $this->never()) |
|
| 196 | - ->method('sendErrorNotifications') |
|
| 197 | - ->with($errorDays + 1); |
|
| 198 | - } else { |
|
| 199 | - $this->appConfig->expects($this->once()) |
|
| 200 | - ->method('setAppValueInt') |
|
| 201 | - ->with('update_check_errors', 0); |
|
| 202 | - $job->expects($this->once()) |
|
| 203 | - ->method('clearErrorNotifications'); |
|
| 204 | - $job->expects($this->once()) |
|
| 205 | - ->method('createNotifications') |
|
| 206 | - ->with('core', $version, $readableVersion); |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - $this->config->expects(self::any()) |
|
| 210 | - ->method('getSystemValueBool') |
|
| 211 | - ->willReturnMap([ |
|
| 212 | - ['updatechecker', true, true], |
|
| 213 | - ['has_internet_connection', true, true], |
|
| 214 | - ]); |
|
| 215 | - |
|
| 216 | - self::invokePrivate($job, 'checkCoreUpdate'); |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - public static function dataCheckAppUpdates(): array { |
|
| 220 | - return [ |
|
| 221 | - [ |
|
| 222 | - ['app1', 'app2'], |
|
| 223 | - [ |
|
| 224 | - ['app1', false], |
|
| 225 | - ['app2', '1.9.2'], |
|
| 226 | - ], |
|
| 227 | - [ |
|
| 228 | - ['app2', '1.9.2', ''], |
|
| 229 | - ], |
|
| 230 | - ], |
|
| 231 | - ]; |
|
| 232 | - } |
|
| 233 | - |
|
| 234 | - /** |
|
| 235 | - * @dataProvider dataCheckAppUpdates |
|
| 236 | - */ |
|
| 237 | - public function testCheckAppUpdates(array $apps, array $isUpdateAvailable, array $notifications): void { |
|
| 238 | - $job = $this->getJob([ |
|
| 239 | - 'isUpdateAvailable', |
|
| 240 | - 'createNotifications', |
|
| 241 | - ]); |
|
| 242 | - |
|
| 243 | - $this->appManager->expects($this->once()) |
|
| 244 | - ->method('getEnabledApps') |
|
| 245 | - ->willReturn($apps); |
|
| 246 | - |
|
| 247 | - $job->expects($this->exactly(\count($apps))) |
|
| 248 | - ->method('isUpdateAvailable') |
|
| 249 | - ->willReturnMap($isUpdateAvailable); |
|
| 250 | - |
|
| 251 | - $i = 0; |
|
| 252 | - $job->expects($this->exactly(\count($notifications))) |
|
| 253 | - ->method('createNotifications') |
|
| 254 | - ->willReturnCallback(function () use ($notifications, &$i): void { |
|
| 255 | - $this->assertEquals($notifications[$i], func_get_args()); |
|
| 256 | - $i++; |
|
| 257 | - }); |
|
| 258 | - |
|
| 259 | - |
|
| 260 | - self::invokePrivate($job, 'checkAppUpdates'); |
|
| 261 | - } |
|
| 262 | - |
|
| 263 | - public static function dataCreateNotifications(): array { |
|
| 264 | - return [ |
|
| 265 | - ['app1', '1.0.0', '1.0.0', false, false, null, null], |
|
| 266 | - ['app2', '1.0.1', '1.0.0', '1.0.0', true, ['user1'], [['user1']]], |
|
| 267 | - ['app3', '1.0.1', false, false, true, ['user2', 'user3'], [['user2'], ['user3']]], |
|
| 268 | - ]; |
|
| 269 | - } |
|
| 270 | - |
|
| 271 | - /** |
|
| 272 | - * @dataProvider dataCreateNotifications |
|
| 273 | - */ |
|
| 274 | - public function testCreateNotifications(string $app, string $version, string|false $lastNotification, string|false $callDelete, bool $createNotification, ?array $users, ?array $userNotifications): void { |
|
| 275 | - $job = $this->getJob([ |
|
| 276 | - 'deleteOutdatedNotifications', |
|
| 277 | - 'getUsersToNotify', |
|
| 278 | - ]); |
|
| 279 | - |
|
| 280 | - $this->appConfig->expects($this->once()) |
|
| 281 | - ->method('getAppValueString') |
|
| 282 | - ->with($app, '') |
|
| 283 | - ->willReturn($lastNotification ?: ''); |
|
| 284 | - |
|
| 285 | - if ($lastNotification !== $version) { |
|
| 286 | - $this->appConfig->expects($this->once()) |
|
| 287 | - ->method('setAppValueString') |
|
| 288 | - ->with($app, $version); |
|
| 289 | - } |
|
| 290 | - |
|
| 291 | - if ($callDelete === false) { |
|
| 292 | - $job->expects($this->never()) |
|
| 293 | - ->method('deleteOutdatedNotifications'); |
|
| 294 | - } else { |
|
| 295 | - $job->expects($this->once()) |
|
| 296 | - ->method('deleteOutdatedNotifications') |
|
| 297 | - ->with($app, $callDelete); |
|
| 298 | - } |
|
| 299 | - |
|
| 300 | - if ($users === null) { |
|
| 301 | - $job->expects($this->never()) |
|
| 302 | - ->method('getUsersToNotify'); |
|
| 303 | - } else { |
|
| 304 | - $job->expects($this->once()) |
|
| 305 | - ->method('getUsersToNotify') |
|
| 306 | - ->willReturn($users); |
|
| 307 | - } |
|
| 308 | - |
|
| 309 | - if ($createNotification) { |
|
| 310 | - $notification = $this->createMock(INotification::class); |
|
| 311 | - $notification->expects($this->once()) |
|
| 312 | - ->method('setApp') |
|
| 313 | - ->with('updatenotification') |
|
| 314 | - ->willReturnSelf(); |
|
| 315 | - $notification->expects($this->once()) |
|
| 316 | - ->method('setDateTime') |
|
| 317 | - ->willReturnSelf(); |
|
| 318 | - $notification->expects($this->once()) |
|
| 319 | - ->method('setObject') |
|
| 320 | - ->with($app, $version) |
|
| 321 | - ->willReturnSelf(); |
|
| 322 | - $notification->expects($this->once()) |
|
| 323 | - ->method('setSubject') |
|
| 324 | - ->with('update_available') |
|
| 325 | - ->willReturnSelf(); |
|
| 326 | - |
|
| 327 | - if ($userNotifications !== null) { |
|
| 328 | - $notification->expects($this->exactly(\count($userNotifications))) |
|
| 329 | - ->method('setUser') |
|
| 330 | - ->willReturnSelf(); |
|
| 331 | - |
|
| 332 | - $this->notificationManager->expects($this->exactly(\count($userNotifications))) |
|
| 333 | - ->method('notify'); |
|
| 334 | - } |
|
| 335 | - |
|
| 336 | - $this->notificationManager->expects($this->once()) |
|
| 337 | - ->method('createNotification') |
|
| 338 | - ->willReturn($notification); |
|
| 339 | - } else { |
|
| 340 | - $this->notificationManager->expects($this->never()) |
|
| 341 | - ->method('createNotification'); |
|
| 342 | - } |
|
| 343 | - |
|
| 344 | - self::invokePrivate($job, 'createNotifications', [$app, $version]); |
|
| 345 | - } |
|
| 346 | - |
|
| 347 | - public static function dataGetUsersToNotify(): array { |
|
| 348 | - return [ |
|
| 349 | - [['g1', 'g2'], ['g1' => null, 'g2' => ['u1', 'u2']], ['u1', 'u2']], |
|
| 350 | - [['g3', 'g4'], ['g3' => ['u1', 'u2'], 'g4' => ['u2', 'u3']], ['u1', 'u2', 'u3']], |
|
| 351 | - ]; |
|
| 352 | - } |
|
| 353 | - |
|
| 354 | - /** |
|
| 355 | - * @dataProvider dataGetUsersToNotify |
|
| 356 | - */ |
|
| 357 | - public function testGetUsersToNotify(array $groups, array $groupUsers, array $expected): void { |
|
| 358 | - $job = $this->getJob(); |
|
| 359 | - |
|
| 360 | - $this->appConfig->expects($this->once()) |
|
| 361 | - ->method('getAppValueArray') |
|
| 362 | - ->with('notify_groups', ['admin']) |
|
| 363 | - ->willReturn($groups); |
|
| 364 | - |
|
| 365 | - $groupMap = []; |
|
| 366 | - foreach ($groupUsers as $gid => $uids) { |
|
| 367 | - if ($uids === null) { |
|
| 368 | - $group = null; |
|
| 369 | - } else { |
|
| 370 | - $group = $this->getGroup($gid); |
|
| 371 | - $group->expects($this->any()) |
|
| 372 | - ->method('getUsers') |
|
| 373 | - ->willReturn($this->getUsers($uids)); |
|
| 374 | - } |
|
| 375 | - $groupMap[] = [$gid, $group]; |
|
| 376 | - } |
|
| 377 | - $this->groupManager->expects($this->exactly(\count($groups))) |
|
| 378 | - ->method('get') |
|
| 379 | - ->willReturnMap($groupMap); |
|
| 380 | - |
|
| 381 | - $result = self::invokePrivate($job, 'getUsersToNotify'); |
|
| 382 | - $this->assertEquals($expected, $result); |
|
| 383 | - |
|
| 384 | - // Test caching |
|
| 385 | - $result = self::invokePrivate($job, 'getUsersToNotify'); |
|
| 386 | - $this->assertEquals($expected, $result); |
|
| 387 | - } |
|
| 388 | - |
|
| 389 | - public static function dataDeleteOutdatedNotifications(): array { |
|
| 390 | - return [ |
|
| 391 | - ['app1', '1.1.0'], |
|
| 392 | - ['app2', '1.2.0'], |
|
| 393 | - ]; |
|
| 394 | - } |
|
| 395 | - |
|
| 396 | - /** |
|
| 397 | - * @dataProvider dataDeleteOutdatedNotifications |
|
| 398 | - * @param string $app |
|
| 399 | - * @param string $version |
|
| 400 | - */ |
|
| 401 | - public function testDeleteOutdatedNotifications(string $app, string $version): void { |
|
| 402 | - $notification = $this->createMock(INotification::class); |
|
| 403 | - $notification->expects($this->once()) |
|
| 404 | - ->method('setApp') |
|
| 405 | - ->with('updatenotification') |
|
| 406 | - ->willReturnSelf(); |
|
| 407 | - $notification->expects($this->once()) |
|
| 408 | - ->method('setObject') |
|
| 409 | - ->with($app, $version) |
|
| 410 | - ->willReturnSelf(); |
|
| 411 | - |
|
| 412 | - $this->notificationManager->expects($this->once()) |
|
| 413 | - ->method('createNotification') |
|
| 414 | - ->willReturn($notification); |
|
| 415 | - $this->notificationManager->expects($this->once()) |
|
| 416 | - ->method('markProcessed') |
|
| 417 | - ->with($notification); |
|
| 418 | - |
|
| 419 | - $job = $this->getJob(); |
|
| 420 | - self::invokePrivate($job, 'deleteOutdatedNotifications', [$app, $version]); |
|
| 421 | - } |
|
| 422 | - |
|
| 423 | - /** |
|
| 424 | - * @param string[] $userIds |
|
| 425 | - * @return IUser[]|MockObject[] |
|
| 426 | - */ |
|
| 427 | - protected function getUsers(array $userIds): array { |
|
| 428 | - $users = []; |
|
| 429 | - foreach ($userIds as $uid) { |
|
| 430 | - $user = $this->createMock(IUser::class); |
|
| 431 | - $user->expects($this->any()) |
|
| 432 | - ->method('getUID') |
|
| 433 | - ->willReturn($uid); |
|
| 434 | - $users[] = $user; |
|
| 435 | - } |
|
| 436 | - return $users; |
|
| 437 | - } |
|
| 438 | - |
|
| 439 | - /** |
|
| 440 | - * @param string $gid |
|
| 441 | - * @return IGroup|MockObject |
|
| 442 | - */ |
|
| 443 | - protected function getGroup(string $gid) { |
|
| 444 | - $group = $this->createMock(IGroup::class); |
|
| 445 | - $group->expects($this->any()) |
|
| 446 | - ->method('getGID') |
|
| 447 | - ->willReturn($gid); |
|
| 448 | - return $group; |
|
| 449 | - } |
|
| 28 | + private ServerVersion&MockObject $serverVersion; |
|
| 29 | + private IConfig&MockObject $config; |
|
| 30 | + private IManager&MockObject $notificationManager; |
|
| 31 | + private IGroupManager&MockObject $groupManager; |
|
| 32 | + private IAppManager&MockObject $appManager; |
|
| 33 | + private IAppConfig&MockObject $appConfig; |
|
| 34 | + private ITimeFactory&MockObject $timeFactory; |
|
| 35 | + private Installer&MockObject $installer; |
|
| 36 | + private VersionCheck&MockObject $versionCheck; |
|
| 37 | + |
|
| 38 | + protected function setUp(): void { |
|
| 39 | + parent::setUp(); |
|
| 40 | + |
|
| 41 | + $this->serverVersion = $this->createMock(ServerVersion::class); |
|
| 42 | + $this->config = $this->createMock(IConfig::class); |
|
| 43 | + $this->appConfig = $this->createMock(IAppConfig::class); |
|
| 44 | + $this->notificationManager = $this->createMock(IManager::class); |
|
| 45 | + $this->groupManager = $this->createMock(IGroupManager::class); |
|
| 46 | + $this->appManager = $this->createMock(IAppManager::class); |
|
| 47 | + $this->timeFactory = $this->createMock(ITimeFactory::class); |
|
| 48 | + $this->installer = $this->createMock(Installer::class); |
|
| 49 | + $this->versionCheck = $this->createMock(VersionCheck::class); |
|
| 50 | + } |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * @return UpdateAvailableNotifications|MockObject |
|
| 54 | + */ |
|
| 55 | + protected function getJob(array $methods = []): UpdateAvailableNotifications { |
|
| 56 | + if (empty($methods)) { |
|
| 57 | + return new UpdateAvailableNotifications( |
|
| 58 | + $this->timeFactory, |
|
| 59 | + $this->serverVersion, |
|
| 60 | + $this->config, |
|
| 61 | + $this->appConfig, |
|
| 62 | + $this->notificationManager, |
|
| 63 | + $this->groupManager, |
|
| 64 | + $this->appManager, |
|
| 65 | + $this->installer, |
|
| 66 | + $this->versionCheck, |
|
| 67 | + ); |
|
| 68 | + } |
|
| 69 | + { |
|
| 70 | + return $this->getMockBuilder(UpdateAvailableNotifications::class) |
|
| 71 | + ->setConstructorArgs([ |
|
| 72 | + $this->timeFactory, |
|
| 73 | + $this->serverVersion, |
|
| 74 | + $this->config, |
|
| 75 | + $this->appConfig, |
|
| 76 | + $this->notificationManager, |
|
| 77 | + $this->groupManager, |
|
| 78 | + $this->appManager, |
|
| 79 | + $this->installer, |
|
| 80 | + $this->versionCheck, |
|
| 81 | + ]) |
|
| 82 | + ->onlyMethods($methods) |
|
| 83 | + ->getMock(); |
|
| 84 | + } |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + public function testRun(): void { |
|
| 88 | + $job = $this->getJob([ |
|
| 89 | + 'checkCoreUpdate', |
|
| 90 | + 'checkAppUpdates', |
|
| 91 | + ]); |
|
| 92 | + |
|
| 93 | + $job->expects($this->once()) |
|
| 94 | + ->method('checkCoreUpdate'); |
|
| 95 | + $job->expects($this->once()) |
|
| 96 | + ->method('checkAppUpdates'); |
|
| 97 | + |
|
| 98 | + $this->config->expects(self::exactly(2)) |
|
| 99 | + ->method('getSystemValueBool') |
|
| 100 | + ->willReturnMap([ |
|
| 101 | + ['debug', false, true], |
|
| 102 | + ['has_internet_connection', true, true], |
|
| 103 | + ]); |
|
| 104 | + self::invokePrivate($job, 'run', [null]); |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + public function testRunNoInternet(): void { |
|
| 108 | + $job = $this->getJob([ |
|
| 109 | + 'checkCoreUpdate', |
|
| 110 | + 'checkAppUpdates', |
|
| 111 | + ]); |
|
| 112 | + |
|
| 113 | + $job->expects($this->never()) |
|
| 114 | + ->method('checkCoreUpdate'); |
|
| 115 | + $job->expects($this->never()) |
|
| 116 | + ->method('checkAppUpdates'); |
|
| 117 | + |
|
| 118 | + $this->config |
|
| 119 | + ->expects(self::once()) |
|
| 120 | + ->method('getSystemValueBool') |
|
| 121 | + ->with('has_internet_connection', true) |
|
| 122 | + ->willReturn(false); |
|
| 123 | + |
|
| 124 | + self::invokePrivate($job, 'run', [null]); |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + public static function dataCheckCoreUpdate(): array { |
|
| 128 | + return [ |
|
| 129 | + ['daily', null, null, null, null], |
|
| 130 | + ['git', null, null, null, null], |
|
| 131 | + ['beta', [], null, null, null], |
|
| 132 | + ['beta', false, false, null, null], |
|
| 133 | + ['beta', false, false, null, 13], |
|
| 134 | + ['beta', [ |
|
| 135 | + 'version' => '9.2.0', |
|
| 136 | + 'versionstring' => 'Nextcloud 11.0.0', |
|
| 137 | + ], '9.2.0', 'Nextcloud 11.0.0', null], |
|
| 138 | + ['stable', [], null, null, null], |
|
| 139 | + ['stable', false, false, null, null], |
|
| 140 | + ['stable', false, false, null, 6], |
|
| 141 | + ['stable', [ |
|
| 142 | + 'version' => '9.2.0', |
|
| 143 | + 'versionstring' => 'Nextcloud 11.0.0', |
|
| 144 | + ], '9.2.0', 'Nextcloud 11.0.0', null], |
|
| 145 | + ['production', [], null, null, null], |
|
| 146 | + ['production', false, false, null, null], |
|
| 147 | + ['production', false, false, null, 2], |
|
| 148 | + ['production', [ |
|
| 149 | + 'version' => '9.2.0', |
|
| 150 | + 'versionstring' => 'Nextcloud 11.0.0', |
|
| 151 | + ], '9.2.0', 'Nextcloud 11.0.0', null], |
|
| 152 | + ]; |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + /** |
|
| 156 | + * @dataProvider dataCheckCoreUpdate |
|
| 157 | + */ |
|
| 158 | + public function testCheckCoreUpdate(string $channel, mixed $versionCheck, mixed $version, ?string $readableVersion, ?int $errorDays): void { |
|
| 159 | + $job = $this->getJob([ |
|
| 160 | + 'createNotifications', |
|
| 161 | + 'clearErrorNotifications', |
|
| 162 | + 'sendErrorNotifications', |
|
| 163 | + ]); |
|
| 164 | + |
|
| 165 | + $this->serverVersion->expects($this->once()) |
|
| 166 | + ->method('getChannel') |
|
| 167 | + ->willReturn($channel); |
|
| 168 | + |
|
| 169 | + if ($versionCheck === null) { |
|
| 170 | + $this->versionCheck->expects($this->never()) |
|
| 171 | + ->method('check'); |
|
| 172 | + } else { |
|
| 173 | + $this->versionCheck->expects($this->once()) |
|
| 174 | + ->method('check') |
|
| 175 | + ->willReturn($versionCheck); |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + if ($version === null) { |
|
| 179 | + $job->expects($this->never()) |
|
| 180 | + ->method('createNotifications'); |
|
| 181 | + $job->expects($versionCheck === null ? $this->never() : $this->once()) |
|
| 182 | + ->method('clearErrorNotifications'); |
|
| 183 | + } elseif ($version === false) { |
|
| 184 | + $job->expects($this->never()) |
|
| 185 | + ->method('createNotifications'); |
|
| 186 | + $job->expects($this->never()) |
|
| 187 | + ->method('clearErrorNotifications'); |
|
| 188 | + |
|
| 189 | + $this->appConfig->expects($this->once()) |
|
| 190 | + ->method('getAppValueInt') |
|
| 191 | + ->willReturn($errorDays ?? 0); |
|
| 192 | + $this->appConfig->expects($this->once()) |
|
| 193 | + ->method('setAppValueInt') |
|
| 194 | + ->with('update_check_errors', $errorDays + 1); |
|
| 195 | + $job->expects($errorDays !== null ? $this->once() : $this->never()) |
|
| 196 | + ->method('sendErrorNotifications') |
|
| 197 | + ->with($errorDays + 1); |
|
| 198 | + } else { |
|
| 199 | + $this->appConfig->expects($this->once()) |
|
| 200 | + ->method('setAppValueInt') |
|
| 201 | + ->with('update_check_errors', 0); |
|
| 202 | + $job->expects($this->once()) |
|
| 203 | + ->method('clearErrorNotifications'); |
|
| 204 | + $job->expects($this->once()) |
|
| 205 | + ->method('createNotifications') |
|
| 206 | + ->with('core', $version, $readableVersion); |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + $this->config->expects(self::any()) |
|
| 210 | + ->method('getSystemValueBool') |
|
| 211 | + ->willReturnMap([ |
|
| 212 | + ['updatechecker', true, true], |
|
| 213 | + ['has_internet_connection', true, true], |
|
| 214 | + ]); |
|
| 215 | + |
|
| 216 | + self::invokePrivate($job, 'checkCoreUpdate'); |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + public static function dataCheckAppUpdates(): array { |
|
| 220 | + return [ |
|
| 221 | + [ |
|
| 222 | + ['app1', 'app2'], |
|
| 223 | + [ |
|
| 224 | + ['app1', false], |
|
| 225 | + ['app2', '1.9.2'], |
|
| 226 | + ], |
|
| 227 | + [ |
|
| 228 | + ['app2', '1.9.2', ''], |
|
| 229 | + ], |
|
| 230 | + ], |
|
| 231 | + ]; |
|
| 232 | + } |
|
| 233 | + |
|
| 234 | + /** |
|
| 235 | + * @dataProvider dataCheckAppUpdates |
|
| 236 | + */ |
|
| 237 | + public function testCheckAppUpdates(array $apps, array $isUpdateAvailable, array $notifications): void { |
|
| 238 | + $job = $this->getJob([ |
|
| 239 | + 'isUpdateAvailable', |
|
| 240 | + 'createNotifications', |
|
| 241 | + ]); |
|
| 242 | + |
|
| 243 | + $this->appManager->expects($this->once()) |
|
| 244 | + ->method('getEnabledApps') |
|
| 245 | + ->willReturn($apps); |
|
| 246 | + |
|
| 247 | + $job->expects($this->exactly(\count($apps))) |
|
| 248 | + ->method('isUpdateAvailable') |
|
| 249 | + ->willReturnMap($isUpdateAvailable); |
|
| 250 | + |
|
| 251 | + $i = 0; |
|
| 252 | + $job->expects($this->exactly(\count($notifications))) |
|
| 253 | + ->method('createNotifications') |
|
| 254 | + ->willReturnCallback(function () use ($notifications, &$i): void { |
|
| 255 | + $this->assertEquals($notifications[$i], func_get_args()); |
|
| 256 | + $i++; |
|
| 257 | + }); |
|
| 258 | + |
|
| 259 | + |
|
| 260 | + self::invokePrivate($job, 'checkAppUpdates'); |
|
| 261 | + } |
|
| 262 | + |
|
| 263 | + public static function dataCreateNotifications(): array { |
|
| 264 | + return [ |
|
| 265 | + ['app1', '1.0.0', '1.0.0', false, false, null, null], |
|
| 266 | + ['app2', '1.0.1', '1.0.0', '1.0.0', true, ['user1'], [['user1']]], |
|
| 267 | + ['app3', '1.0.1', false, false, true, ['user2', 'user3'], [['user2'], ['user3']]], |
|
| 268 | + ]; |
|
| 269 | + } |
|
| 270 | + |
|
| 271 | + /** |
|
| 272 | + * @dataProvider dataCreateNotifications |
|
| 273 | + */ |
|
| 274 | + public function testCreateNotifications(string $app, string $version, string|false $lastNotification, string|false $callDelete, bool $createNotification, ?array $users, ?array $userNotifications): void { |
|
| 275 | + $job = $this->getJob([ |
|
| 276 | + 'deleteOutdatedNotifications', |
|
| 277 | + 'getUsersToNotify', |
|
| 278 | + ]); |
|
| 279 | + |
|
| 280 | + $this->appConfig->expects($this->once()) |
|
| 281 | + ->method('getAppValueString') |
|
| 282 | + ->with($app, '') |
|
| 283 | + ->willReturn($lastNotification ?: ''); |
|
| 284 | + |
|
| 285 | + if ($lastNotification !== $version) { |
|
| 286 | + $this->appConfig->expects($this->once()) |
|
| 287 | + ->method('setAppValueString') |
|
| 288 | + ->with($app, $version); |
|
| 289 | + } |
|
| 290 | + |
|
| 291 | + if ($callDelete === false) { |
|
| 292 | + $job->expects($this->never()) |
|
| 293 | + ->method('deleteOutdatedNotifications'); |
|
| 294 | + } else { |
|
| 295 | + $job->expects($this->once()) |
|
| 296 | + ->method('deleteOutdatedNotifications') |
|
| 297 | + ->with($app, $callDelete); |
|
| 298 | + } |
|
| 299 | + |
|
| 300 | + if ($users === null) { |
|
| 301 | + $job->expects($this->never()) |
|
| 302 | + ->method('getUsersToNotify'); |
|
| 303 | + } else { |
|
| 304 | + $job->expects($this->once()) |
|
| 305 | + ->method('getUsersToNotify') |
|
| 306 | + ->willReturn($users); |
|
| 307 | + } |
|
| 308 | + |
|
| 309 | + if ($createNotification) { |
|
| 310 | + $notification = $this->createMock(INotification::class); |
|
| 311 | + $notification->expects($this->once()) |
|
| 312 | + ->method('setApp') |
|
| 313 | + ->with('updatenotification') |
|
| 314 | + ->willReturnSelf(); |
|
| 315 | + $notification->expects($this->once()) |
|
| 316 | + ->method('setDateTime') |
|
| 317 | + ->willReturnSelf(); |
|
| 318 | + $notification->expects($this->once()) |
|
| 319 | + ->method('setObject') |
|
| 320 | + ->with($app, $version) |
|
| 321 | + ->willReturnSelf(); |
|
| 322 | + $notification->expects($this->once()) |
|
| 323 | + ->method('setSubject') |
|
| 324 | + ->with('update_available') |
|
| 325 | + ->willReturnSelf(); |
|
| 326 | + |
|
| 327 | + if ($userNotifications !== null) { |
|
| 328 | + $notification->expects($this->exactly(\count($userNotifications))) |
|
| 329 | + ->method('setUser') |
|
| 330 | + ->willReturnSelf(); |
|
| 331 | + |
|
| 332 | + $this->notificationManager->expects($this->exactly(\count($userNotifications))) |
|
| 333 | + ->method('notify'); |
|
| 334 | + } |
|
| 335 | + |
|
| 336 | + $this->notificationManager->expects($this->once()) |
|
| 337 | + ->method('createNotification') |
|
| 338 | + ->willReturn($notification); |
|
| 339 | + } else { |
|
| 340 | + $this->notificationManager->expects($this->never()) |
|
| 341 | + ->method('createNotification'); |
|
| 342 | + } |
|
| 343 | + |
|
| 344 | + self::invokePrivate($job, 'createNotifications', [$app, $version]); |
|
| 345 | + } |
|
| 346 | + |
|
| 347 | + public static function dataGetUsersToNotify(): array { |
|
| 348 | + return [ |
|
| 349 | + [['g1', 'g2'], ['g1' => null, 'g2' => ['u1', 'u2']], ['u1', 'u2']], |
|
| 350 | + [['g3', 'g4'], ['g3' => ['u1', 'u2'], 'g4' => ['u2', 'u3']], ['u1', 'u2', 'u3']], |
|
| 351 | + ]; |
|
| 352 | + } |
|
| 353 | + |
|
| 354 | + /** |
|
| 355 | + * @dataProvider dataGetUsersToNotify |
|
| 356 | + */ |
|
| 357 | + public function testGetUsersToNotify(array $groups, array $groupUsers, array $expected): void { |
|
| 358 | + $job = $this->getJob(); |
|
| 359 | + |
|
| 360 | + $this->appConfig->expects($this->once()) |
|
| 361 | + ->method('getAppValueArray') |
|
| 362 | + ->with('notify_groups', ['admin']) |
|
| 363 | + ->willReturn($groups); |
|
| 364 | + |
|
| 365 | + $groupMap = []; |
|
| 366 | + foreach ($groupUsers as $gid => $uids) { |
|
| 367 | + if ($uids === null) { |
|
| 368 | + $group = null; |
|
| 369 | + } else { |
|
| 370 | + $group = $this->getGroup($gid); |
|
| 371 | + $group->expects($this->any()) |
|
| 372 | + ->method('getUsers') |
|
| 373 | + ->willReturn($this->getUsers($uids)); |
|
| 374 | + } |
|
| 375 | + $groupMap[] = [$gid, $group]; |
|
| 376 | + } |
|
| 377 | + $this->groupManager->expects($this->exactly(\count($groups))) |
|
| 378 | + ->method('get') |
|
| 379 | + ->willReturnMap($groupMap); |
|
| 380 | + |
|
| 381 | + $result = self::invokePrivate($job, 'getUsersToNotify'); |
|
| 382 | + $this->assertEquals($expected, $result); |
|
| 383 | + |
|
| 384 | + // Test caching |
|
| 385 | + $result = self::invokePrivate($job, 'getUsersToNotify'); |
|
| 386 | + $this->assertEquals($expected, $result); |
|
| 387 | + } |
|
| 388 | + |
|
| 389 | + public static function dataDeleteOutdatedNotifications(): array { |
|
| 390 | + return [ |
|
| 391 | + ['app1', '1.1.0'], |
|
| 392 | + ['app2', '1.2.0'], |
|
| 393 | + ]; |
|
| 394 | + } |
|
| 395 | + |
|
| 396 | + /** |
|
| 397 | + * @dataProvider dataDeleteOutdatedNotifications |
|
| 398 | + * @param string $app |
|
| 399 | + * @param string $version |
|
| 400 | + */ |
|
| 401 | + public function testDeleteOutdatedNotifications(string $app, string $version): void { |
|
| 402 | + $notification = $this->createMock(INotification::class); |
|
| 403 | + $notification->expects($this->once()) |
|
| 404 | + ->method('setApp') |
|
| 405 | + ->with('updatenotification') |
|
| 406 | + ->willReturnSelf(); |
|
| 407 | + $notification->expects($this->once()) |
|
| 408 | + ->method('setObject') |
|
| 409 | + ->with($app, $version) |
|
| 410 | + ->willReturnSelf(); |
|
| 411 | + |
|
| 412 | + $this->notificationManager->expects($this->once()) |
|
| 413 | + ->method('createNotification') |
|
| 414 | + ->willReturn($notification); |
|
| 415 | + $this->notificationManager->expects($this->once()) |
|
| 416 | + ->method('markProcessed') |
|
| 417 | + ->with($notification); |
|
| 418 | + |
|
| 419 | + $job = $this->getJob(); |
|
| 420 | + self::invokePrivate($job, 'deleteOutdatedNotifications', [$app, $version]); |
|
| 421 | + } |
|
| 422 | + |
|
| 423 | + /** |
|
| 424 | + * @param string[] $userIds |
|
| 425 | + * @return IUser[]|MockObject[] |
|
| 426 | + */ |
|
| 427 | + protected function getUsers(array $userIds): array { |
|
| 428 | + $users = []; |
|
| 429 | + foreach ($userIds as $uid) { |
|
| 430 | + $user = $this->createMock(IUser::class); |
|
| 431 | + $user->expects($this->any()) |
|
| 432 | + ->method('getUID') |
|
| 433 | + ->willReturn($uid); |
|
| 434 | + $users[] = $user; |
|
| 435 | + } |
|
| 436 | + return $users; |
|
| 437 | + } |
|
| 438 | + |
|
| 439 | + /** |
|
| 440 | + * @param string $gid |
|
| 441 | + * @return IGroup|MockObject |
|
| 442 | + */ |
|
| 443 | + protected function getGroup(string $gid) { |
|
| 444 | + $group = $this->createMock(IGroup::class); |
|
| 445 | + $group->expects($this->any()) |
|
| 446 | + ->method('getGID') |
|
| 447 | + ->willReturn($gid); |
|
| 448 | + return $group; |
|
| 449 | + } |
|
| 450 | 450 | } |
@@ -251,7 +251,7 @@ discard block |
||
| 251 | 251 | $i = 0; |
| 252 | 252 | $job->expects($this->exactly(\count($notifications))) |
| 253 | 253 | ->method('createNotifications') |
| 254 | - ->willReturnCallback(function () use ($notifications, &$i): void { |
|
| 254 | + ->willReturnCallback(function() use ($notifications, &$i): void { |
|
| 255 | 255 | $this->assertEquals($notifications[$i], func_get_args()); |
| 256 | 256 | $i++; |
| 257 | 257 | }); |
@@ -271,7 +271,7 @@ discard block |
||
| 271 | 271 | /** |
| 272 | 272 | * @dataProvider dataCreateNotifications |
| 273 | 273 | */ |
| 274 | - public function testCreateNotifications(string $app, string $version, string|false $lastNotification, string|false $callDelete, bool $createNotification, ?array $users, ?array $userNotifications): void { |
|
| 274 | + public function testCreateNotifications(string $app, string $version, string | false $lastNotification, string | false $callDelete, bool $createNotification, ?array $users, ?array $userNotifications): void { |
|
| 275 | 275 | $job = $this->getJob([ |
| 276 | 276 | 'deleteOutdatedNotifications', |
| 277 | 277 | 'getUsersToNotify', |
@@ -19,88 +19,88 @@ |
||
| 19 | 19 | * @group DB |
| 20 | 20 | */ |
| 21 | 21 | class BackupCodeMapperTest extends TestCase { |
| 22 | - private IDBConnection $db; |
|
| 23 | - private BackupCodeMapper $mapper; |
|
| 24 | - private string $testUID = 'test123456'; |
|
| 25 | - |
|
| 26 | - private function resetDB() { |
|
| 27 | - $qb = $this->db->getQueryBuilder(); |
|
| 28 | - $qb->delete($this->mapper->getTableName()) |
|
| 29 | - ->where($qb->expr()->eq('user_id', $qb->createNamedParameter($this->testUID))); |
|
| 30 | - $qb->execute(); |
|
| 31 | - } |
|
| 32 | - |
|
| 33 | - protected function setUp(): void { |
|
| 34 | - parent::setUp(); |
|
| 35 | - |
|
| 36 | - $this->db = Server::get(IDBConnection::class); |
|
| 37 | - $this->mapper = Server::get(BackupCodeMapper::class); |
|
| 38 | - |
|
| 39 | - $this->resetDB(); |
|
| 40 | - } |
|
| 41 | - |
|
| 42 | - protected function tearDown(): void { |
|
| 43 | - parent::tearDown(); |
|
| 44 | - |
|
| 45 | - $this->resetDB(); |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - public function testGetBackupCodes(): void { |
|
| 49 | - $code1 = new BackupCode(); |
|
| 50 | - $code1->setUserId($this->testUID); |
|
| 51 | - $code1->setCode('1|$2y$10$Fyo.DkMtkaHapVvRVbQBeeIdi5x/6nmPnxiBzD0GDKa08NMus5xze'); |
|
| 52 | - $code1->setUsed(1); |
|
| 53 | - |
|
| 54 | - $code2 = new BackupCode(); |
|
| 55 | - $code2->setUserId($this->testUID); |
|
| 56 | - $code2->setCode('1|$2y$10$nj3sZaCqGN8t6.SsnNADt.eX34UCkdX6FPx.r.rIwE6Jj3vi5wyt2'); |
|
| 57 | - $code2->setUsed(0); |
|
| 58 | - |
|
| 59 | - $this->mapper->insert($code1); |
|
| 60 | - $this->mapper->insert($code2); |
|
| 61 | - |
|
| 62 | - $user = $this->getMockBuilder(IUser::class)->getMock(); |
|
| 63 | - $user->expects($this->once()) |
|
| 64 | - ->method('getUID') |
|
| 65 | - ->willReturn($this->testUID); |
|
| 66 | - |
|
| 67 | - $dbCodes = $this->mapper->getBackupCodes($user); |
|
| 68 | - |
|
| 69 | - $this->assertCount(2, $dbCodes); |
|
| 70 | - $this->assertInstanceOf(BackupCode::class, $dbCodes[0]); |
|
| 71 | - $this->assertInstanceOf(BackupCode::class, $dbCodes[1]); |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - public function testDeleteCodes(): void { |
|
| 75 | - $code = new BackupCode(); |
|
| 76 | - $code->setUserId($this->testUID); |
|
| 77 | - $code->setCode('1|$2y$10$CagG8pEhZL.xDirtCCP/KuuWtnsAasgq60zY9rU46dBK4w8yW0Z/y'); |
|
| 78 | - $code->setUsed(1); |
|
| 79 | - $user = $this->getMockBuilder(IUser::class)->getMock(); |
|
| 80 | - $user->expects($this->any()) |
|
| 81 | - ->method('getUID') |
|
| 82 | - ->willReturn($this->testUID); |
|
| 83 | - |
|
| 84 | - $this->mapper->insert($code); |
|
| 85 | - |
|
| 86 | - $this->assertCount(1, $this->mapper->getBackupCodes($user)); |
|
| 87 | - |
|
| 88 | - $this->mapper->deleteCodes($user); |
|
| 89 | - |
|
| 90 | - $this->assertCount(0, $this->mapper->getBackupCodes($user)); |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - public function testInsertArgonEncryptedCodes(): void { |
|
| 94 | - $code = new BackupCode(); |
|
| 95 | - $code->setUserId($this->testUID); |
|
| 96 | - $code->setCode('2|$argon2i$v=19$m=1024,t=2,p=2$MjJWUjRFWndtMm5BWGxOag$BusVxLeFyiLLWtaVvX/JRFBiPdZcjRrzpQ/rAhn6vqY'); |
|
| 97 | - $code->setUsed(1); |
|
| 98 | - $user = $this->getMockBuilder(IUser::class)->getMock(); |
|
| 99 | - $user->expects($this->any()) |
|
| 100 | - ->method('getUID') |
|
| 101 | - ->willReturn($this->testUID); |
|
| 102 | - |
|
| 103 | - $this->mapper->insert($code); |
|
| 104 | - $this->assertCount(1, $this->mapper->getBackupCodes($user)); |
|
| 105 | - } |
|
| 22 | + private IDBConnection $db; |
|
| 23 | + private BackupCodeMapper $mapper; |
|
| 24 | + private string $testUID = 'test123456'; |
|
| 25 | + |
|
| 26 | + private function resetDB() { |
|
| 27 | + $qb = $this->db->getQueryBuilder(); |
|
| 28 | + $qb->delete($this->mapper->getTableName()) |
|
| 29 | + ->where($qb->expr()->eq('user_id', $qb->createNamedParameter($this->testUID))); |
|
| 30 | + $qb->execute(); |
|
| 31 | + } |
|
| 32 | + |
|
| 33 | + protected function setUp(): void { |
|
| 34 | + parent::setUp(); |
|
| 35 | + |
|
| 36 | + $this->db = Server::get(IDBConnection::class); |
|
| 37 | + $this->mapper = Server::get(BackupCodeMapper::class); |
|
| 38 | + |
|
| 39 | + $this->resetDB(); |
|
| 40 | + } |
|
| 41 | + |
|
| 42 | + protected function tearDown(): void { |
|
| 43 | + parent::tearDown(); |
|
| 44 | + |
|
| 45 | + $this->resetDB(); |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + public function testGetBackupCodes(): void { |
|
| 49 | + $code1 = new BackupCode(); |
|
| 50 | + $code1->setUserId($this->testUID); |
|
| 51 | + $code1->setCode('1|$2y$10$Fyo.DkMtkaHapVvRVbQBeeIdi5x/6nmPnxiBzD0GDKa08NMus5xze'); |
|
| 52 | + $code1->setUsed(1); |
|
| 53 | + |
|
| 54 | + $code2 = new BackupCode(); |
|
| 55 | + $code2->setUserId($this->testUID); |
|
| 56 | + $code2->setCode('1|$2y$10$nj3sZaCqGN8t6.SsnNADt.eX34UCkdX6FPx.r.rIwE6Jj3vi5wyt2'); |
|
| 57 | + $code2->setUsed(0); |
|
| 58 | + |
|
| 59 | + $this->mapper->insert($code1); |
|
| 60 | + $this->mapper->insert($code2); |
|
| 61 | + |
|
| 62 | + $user = $this->getMockBuilder(IUser::class)->getMock(); |
|
| 63 | + $user->expects($this->once()) |
|
| 64 | + ->method('getUID') |
|
| 65 | + ->willReturn($this->testUID); |
|
| 66 | + |
|
| 67 | + $dbCodes = $this->mapper->getBackupCodes($user); |
|
| 68 | + |
|
| 69 | + $this->assertCount(2, $dbCodes); |
|
| 70 | + $this->assertInstanceOf(BackupCode::class, $dbCodes[0]); |
|
| 71 | + $this->assertInstanceOf(BackupCode::class, $dbCodes[1]); |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + public function testDeleteCodes(): void { |
|
| 75 | + $code = new BackupCode(); |
|
| 76 | + $code->setUserId($this->testUID); |
|
| 77 | + $code->setCode('1|$2y$10$CagG8pEhZL.xDirtCCP/KuuWtnsAasgq60zY9rU46dBK4w8yW0Z/y'); |
|
| 78 | + $code->setUsed(1); |
|
| 79 | + $user = $this->getMockBuilder(IUser::class)->getMock(); |
|
| 80 | + $user->expects($this->any()) |
|
| 81 | + ->method('getUID') |
|
| 82 | + ->willReturn($this->testUID); |
|
| 83 | + |
|
| 84 | + $this->mapper->insert($code); |
|
| 85 | + |
|
| 86 | + $this->assertCount(1, $this->mapper->getBackupCodes($user)); |
|
| 87 | + |
|
| 88 | + $this->mapper->deleteCodes($user); |
|
| 89 | + |
|
| 90 | + $this->assertCount(0, $this->mapper->getBackupCodes($user)); |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + public function testInsertArgonEncryptedCodes(): void { |
|
| 94 | + $code = new BackupCode(); |
|
| 95 | + $code->setUserId($this->testUID); |
|
| 96 | + $code->setCode('2|$argon2i$v=19$m=1024,t=2,p=2$MjJWUjRFWndtMm5BWGxOag$BusVxLeFyiLLWtaVvX/JRFBiPdZcjRrzpQ/rAhn6vqY'); |
|
| 97 | + $code->setUsed(1); |
|
| 98 | + $user = $this->getMockBuilder(IUser::class)->getMock(); |
|
| 99 | + $user->expects($this->any()) |
|
| 100 | + ->method('getUID') |
|
| 101 | + ->willReturn($this->testUID); |
|
| 102 | + |
|
| 103 | + $this->mapper->insert($code); |
|
| 104 | + $this->assertCount(1, $this->mapper->getBackupCodes($user)); |
|
| 105 | + } |
|
| 106 | 106 | } |
@@ -20,71 +20,71 @@ |
||
| 20 | 20 | * @group DB |
| 21 | 21 | */ |
| 22 | 22 | class BackupCodeStorageTest extends TestCase { |
| 23 | - private IManager&MockObject $notificationManager; |
|
| 24 | - private string $testUID = 'test123456789'; |
|
| 25 | - private BackupCodeStorage $storage; |
|
| 23 | + private IManager&MockObject $notificationManager; |
|
| 24 | + private string $testUID = 'test123456789'; |
|
| 25 | + private BackupCodeStorage $storage; |
|
| 26 | 26 | |
| 27 | - protected function setUp(): void { |
|
| 28 | - parent::setUp(); |
|
| 27 | + protected function setUp(): void { |
|
| 28 | + parent::setUp(); |
|
| 29 | 29 | |
| 30 | - $this->storage = Server::get(BackupCodeStorage::class); |
|
| 30 | + $this->storage = Server::get(BackupCodeStorage::class); |
|
| 31 | 31 | |
| 32 | - $this->notificationManager = $this->createMock(IManager::class); |
|
| 33 | - $this->notificationManager->method('createNotification') |
|
| 34 | - ->willReturn(Server::get(IManager::class)->createNotification()); |
|
| 35 | - $this->overwriteService(IManager::class, $this->notificationManager); |
|
| 36 | - } |
|
| 32 | + $this->notificationManager = $this->createMock(IManager::class); |
|
| 33 | + $this->notificationManager->method('createNotification') |
|
| 34 | + ->willReturn(Server::get(IManager::class)->createNotification()); |
|
| 35 | + $this->overwriteService(IManager::class, $this->notificationManager); |
|
| 36 | + } |
|
| 37 | 37 | |
| 38 | - public function testSimpleWorkFlow(): void { |
|
| 39 | - $user = $this->getMockBuilder(IUser::class)->getMock(); |
|
| 40 | - $user->expects($this->any()) |
|
| 41 | - ->method('getUID') |
|
| 42 | - ->willReturn($this->testUID); |
|
| 38 | + public function testSimpleWorkFlow(): void { |
|
| 39 | + $user = $this->getMockBuilder(IUser::class)->getMock(); |
|
| 40 | + $user->expects($this->any()) |
|
| 41 | + ->method('getUID') |
|
| 42 | + ->willReturn($this->testUID); |
|
| 43 | 43 | |
| 44 | - $this->notificationManager->expects($this->once()) |
|
| 45 | - ->method('markProcessed') |
|
| 46 | - ->with($this->callback(function (INotification $notification) { |
|
| 47 | - return $notification->getUser() === $this->testUID && |
|
| 48 | - $notification->getObjectType() === 'create' && |
|
| 49 | - $notification->getObjectId() === 'codes' && |
|
| 50 | - $notification->getApp() === 'twofactor_backupcodes'; |
|
| 51 | - })); |
|
| 44 | + $this->notificationManager->expects($this->once()) |
|
| 45 | + ->method('markProcessed') |
|
| 46 | + ->with($this->callback(function (INotification $notification) { |
|
| 47 | + return $notification->getUser() === $this->testUID && |
|
| 48 | + $notification->getObjectType() === 'create' && |
|
| 49 | + $notification->getObjectId() === 'codes' && |
|
| 50 | + $notification->getApp() === 'twofactor_backupcodes'; |
|
| 51 | + })); |
|
| 52 | 52 | |
| 53 | - // Create codes |
|
| 54 | - $codes = $this->storage->createCodes($user, 5); |
|
| 55 | - $this->assertCount(5, $codes); |
|
| 56 | - $this->assertTrue($this->storage->hasBackupCodes($user)); |
|
| 57 | - $initialState = [ |
|
| 58 | - 'enabled' => true, |
|
| 59 | - 'total' => 5, |
|
| 60 | - 'used' => 0, |
|
| 61 | - ]; |
|
| 62 | - $this->assertEquals($initialState, $this->storage->getBackupCodesState($user)); |
|
| 53 | + // Create codes |
|
| 54 | + $codes = $this->storage->createCodes($user, 5); |
|
| 55 | + $this->assertCount(5, $codes); |
|
| 56 | + $this->assertTrue($this->storage->hasBackupCodes($user)); |
|
| 57 | + $initialState = [ |
|
| 58 | + 'enabled' => true, |
|
| 59 | + 'total' => 5, |
|
| 60 | + 'used' => 0, |
|
| 61 | + ]; |
|
| 62 | + $this->assertEquals($initialState, $this->storage->getBackupCodesState($user)); |
|
| 63 | 63 | |
| 64 | - // Use codes |
|
| 65 | - $code = $codes[2]; |
|
| 66 | - $this->assertTrue($this->storage->validateCode($user, $code)); |
|
| 67 | - // Code must not be used twice |
|
| 68 | - $this->assertFalse($this->storage->validateCode($user, $code)); |
|
| 69 | - // Invalid codes are invalid |
|
| 70 | - $this->assertFalse($this->storage->validateCode($user, 'I DO NOT EXIST')); |
|
| 71 | - $stateAfter = [ |
|
| 72 | - 'enabled' => true, |
|
| 73 | - 'total' => 5, |
|
| 74 | - 'used' => 1, |
|
| 75 | - ]; |
|
| 76 | - $this->assertEquals($stateAfter, $this->storage->getBackupCodesState($user)); |
|
| 64 | + // Use codes |
|
| 65 | + $code = $codes[2]; |
|
| 66 | + $this->assertTrue($this->storage->validateCode($user, $code)); |
|
| 67 | + // Code must not be used twice |
|
| 68 | + $this->assertFalse($this->storage->validateCode($user, $code)); |
|
| 69 | + // Invalid codes are invalid |
|
| 70 | + $this->assertFalse($this->storage->validateCode($user, 'I DO NOT EXIST')); |
|
| 71 | + $stateAfter = [ |
|
| 72 | + 'enabled' => true, |
|
| 73 | + 'total' => 5, |
|
| 74 | + 'used' => 1, |
|
| 75 | + ]; |
|
| 76 | + $this->assertEquals($stateAfter, $this->storage->getBackupCodesState($user)); |
|
| 77 | 77 | |
| 78 | - // Deplete codes |
|
| 79 | - $this->assertTrue($this->storage->validateCode($user, $codes[0])); |
|
| 80 | - $this->assertTrue($this->storage->validateCode($user, $codes[1])); |
|
| 81 | - $this->assertTrue($this->storage->validateCode($user, $codes[3])); |
|
| 82 | - $this->assertTrue($this->storage->validateCode($user, $codes[4])); |
|
| 83 | - $stateAllUsed = [ |
|
| 84 | - 'enabled' => true, |
|
| 85 | - 'total' => 5, |
|
| 86 | - 'used' => 5, |
|
| 87 | - ]; |
|
| 88 | - $this->assertEquals($stateAllUsed, $this->storage->getBackupCodesState($user)); |
|
| 89 | - } |
|
| 78 | + // Deplete codes |
|
| 79 | + $this->assertTrue($this->storage->validateCode($user, $codes[0])); |
|
| 80 | + $this->assertTrue($this->storage->validateCode($user, $codes[1])); |
|
| 81 | + $this->assertTrue($this->storage->validateCode($user, $codes[3])); |
|
| 82 | + $this->assertTrue($this->storage->validateCode($user, $codes[4])); |
|
| 83 | + $stateAllUsed = [ |
|
| 84 | + 'enabled' => true, |
|
| 85 | + 'total' => 5, |
|
| 86 | + 'used' => 5, |
|
| 87 | + ]; |
|
| 88 | + $this->assertEquals($stateAllUsed, $this->storage->getBackupCodesState($user)); |
|
| 89 | + } |
|
| 90 | 90 | } |