@@ -32,417 +32,417 @@ |
||
| 32 | 32 | */ |
| 33 | 33 | class LoginFlowV2ServiceUnitTest extends TestCase { |
| 34 | 34 | |
| 35 | - private LoginFlowV2Service $subjectUnderTest; |
|
| 36 | - |
|
| 37 | - private IConfig&MockObject $config; |
|
| 38 | - private ICrypto&MockObject $crypto; |
|
| 39 | - private LoggerInterface&MockObject $logger; |
|
| 40 | - private LoginFlowV2Mapper&MockObject $mapper; |
|
| 41 | - private ISecureRandom&MockObject $secureRandom; |
|
| 42 | - private ITimeFactory&MockObject $timeFactory; |
|
| 43 | - private IProvider&MockObject $tokenProvider; |
|
| 44 | - |
|
| 45 | - public function setUp(): void { |
|
| 46 | - parent::setUp(); |
|
| 47 | - |
|
| 48 | - $this->setupSubjectUnderTest(); |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * Setup subject under test with mocked constructor arguments. |
|
| 53 | - * |
|
| 54 | - * Code was moved to separate function to keep setUp function small and clear. |
|
| 55 | - */ |
|
| 56 | - private function setupSubjectUnderTest(): void { |
|
| 57 | - $this->config = $this->createMock(IConfig::class); |
|
| 58 | - $this->crypto = $this->createMock(ICrypto::class); |
|
| 59 | - $this->mapper = $this->createMock(LoginFlowV2Mapper::class); |
|
| 60 | - $this->logger = $this->createMock(LoggerInterface::class); |
|
| 61 | - $this->tokenProvider = $this->createMock(IProvider::class); |
|
| 62 | - $this->secureRandom = $this->createMock(ISecureRandom::class); |
|
| 63 | - $this->timeFactory = $this->createMock(ITimeFactory::class); |
|
| 64 | - |
|
| 65 | - $this->subjectUnderTest = new LoginFlowV2Service( |
|
| 66 | - $this->mapper, |
|
| 67 | - $this->secureRandom, |
|
| 68 | - $this->timeFactory, |
|
| 69 | - $this->config, |
|
| 70 | - $this->crypto, |
|
| 71 | - $this->logger, |
|
| 72 | - $this->tokenProvider |
|
| 73 | - ); |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * Generates for a given password required OpenSSL parts. |
|
| 78 | - * |
|
| 79 | - * @return array Array contains encrypted password, private key and public key. |
|
| 80 | - */ |
|
| 81 | - private function getOpenSSLEncryptedPublicAndPrivateKey(string $appPassword): array { |
|
| 82 | - // Create the private and public key |
|
| 83 | - $res = openssl_pkey_new([ |
|
| 84 | - 'digest_alg' => 'md5', // take fast algorithm for testing purposes |
|
| 85 | - 'private_key_bits' => 512, |
|
| 86 | - 'private_key_type' => OPENSSL_KEYTYPE_RSA, |
|
| 87 | - ]); |
|
| 88 | - |
|
| 89 | - // Extract the private key from $res |
|
| 90 | - openssl_pkey_export($res, $privateKey); |
|
| 91 | - |
|
| 92 | - // Extract the public key from $res |
|
| 93 | - $publicKey = openssl_pkey_get_details($res); |
|
| 94 | - $publicKey = $publicKey['key']; |
|
| 95 | - |
|
| 96 | - // Encrypt the data to $encrypted using the public key |
|
| 97 | - openssl_public_encrypt($appPassword, $encrypted, $publicKey, OPENSSL_PKCS1_OAEP_PADDING); |
|
| 98 | - |
|
| 99 | - return [$encrypted, $privateKey, $publicKey]; |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - /* |
|
| 35 | + private LoginFlowV2Service $subjectUnderTest; |
|
| 36 | + |
|
| 37 | + private IConfig&MockObject $config; |
|
| 38 | + private ICrypto&MockObject $crypto; |
|
| 39 | + private LoggerInterface&MockObject $logger; |
|
| 40 | + private LoginFlowV2Mapper&MockObject $mapper; |
|
| 41 | + private ISecureRandom&MockObject $secureRandom; |
|
| 42 | + private ITimeFactory&MockObject $timeFactory; |
|
| 43 | + private IProvider&MockObject $tokenProvider; |
|
| 44 | + |
|
| 45 | + public function setUp(): void { |
|
| 46 | + parent::setUp(); |
|
| 47 | + |
|
| 48 | + $this->setupSubjectUnderTest(); |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * Setup subject under test with mocked constructor arguments. |
|
| 53 | + * |
|
| 54 | + * Code was moved to separate function to keep setUp function small and clear. |
|
| 55 | + */ |
|
| 56 | + private function setupSubjectUnderTest(): void { |
|
| 57 | + $this->config = $this->createMock(IConfig::class); |
|
| 58 | + $this->crypto = $this->createMock(ICrypto::class); |
|
| 59 | + $this->mapper = $this->createMock(LoginFlowV2Mapper::class); |
|
| 60 | + $this->logger = $this->createMock(LoggerInterface::class); |
|
| 61 | + $this->tokenProvider = $this->createMock(IProvider::class); |
|
| 62 | + $this->secureRandom = $this->createMock(ISecureRandom::class); |
|
| 63 | + $this->timeFactory = $this->createMock(ITimeFactory::class); |
|
| 64 | + |
|
| 65 | + $this->subjectUnderTest = new LoginFlowV2Service( |
|
| 66 | + $this->mapper, |
|
| 67 | + $this->secureRandom, |
|
| 68 | + $this->timeFactory, |
|
| 69 | + $this->config, |
|
| 70 | + $this->crypto, |
|
| 71 | + $this->logger, |
|
| 72 | + $this->tokenProvider |
|
| 73 | + ); |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * Generates for a given password required OpenSSL parts. |
|
| 78 | + * |
|
| 79 | + * @return array Array contains encrypted password, private key and public key. |
|
| 80 | + */ |
|
| 81 | + private function getOpenSSLEncryptedPublicAndPrivateKey(string $appPassword): array { |
|
| 82 | + // Create the private and public key |
|
| 83 | + $res = openssl_pkey_new([ |
|
| 84 | + 'digest_alg' => 'md5', // take fast algorithm for testing purposes |
|
| 85 | + 'private_key_bits' => 512, |
|
| 86 | + 'private_key_type' => OPENSSL_KEYTYPE_RSA, |
|
| 87 | + ]); |
|
| 88 | + |
|
| 89 | + // Extract the private key from $res |
|
| 90 | + openssl_pkey_export($res, $privateKey); |
|
| 91 | + |
|
| 92 | + // Extract the public key from $res |
|
| 93 | + $publicKey = openssl_pkey_get_details($res); |
|
| 94 | + $publicKey = $publicKey['key']; |
|
| 95 | + |
|
| 96 | + // Encrypt the data to $encrypted using the public key |
|
| 97 | + openssl_public_encrypt($appPassword, $encrypted, $publicKey, OPENSSL_PKCS1_OAEP_PADDING); |
|
| 98 | + |
|
| 99 | + return [$encrypted, $privateKey, $publicKey]; |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + /* |
|
| 103 | 103 | * Tests for poll |
| 104 | 104 | */ |
| 105 | - public function testPollPrivateKeyCouldNotBeDecrypted(): void { |
|
| 106 | - $this->expectException(LoginFlowV2NotFoundException::class); |
|
| 107 | - $this->expectExceptionMessage('Apptoken could not be decrypted'); |
|
| 105 | + public function testPollPrivateKeyCouldNotBeDecrypted(): void { |
|
| 106 | + $this->expectException(LoginFlowV2NotFoundException::class); |
|
| 107 | + $this->expectExceptionMessage('Apptoken could not be decrypted'); |
|
| 108 | 108 | |
| 109 | - $this->crypto->expects($this->once()) |
|
| 110 | - ->method('decrypt') |
|
| 111 | - ->willThrowException(new \Exception('HMAC mismatch')); |
|
| 109 | + $this->crypto->expects($this->once()) |
|
| 110 | + ->method('decrypt') |
|
| 111 | + ->willThrowException(new \Exception('HMAC mismatch')); |
|
| 112 | 112 | |
| 113 | - /* |
|
| 113 | + /* |
|
| 114 | 114 | * Cannot be mocked, because functions like getLoginName are magic functions. |
| 115 | 115 | * To be able to set internal properties, we have to use the real class here. |
| 116 | 116 | */ |
| 117 | - $loginFlowV2 = new LoginFlowV2(); |
|
| 118 | - $loginFlowV2->setLoginName('test'); |
|
| 119 | - $loginFlowV2->setServer('test'); |
|
| 120 | - $loginFlowV2->setAppPassword('test'); |
|
| 121 | - $loginFlowV2->setPrivateKey('test'); |
|
| 117 | + $loginFlowV2 = new LoginFlowV2(); |
|
| 118 | + $loginFlowV2->setLoginName('test'); |
|
| 119 | + $loginFlowV2->setServer('test'); |
|
| 120 | + $loginFlowV2->setAppPassword('test'); |
|
| 121 | + $loginFlowV2->setPrivateKey('test'); |
|
| 122 | 122 | |
| 123 | - $this->mapper->expects($this->once()) |
|
| 124 | - ->method('getByPollToken') |
|
| 125 | - ->willReturn($loginFlowV2); |
|
| 123 | + $this->mapper->expects($this->once()) |
|
| 124 | + ->method('getByPollToken') |
|
| 125 | + ->willReturn($loginFlowV2); |
|
| 126 | 126 | |
| 127 | - $this->subjectUnderTest->poll(''); |
|
| 128 | - } |
|
| 127 | + $this->subjectUnderTest->poll(''); |
|
| 128 | + } |
|
| 129 | 129 | |
| 130 | - public function testPollApptokenCouldNotBeDecrypted(): void { |
|
| 131 | - $this->expectException(LoginFlowV2NotFoundException::class); |
|
| 132 | - $this->expectExceptionMessage('Apptoken could not be decrypted'); |
|
| 130 | + public function testPollApptokenCouldNotBeDecrypted(): void { |
|
| 131 | + $this->expectException(LoginFlowV2NotFoundException::class); |
|
| 132 | + $this->expectExceptionMessage('Apptoken could not be decrypted'); |
|
| 133 | 133 | |
| 134 | - /* |
|
| 134 | + /* |
|
| 135 | 135 | * Cannot be mocked, because functions like getLoginName are magic functions. |
| 136 | 136 | * To be able to set internal properties, we have to use the real class here. |
| 137 | 137 | */ |
| 138 | - [$encrypted, $privateKey,] = $this->getOpenSSLEncryptedPublicAndPrivateKey('test'); |
|
| 139 | - $loginFlowV2 = new LoginFlowV2(); |
|
| 140 | - $loginFlowV2->setLoginName('test'); |
|
| 141 | - $loginFlowV2->setServer('test'); |
|
| 142 | - $loginFlowV2->setAppPassword('broken#' . $encrypted); |
|
| 143 | - $loginFlowV2->setPrivateKey('encrypted(test)'); |
|
| 138 | + [$encrypted, $privateKey,] = $this->getOpenSSLEncryptedPublicAndPrivateKey('test'); |
|
| 139 | + $loginFlowV2 = new LoginFlowV2(); |
|
| 140 | + $loginFlowV2->setLoginName('test'); |
|
| 141 | + $loginFlowV2->setServer('test'); |
|
| 142 | + $loginFlowV2->setAppPassword('broken#' . $encrypted); |
|
| 143 | + $loginFlowV2->setPrivateKey('encrypted(test)'); |
|
| 144 | 144 | |
| 145 | - $this->crypto->expects($this->once()) |
|
| 146 | - ->method('decrypt') |
|
| 147 | - ->willReturn($privateKey); |
|
| 145 | + $this->crypto->expects($this->once()) |
|
| 146 | + ->method('decrypt') |
|
| 147 | + ->willReturn($privateKey); |
|
| 148 | 148 | |
| 149 | - $this->mapper->expects($this->once()) |
|
| 150 | - ->method('getByPollToken') |
|
| 151 | - ->willReturn($loginFlowV2); |
|
| 149 | + $this->mapper->expects($this->once()) |
|
| 150 | + ->method('getByPollToken') |
|
| 151 | + ->willReturn($loginFlowV2); |
|
| 152 | 152 | |
| 153 | - $this->subjectUnderTest->poll('test'); |
|
| 154 | - } |
|
| 153 | + $this->subjectUnderTest->poll('test'); |
|
| 154 | + } |
|
| 155 | 155 | |
| 156 | - public function testPollInvalidToken(): void { |
|
| 157 | - $this->expectException(LoginFlowV2NotFoundException::class); |
|
| 158 | - $this->expectExceptionMessage('Invalid token'); |
|
| 156 | + public function testPollInvalidToken(): void { |
|
| 157 | + $this->expectException(LoginFlowV2NotFoundException::class); |
|
| 158 | + $this->expectExceptionMessage('Invalid token'); |
|
| 159 | 159 | |
| 160 | - $this->mapper->expects($this->once()) |
|
| 161 | - ->method('getByPollToken') |
|
| 162 | - ->willThrowException(new DoesNotExistException('')); |
|
| 160 | + $this->mapper->expects($this->once()) |
|
| 161 | + ->method('getByPollToken') |
|
| 162 | + ->willThrowException(new DoesNotExistException('')); |
|
| 163 | 163 | |
| 164 | - $this->subjectUnderTest->poll(''); |
|
| 165 | - } |
|
| 164 | + $this->subjectUnderTest->poll(''); |
|
| 165 | + } |
|
| 166 | 166 | |
| 167 | - public function testPollTokenNotYetReady(): void { |
|
| 168 | - $this->expectException(LoginFlowV2NotFoundException::class); |
|
| 169 | - $this->expectExceptionMessage('Token not yet ready'); |
|
| 167 | + public function testPollTokenNotYetReady(): void { |
|
| 168 | + $this->expectException(LoginFlowV2NotFoundException::class); |
|
| 169 | + $this->expectExceptionMessage('Token not yet ready'); |
|
| 170 | 170 | |
| 171 | - $this->subjectUnderTest->poll(''); |
|
| 172 | - } |
|
| 171 | + $this->subjectUnderTest->poll(''); |
|
| 172 | + } |
|
| 173 | 173 | |
| 174 | - public function testPollRemoveDataFromDb(): void { |
|
| 175 | - [$encrypted, $privateKey] = $this->getOpenSSLEncryptedPublicAndPrivateKey('test_pass'); |
|
| 174 | + public function testPollRemoveDataFromDb(): void { |
|
| 175 | + [$encrypted, $privateKey] = $this->getOpenSSLEncryptedPublicAndPrivateKey('test_pass'); |
|
| 176 | 176 | |
| 177 | - $this->crypto->expects($this->once()) |
|
| 178 | - ->method('decrypt') |
|
| 179 | - ->willReturn($privateKey); |
|
| 177 | + $this->crypto->expects($this->once()) |
|
| 178 | + ->method('decrypt') |
|
| 179 | + ->willReturn($privateKey); |
|
| 180 | 180 | |
| 181 | - /* |
|
| 181 | + /* |
|
| 182 | 182 | * Cannot be mocked, because functions like getLoginName are magic functions. |
| 183 | 183 | * To be able to set internal properties, we have to use the real class here. |
| 184 | 184 | */ |
| 185 | - $loginFlowV2 = new LoginFlowV2(); |
|
| 186 | - $loginFlowV2->setLoginName('test_login'); |
|
| 187 | - $loginFlowV2->setServer('test_server'); |
|
| 188 | - $loginFlowV2->setAppPassword(base64_encode($encrypted)); |
|
| 189 | - $loginFlowV2->setPrivateKey($privateKey); |
|
| 190 | - |
|
| 191 | - $this->mapper->expects($this->once()) |
|
| 192 | - ->method('delete') |
|
| 193 | - ->with($this->equalTo($loginFlowV2)); |
|
| 194 | - |
|
| 195 | - $this->mapper->expects($this->once()) |
|
| 196 | - ->method('getByPollToken') |
|
| 197 | - ->willReturn($loginFlowV2); |
|
| 198 | - |
|
| 199 | - $credentials = $this->subjectUnderTest->poll(''); |
|
| 200 | - |
|
| 201 | - $this->assertTrue($credentials instanceof LoginFlowV2Credentials); |
|
| 202 | - $this->assertEquals( |
|
| 203 | - [ |
|
| 204 | - 'server' => 'test_server', |
|
| 205 | - 'loginName' => 'test_login', |
|
| 206 | - 'appPassword' => 'test_pass', |
|
| 207 | - ], |
|
| 208 | - $credentials->jsonSerialize() |
|
| 209 | - ); |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - /* |
|
| 185 | + $loginFlowV2 = new LoginFlowV2(); |
|
| 186 | + $loginFlowV2->setLoginName('test_login'); |
|
| 187 | + $loginFlowV2->setServer('test_server'); |
|
| 188 | + $loginFlowV2->setAppPassword(base64_encode($encrypted)); |
|
| 189 | + $loginFlowV2->setPrivateKey($privateKey); |
|
| 190 | + |
|
| 191 | + $this->mapper->expects($this->once()) |
|
| 192 | + ->method('delete') |
|
| 193 | + ->with($this->equalTo($loginFlowV2)); |
|
| 194 | + |
|
| 195 | + $this->mapper->expects($this->once()) |
|
| 196 | + ->method('getByPollToken') |
|
| 197 | + ->willReturn($loginFlowV2); |
|
| 198 | + |
|
| 199 | + $credentials = $this->subjectUnderTest->poll(''); |
|
| 200 | + |
|
| 201 | + $this->assertTrue($credentials instanceof LoginFlowV2Credentials); |
|
| 202 | + $this->assertEquals( |
|
| 203 | + [ |
|
| 204 | + 'server' => 'test_server', |
|
| 205 | + 'loginName' => 'test_login', |
|
| 206 | + 'appPassword' => 'test_pass', |
|
| 207 | + ], |
|
| 208 | + $credentials->jsonSerialize() |
|
| 209 | + ); |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + /* |
|
| 213 | 213 | * Tests for getByLoginToken |
| 214 | 214 | */ |
| 215 | 215 | |
| 216 | - public function testGetByLoginToken(): void { |
|
| 217 | - $loginFlowV2 = new LoginFlowV2(); |
|
| 218 | - $loginFlowV2->setLoginName('test_login'); |
|
| 219 | - $loginFlowV2->setServer('test_server'); |
|
| 220 | - $loginFlowV2->setAppPassword('test'); |
|
| 216 | + public function testGetByLoginToken(): void { |
|
| 217 | + $loginFlowV2 = new LoginFlowV2(); |
|
| 218 | + $loginFlowV2->setLoginName('test_login'); |
|
| 219 | + $loginFlowV2->setServer('test_server'); |
|
| 220 | + $loginFlowV2->setAppPassword('test'); |
|
| 221 | 221 | |
| 222 | - $this->mapper->expects($this->once()) |
|
| 223 | - ->method('getByLoginToken') |
|
| 224 | - ->willReturn($loginFlowV2); |
|
| 222 | + $this->mapper->expects($this->once()) |
|
| 223 | + ->method('getByLoginToken') |
|
| 224 | + ->willReturn($loginFlowV2); |
|
| 225 | 225 | |
| 226 | - $result = $this->subjectUnderTest->getByLoginToken('test_token'); |
|
| 226 | + $result = $this->subjectUnderTest->getByLoginToken('test_token'); |
|
| 227 | 227 | |
| 228 | - $this->assertTrue($result instanceof LoginFlowV2); |
|
| 229 | - $this->assertEquals('test_server', $result->getServer()); |
|
| 230 | - $this->assertEquals('test_login', $result->getLoginName()); |
|
| 231 | - $this->assertEquals('test', $result->getAppPassword()); |
|
| 232 | - } |
|
| 228 | + $this->assertTrue($result instanceof LoginFlowV2); |
|
| 229 | + $this->assertEquals('test_server', $result->getServer()); |
|
| 230 | + $this->assertEquals('test_login', $result->getLoginName()); |
|
| 231 | + $this->assertEquals('test', $result->getAppPassword()); |
|
| 232 | + } |
|
| 233 | 233 | |
| 234 | - public function testGetByLoginTokenLoginTokenInvalid(): void { |
|
| 235 | - $this->expectException(LoginFlowV2NotFoundException::class); |
|
| 236 | - $this->expectExceptionMessage('Login token invalid'); |
|
| 234 | + public function testGetByLoginTokenLoginTokenInvalid(): void { |
|
| 235 | + $this->expectException(LoginFlowV2NotFoundException::class); |
|
| 236 | + $this->expectExceptionMessage('Login token invalid'); |
|
| 237 | 237 | |
| 238 | - $this->mapper->expects($this->once()) |
|
| 239 | - ->method('getByLoginToken') |
|
| 240 | - ->willThrowException(new DoesNotExistException('')); |
|
| 238 | + $this->mapper->expects($this->once()) |
|
| 239 | + ->method('getByLoginToken') |
|
| 240 | + ->willThrowException(new DoesNotExistException('')); |
|
| 241 | 241 | |
| 242 | - $this->subjectUnderTest->getByLoginToken('test_token'); |
|
| 243 | - } |
|
| 242 | + $this->subjectUnderTest->getByLoginToken('test_token'); |
|
| 243 | + } |
|
| 244 | 244 | |
| 245 | - public function testGetByLoginTokenClientForbidden() { |
|
| 246 | - $this->expectException(LoginFlowV2ClientForbiddenException::class); |
|
| 247 | - $this->expectExceptionMessage('Client not allowed'); |
|
| 245 | + public function testGetByLoginTokenClientForbidden() { |
|
| 246 | + $this->expectException(LoginFlowV2ClientForbiddenException::class); |
|
| 247 | + $this->expectExceptionMessage('Client not allowed'); |
|
| 248 | 248 | |
| 249 | - $allowedClients = [ |
|
| 250 | - '/Custom Allowed Client/i' |
|
| 251 | - ]; |
|
| 249 | + $allowedClients = [ |
|
| 250 | + '/Custom Allowed Client/i' |
|
| 251 | + ]; |
|
| 252 | 252 | |
| 253 | - $this->config->expects($this->exactly(1)) |
|
| 254 | - ->method('getSystemValue') |
|
| 255 | - ->willReturnCallback(function ($key) use ($allowedClients) { |
|
| 256 | - // Note: \OCP\IConfig::getSystemValue returns either an array or string. |
|
| 257 | - return $key == 'core.login_flow_v2.allowed_user_agents' ? $allowedClients : ''; |
|
| 258 | - }); |
|
| 253 | + $this->config->expects($this->exactly(1)) |
|
| 254 | + ->method('getSystemValue') |
|
| 255 | + ->willReturnCallback(function ($key) use ($allowedClients) { |
|
| 256 | + // Note: \OCP\IConfig::getSystemValue returns either an array or string. |
|
| 257 | + return $key == 'core.login_flow_v2.allowed_user_agents' ? $allowedClients : ''; |
|
| 258 | + }); |
|
| 259 | 259 | |
| 260 | - $loginFlowV2 = new LoginFlowV2(); |
|
| 261 | - $loginFlowV2->setClientName('Rogue Curl Client/1.0'); |
|
| 260 | + $loginFlowV2 = new LoginFlowV2(); |
|
| 261 | + $loginFlowV2->setClientName('Rogue Curl Client/1.0'); |
|
| 262 | 262 | |
| 263 | - $this->mapper->expects($this->once()) |
|
| 264 | - ->method('getByLoginToken') |
|
| 265 | - ->willReturn($loginFlowV2); |
|
| 263 | + $this->mapper->expects($this->once()) |
|
| 264 | + ->method('getByLoginToken') |
|
| 265 | + ->willReturn($loginFlowV2); |
|
| 266 | 266 | |
| 267 | - $this->subjectUnderTest->getByLoginToken('test_token'); |
|
| 268 | - } |
|
| 267 | + $this->subjectUnderTest->getByLoginToken('test_token'); |
|
| 268 | + } |
|
| 269 | 269 | |
| 270 | - public function testGetByLoginTokenClientAllowed() { |
|
| 271 | - $allowedClients = [ |
|
| 272 | - '/Foo Allowed Client/i', |
|
| 273 | - '/Custom Allowed Client/i' |
|
| 274 | - ]; |
|
| 270 | + public function testGetByLoginTokenClientAllowed() { |
|
| 271 | + $allowedClients = [ |
|
| 272 | + '/Foo Allowed Client/i', |
|
| 273 | + '/Custom Allowed Client/i' |
|
| 274 | + ]; |
|
| 275 | 275 | |
| 276 | - $loginFlowV2 = new LoginFlowV2(); |
|
| 277 | - $loginFlowV2->setClientName('Custom Allowed Client Curl Client/1.0'); |
|
| 276 | + $loginFlowV2 = new LoginFlowV2(); |
|
| 277 | + $loginFlowV2->setClientName('Custom Allowed Client Curl Client/1.0'); |
|
| 278 | 278 | |
| 279 | - $this->config->expects($this->exactly(1)) |
|
| 280 | - ->method('getSystemValue') |
|
| 281 | - ->willReturnCallback(function ($key) use ($allowedClients) { |
|
| 282 | - // Note: \OCP\IConfig::getSystemValue returns either an array or string. |
|
| 283 | - return $key == 'core.login_flow_v2.allowed_user_agents' ? $allowedClients : ''; |
|
| 284 | - }); |
|
| 279 | + $this->config->expects($this->exactly(1)) |
|
| 280 | + ->method('getSystemValue') |
|
| 281 | + ->willReturnCallback(function ($key) use ($allowedClients) { |
|
| 282 | + // Note: \OCP\IConfig::getSystemValue returns either an array or string. |
|
| 283 | + return $key == 'core.login_flow_v2.allowed_user_agents' ? $allowedClients : ''; |
|
| 284 | + }); |
|
| 285 | 285 | |
| 286 | - $this->mapper->expects($this->once()) |
|
| 287 | - ->method('getByLoginToken') |
|
| 288 | - ->willReturn($loginFlowV2); |
|
| 286 | + $this->mapper->expects($this->once()) |
|
| 287 | + ->method('getByLoginToken') |
|
| 288 | + ->willReturn($loginFlowV2); |
|
| 289 | 289 | |
| 290 | - $result = $this->subjectUnderTest->getByLoginToken('test_token'); |
|
| 290 | + $result = $this->subjectUnderTest->getByLoginToken('test_token'); |
|
| 291 | 291 | |
| 292 | - $this->assertTrue($result instanceof LoginFlowV2); |
|
| 293 | - $this->assertEquals('Custom Allowed Client Curl Client/1.0', $result->getClientName()); |
|
| 294 | - } |
|
| 292 | + $this->assertTrue($result instanceof LoginFlowV2); |
|
| 293 | + $this->assertEquals('Custom Allowed Client Curl Client/1.0', $result->getClientName()); |
|
| 294 | + } |
|
| 295 | 295 | |
| 296 | - /* |
|
| 296 | + /* |
|
| 297 | 297 | * Tests for startLoginFlow |
| 298 | 298 | */ |
| 299 | 299 | |
| 300 | - public function testStartLoginFlow(): void { |
|
| 301 | - $loginFlowV2 = new LoginFlowV2(); |
|
| 300 | + public function testStartLoginFlow(): void { |
|
| 301 | + $loginFlowV2 = new LoginFlowV2(); |
|
| 302 | 302 | |
| 303 | - $this->mapper->expects($this->once()) |
|
| 304 | - ->method('getByLoginToken') |
|
| 305 | - ->willReturn($loginFlowV2); |
|
| 303 | + $this->mapper->expects($this->once()) |
|
| 304 | + ->method('getByLoginToken') |
|
| 305 | + ->willReturn($loginFlowV2); |
|
| 306 | 306 | |
| 307 | - $this->mapper->expects($this->once()) |
|
| 308 | - ->method('update'); |
|
| 307 | + $this->mapper->expects($this->once()) |
|
| 308 | + ->method('update'); |
|
| 309 | 309 | |
| 310 | - $this->assertTrue($this->subjectUnderTest->startLoginFlow('test_token')); |
|
| 311 | - } |
|
| 310 | + $this->assertTrue($this->subjectUnderTest->startLoginFlow('test_token')); |
|
| 311 | + } |
|
| 312 | 312 | |
| 313 | - public function testStartLoginFlowDoesNotExistException(): void { |
|
| 314 | - $this->mapper->expects($this->once()) |
|
| 315 | - ->method('getByLoginToken') |
|
| 316 | - ->willThrowException(new DoesNotExistException('')); |
|
| 313 | + public function testStartLoginFlowDoesNotExistException(): void { |
|
| 314 | + $this->mapper->expects($this->once()) |
|
| 315 | + ->method('getByLoginToken') |
|
| 316 | + ->willThrowException(new DoesNotExistException('')); |
|
| 317 | 317 | |
| 318 | - $this->assertFalse($this->subjectUnderTest->startLoginFlow('test_token')); |
|
| 319 | - } |
|
| 318 | + $this->assertFalse($this->subjectUnderTest->startLoginFlow('test_token')); |
|
| 319 | + } |
|
| 320 | 320 | |
| 321 | - /** |
|
| 322 | - * If an exception not of type DoesNotExistException is thrown, |
|
| 323 | - * it is expected that it is not being handled by startLoginFlow. |
|
| 324 | - */ |
|
| 325 | - public function testStartLoginFlowException(): void { |
|
| 326 | - $this->expectException(Exception::class); |
|
| 321 | + /** |
|
| 322 | + * If an exception not of type DoesNotExistException is thrown, |
|
| 323 | + * it is expected that it is not being handled by startLoginFlow. |
|
| 324 | + */ |
|
| 325 | + public function testStartLoginFlowException(): void { |
|
| 326 | + $this->expectException(Exception::class); |
|
| 327 | 327 | |
| 328 | - $this->mapper->expects($this->once()) |
|
| 329 | - ->method('getByLoginToken') |
|
| 330 | - ->willThrowException(new Exception('')); |
|
| 328 | + $this->mapper->expects($this->once()) |
|
| 329 | + ->method('getByLoginToken') |
|
| 330 | + ->willThrowException(new Exception('')); |
|
| 331 | 331 | |
| 332 | - $this->subjectUnderTest->startLoginFlow('test_token'); |
|
| 333 | - } |
|
| 332 | + $this->subjectUnderTest->startLoginFlow('test_token'); |
|
| 333 | + } |
|
| 334 | 334 | |
| 335 | - /* |
|
| 335 | + /* |
|
| 336 | 336 | * Tests for flowDone |
| 337 | 337 | */ |
| 338 | 338 | |
| 339 | - public function testFlowDone(): void { |
|
| 340 | - [,, $publicKey] = $this->getOpenSSLEncryptedPublicAndPrivateKey('test_pass'); |
|
| 341 | - |
|
| 342 | - $loginFlowV2 = new LoginFlowV2(); |
|
| 343 | - $loginFlowV2->setPublicKey($publicKey); |
|
| 344 | - $loginFlowV2->setClientName('client_name'); |
|
| 345 | - |
|
| 346 | - $this->mapper->expects($this->once()) |
|
| 347 | - ->method('getByLoginToken') |
|
| 348 | - ->willReturn($loginFlowV2); |
|
| 349 | - |
|
| 350 | - $this->mapper->expects($this->once()) |
|
| 351 | - ->method('update'); |
|
| 352 | - |
|
| 353 | - $this->secureRandom->expects($this->once()) |
|
| 354 | - ->method('generate') |
|
| 355 | - ->with(72, ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_DIGITS) |
|
| 356 | - ->willReturn('test_pass'); |
|
| 357 | - |
|
| 358 | - // session token |
|
| 359 | - $sessionToken = $this->getMockBuilder(IToken::class)->disableOriginalConstructor()->getMock(); |
|
| 360 | - $sessionToken->expects($this->once()) |
|
| 361 | - ->method('getLoginName') |
|
| 362 | - ->willReturn('login_name'); |
|
| 363 | - |
|
| 364 | - $this->tokenProvider->expects($this->once()) |
|
| 365 | - ->method('getPassword') |
|
| 366 | - ->willReturn('test_pass'); |
|
| 367 | - |
|
| 368 | - $this->tokenProvider->expects($this->once()) |
|
| 369 | - ->method('getToken') |
|
| 370 | - ->willReturn($sessionToken); |
|
| 371 | - |
|
| 372 | - $this->tokenProvider->expects($this->once()) |
|
| 373 | - ->method('generateToken') |
|
| 374 | - ->with( |
|
| 375 | - 'test_pass', |
|
| 376 | - 'user_id', |
|
| 377 | - 'login_name', |
|
| 378 | - 'test_pass', |
|
| 379 | - 'client_name', |
|
| 380 | - IToken::PERMANENT_TOKEN, |
|
| 381 | - IToken::DO_NOT_REMEMBER |
|
| 382 | - ); |
|
| 383 | - |
|
| 384 | - $result = $this->subjectUnderTest->flowDone( |
|
| 385 | - 'login_token', |
|
| 386 | - 'session_id', |
|
| 387 | - 'server', |
|
| 388 | - 'user_id' |
|
| 389 | - ); |
|
| 390 | - $this->assertTrue($result); |
|
| 391 | - |
|
| 392 | - // app password is encrypted and must look like: |
|
| 393 | - // ZACZOOzxTpKz4+KXL5kZ/gCK0xvkaVi/8yzupAn6Ui6+5qCSKvfPKGgeDRKs0sivvSLzk/XSp811SZCZmH0Y3g== |
|
| 394 | - $this->assertMatchesRegularExpression('/[a-zA-Z\/0-9+=]+/', $loginFlowV2->getAppPassword()); |
|
| 395 | - |
|
| 396 | - $this->assertEquals('server', $loginFlowV2->getServer()); |
|
| 397 | - } |
|
| 398 | - |
|
| 399 | - public function testFlowDoneDoesNotExistException(): void { |
|
| 400 | - $this->mapper->expects($this->once()) |
|
| 401 | - ->method('getByLoginToken') |
|
| 402 | - ->willThrowException(new DoesNotExistException('')); |
|
| 403 | - |
|
| 404 | - $result = $this->subjectUnderTest->flowDone( |
|
| 405 | - 'login_token', |
|
| 406 | - 'session_id', |
|
| 407 | - 'server', |
|
| 408 | - 'user_id' |
|
| 409 | - ); |
|
| 410 | - $this->assertFalse($result); |
|
| 411 | - } |
|
| 412 | - |
|
| 413 | - public function testFlowDonePasswordlessTokenException(): void { |
|
| 414 | - $this->tokenProvider->expects($this->once()) |
|
| 415 | - ->method('getToken') |
|
| 416 | - ->willThrowException(new InvalidTokenException('')); |
|
| 417 | - |
|
| 418 | - $result = $this->subjectUnderTest->flowDone( |
|
| 419 | - 'login_token', |
|
| 420 | - 'session_id', |
|
| 421 | - 'server', |
|
| 422 | - 'user_id' |
|
| 423 | - ); |
|
| 424 | - $this->assertFalse($result); |
|
| 425 | - } |
|
| 426 | - |
|
| 427 | - /* |
|
| 339 | + public function testFlowDone(): void { |
|
| 340 | + [,, $publicKey] = $this->getOpenSSLEncryptedPublicAndPrivateKey('test_pass'); |
|
| 341 | + |
|
| 342 | + $loginFlowV2 = new LoginFlowV2(); |
|
| 343 | + $loginFlowV2->setPublicKey($publicKey); |
|
| 344 | + $loginFlowV2->setClientName('client_name'); |
|
| 345 | + |
|
| 346 | + $this->mapper->expects($this->once()) |
|
| 347 | + ->method('getByLoginToken') |
|
| 348 | + ->willReturn($loginFlowV2); |
|
| 349 | + |
|
| 350 | + $this->mapper->expects($this->once()) |
|
| 351 | + ->method('update'); |
|
| 352 | + |
|
| 353 | + $this->secureRandom->expects($this->once()) |
|
| 354 | + ->method('generate') |
|
| 355 | + ->with(72, ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_DIGITS) |
|
| 356 | + ->willReturn('test_pass'); |
|
| 357 | + |
|
| 358 | + // session token |
|
| 359 | + $sessionToken = $this->getMockBuilder(IToken::class)->disableOriginalConstructor()->getMock(); |
|
| 360 | + $sessionToken->expects($this->once()) |
|
| 361 | + ->method('getLoginName') |
|
| 362 | + ->willReturn('login_name'); |
|
| 363 | + |
|
| 364 | + $this->tokenProvider->expects($this->once()) |
|
| 365 | + ->method('getPassword') |
|
| 366 | + ->willReturn('test_pass'); |
|
| 367 | + |
|
| 368 | + $this->tokenProvider->expects($this->once()) |
|
| 369 | + ->method('getToken') |
|
| 370 | + ->willReturn($sessionToken); |
|
| 371 | + |
|
| 372 | + $this->tokenProvider->expects($this->once()) |
|
| 373 | + ->method('generateToken') |
|
| 374 | + ->with( |
|
| 375 | + 'test_pass', |
|
| 376 | + 'user_id', |
|
| 377 | + 'login_name', |
|
| 378 | + 'test_pass', |
|
| 379 | + 'client_name', |
|
| 380 | + IToken::PERMANENT_TOKEN, |
|
| 381 | + IToken::DO_NOT_REMEMBER |
|
| 382 | + ); |
|
| 383 | + |
|
| 384 | + $result = $this->subjectUnderTest->flowDone( |
|
| 385 | + 'login_token', |
|
| 386 | + 'session_id', |
|
| 387 | + 'server', |
|
| 388 | + 'user_id' |
|
| 389 | + ); |
|
| 390 | + $this->assertTrue($result); |
|
| 391 | + |
|
| 392 | + // app password is encrypted and must look like: |
|
| 393 | + // ZACZOOzxTpKz4+KXL5kZ/gCK0xvkaVi/8yzupAn6Ui6+5qCSKvfPKGgeDRKs0sivvSLzk/XSp811SZCZmH0Y3g== |
|
| 394 | + $this->assertMatchesRegularExpression('/[a-zA-Z\/0-9+=]+/', $loginFlowV2->getAppPassword()); |
|
| 395 | + |
|
| 396 | + $this->assertEquals('server', $loginFlowV2->getServer()); |
|
| 397 | + } |
|
| 398 | + |
|
| 399 | + public function testFlowDoneDoesNotExistException(): void { |
|
| 400 | + $this->mapper->expects($this->once()) |
|
| 401 | + ->method('getByLoginToken') |
|
| 402 | + ->willThrowException(new DoesNotExistException('')); |
|
| 403 | + |
|
| 404 | + $result = $this->subjectUnderTest->flowDone( |
|
| 405 | + 'login_token', |
|
| 406 | + 'session_id', |
|
| 407 | + 'server', |
|
| 408 | + 'user_id' |
|
| 409 | + ); |
|
| 410 | + $this->assertFalse($result); |
|
| 411 | + } |
|
| 412 | + |
|
| 413 | + public function testFlowDonePasswordlessTokenException(): void { |
|
| 414 | + $this->tokenProvider->expects($this->once()) |
|
| 415 | + ->method('getToken') |
|
| 416 | + ->willThrowException(new InvalidTokenException('')); |
|
| 417 | + |
|
| 418 | + $result = $this->subjectUnderTest->flowDone( |
|
| 419 | + 'login_token', |
|
| 420 | + 'session_id', |
|
| 421 | + 'server', |
|
| 422 | + 'user_id' |
|
| 423 | + ); |
|
| 424 | + $this->assertFalse($result); |
|
| 425 | + } |
|
| 426 | + |
|
| 427 | + /* |
|
| 428 | 428 | * Tests for createTokens |
| 429 | 429 | */ |
| 430 | 430 | |
| 431 | - public function testCreateTokens(): void { |
|
| 432 | - $this->config->expects($this->exactly(2)) |
|
| 433 | - ->method('getSystemValue') |
|
| 434 | - ->willReturnCallback(function ($key) { |
|
| 435 | - // Note: \OCP\IConfig::getSystemValue returns either an array or string. |
|
| 436 | - return $key == 'openssl' ? [] : ''; |
|
| 437 | - }); |
|
| 431 | + public function testCreateTokens(): void { |
|
| 432 | + $this->config->expects($this->exactly(2)) |
|
| 433 | + ->method('getSystemValue') |
|
| 434 | + ->willReturnCallback(function ($key) { |
|
| 435 | + // Note: \OCP\IConfig::getSystemValue returns either an array or string. |
|
| 436 | + return $key == 'openssl' ? [] : ''; |
|
| 437 | + }); |
|
| 438 | 438 | |
| 439 | - $this->mapper->expects($this->once()) |
|
| 440 | - ->method('insert'); |
|
| 439 | + $this->mapper->expects($this->once()) |
|
| 440 | + ->method('insert'); |
|
| 441 | 441 | |
| 442 | - $this->secureRandom->expects($this->exactly(2)) |
|
| 443 | - ->method('generate'); |
|
| 442 | + $this->secureRandom->expects($this->exactly(2)) |
|
| 443 | + ->method('generate'); |
|
| 444 | 444 | |
| 445 | - $token = $this->subjectUnderTest->createTokens('user_agent'); |
|
| 446 | - $this->assertTrue($token instanceof LoginFlowV2Tokens); |
|
| 447 | - } |
|
| 445 | + $token = $this->subjectUnderTest->createTokens('user_agent'); |
|
| 446 | + $this->assertTrue($token instanceof LoginFlowV2Tokens); |
|
| 447 | + } |
|
| 448 | 448 | } |
@@ -135,11 +135,11 @@ discard block |
||
| 135 | 135 | * Cannot be mocked, because functions like getLoginName are magic functions. |
| 136 | 136 | * To be able to set internal properties, we have to use the real class here. |
| 137 | 137 | */ |
| 138 | - [$encrypted, $privateKey,] = $this->getOpenSSLEncryptedPublicAndPrivateKey('test'); |
|
| 138 | + [$encrypted, $privateKey, ] = $this->getOpenSSLEncryptedPublicAndPrivateKey('test'); |
|
| 139 | 139 | $loginFlowV2 = new LoginFlowV2(); |
| 140 | 140 | $loginFlowV2->setLoginName('test'); |
| 141 | 141 | $loginFlowV2->setServer('test'); |
| 142 | - $loginFlowV2->setAppPassword('broken#' . $encrypted); |
|
| 142 | + $loginFlowV2->setAppPassword('broken#'.$encrypted); |
|
| 143 | 143 | $loginFlowV2->setPrivateKey('encrypted(test)'); |
| 144 | 144 | |
| 145 | 145 | $this->crypto->expects($this->once()) |
@@ -252,7 +252,7 @@ discard block |
||
| 252 | 252 | |
| 253 | 253 | $this->config->expects($this->exactly(1)) |
| 254 | 254 | ->method('getSystemValue') |
| 255 | - ->willReturnCallback(function ($key) use ($allowedClients) { |
|
| 255 | + ->willReturnCallback(function($key) use ($allowedClients) { |
|
| 256 | 256 | // Note: \OCP\IConfig::getSystemValue returns either an array or string. |
| 257 | 257 | return $key == 'core.login_flow_v2.allowed_user_agents' ? $allowedClients : ''; |
| 258 | 258 | }); |
@@ -278,7 +278,7 @@ discard block |
||
| 278 | 278 | |
| 279 | 279 | $this->config->expects($this->exactly(1)) |
| 280 | 280 | ->method('getSystemValue') |
| 281 | - ->willReturnCallback(function ($key) use ($allowedClients) { |
|
| 281 | + ->willReturnCallback(function($key) use ($allowedClients) { |
|
| 282 | 282 | // Note: \OCP\IConfig::getSystemValue returns either an array or string. |
| 283 | 283 | return $key == 'core.login_flow_v2.allowed_user_agents' ? $allowedClients : ''; |
| 284 | 284 | }); |
@@ -352,7 +352,7 @@ discard block |
||
| 352 | 352 | |
| 353 | 353 | $this->secureRandom->expects($this->once()) |
| 354 | 354 | ->method('generate') |
| 355 | - ->with(72, ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_DIGITS) |
|
| 355 | + ->with(72, ISecureRandom::CHAR_UPPER.ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_DIGITS) |
|
| 356 | 356 | ->willReturn('test_pass'); |
| 357 | 357 | |
| 358 | 358 | // session token |
@@ -431,7 +431,7 @@ discard block |
||
| 431 | 431 | public function testCreateTokens(): void { |
| 432 | 432 | $this->config->expects($this->exactly(2)) |
| 433 | 433 | ->method('getSystemValue') |
| 434 | - ->willReturnCallback(function ($key) { |
|
| 434 | + ->willReturnCallback(function($key) { |
|
| 435 | 435 | // Note: \OCP\IConfig::getSystemValue returns either an array or string. |
| 436 | 436 | return $key == 'openssl' ? [] : ''; |
| 437 | 437 | }); |
@@ -13,55 +13,55 @@ |
||
| 13 | 13 | |
| 14 | 14 | #[\PHPUnit\Framework\Attributes\Group('Memcache')] |
| 15 | 15 | class CasTraitTest extends TestCase { |
| 16 | - /** |
|
| 17 | - * @return \OC\Memcache\CasTrait |
|
| 18 | - */ |
|
| 19 | - private function getCache() { |
|
| 20 | - $sourceCache = new ArrayCache(); |
|
| 21 | - $mock = $this->getMockBuilder(CasTraitTestClass::class)->onlyMethods([ |
|
| 22 | - 'set', |
|
| 23 | - 'get', |
|
| 24 | - 'add', |
|
| 25 | - 'remove', |
|
| 26 | - ])->getMock(); |
|
| 16 | + /** |
|
| 17 | + * @return \OC\Memcache\CasTrait |
|
| 18 | + */ |
|
| 19 | + private function getCache() { |
|
| 20 | + $sourceCache = new ArrayCache(); |
|
| 21 | + $mock = $this->getMockBuilder(CasTraitTestClass::class)->onlyMethods([ |
|
| 22 | + 'set', |
|
| 23 | + 'get', |
|
| 24 | + 'add', |
|
| 25 | + 'remove', |
|
| 26 | + ])->getMock(); |
|
| 27 | 27 | |
| 28 | - $mock->expects($this->any()) |
|
| 29 | - ->method('set') |
|
| 30 | - ->willReturnCallback(function ($key, $value, $ttl) use ($sourceCache) { |
|
| 31 | - return $sourceCache->set($key, $value, $ttl); |
|
| 32 | - }); |
|
| 28 | + $mock->expects($this->any()) |
|
| 29 | + ->method('set') |
|
| 30 | + ->willReturnCallback(function ($key, $value, $ttl) use ($sourceCache) { |
|
| 31 | + return $sourceCache->set($key, $value, $ttl); |
|
| 32 | + }); |
|
| 33 | 33 | |
| 34 | - $mock->expects($this->any()) |
|
| 35 | - ->method('get') |
|
| 36 | - ->willReturnCallback(function ($key) use ($sourceCache) { |
|
| 37 | - return $sourceCache->get($key); |
|
| 38 | - }); |
|
| 34 | + $mock->expects($this->any()) |
|
| 35 | + ->method('get') |
|
| 36 | + ->willReturnCallback(function ($key) use ($sourceCache) { |
|
| 37 | + return $sourceCache->get($key); |
|
| 38 | + }); |
|
| 39 | 39 | |
| 40 | - $mock->expects($this->any()) |
|
| 41 | - ->method('add') |
|
| 42 | - ->willReturnCallback(function ($key, $value, $ttl) use ($sourceCache) { |
|
| 43 | - return $sourceCache->add($key, $value, $ttl); |
|
| 44 | - }); |
|
| 40 | + $mock->expects($this->any()) |
|
| 41 | + ->method('add') |
|
| 42 | + ->willReturnCallback(function ($key, $value, $ttl) use ($sourceCache) { |
|
| 43 | + return $sourceCache->add($key, $value, $ttl); |
|
| 44 | + }); |
|
| 45 | 45 | |
| 46 | - $mock->expects($this->any()) |
|
| 47 | - ->method('remove') |
|
| 48 | - ->willReturnCallback(function ($key) use ($sourceCache) { |
|
| 49 | - return $sourceCache->remove($key); |
|
| 50 | - }); |
|
| 51 | - return $mock; |
|
| 52 | - } |
|
| 46 | + $mock->expects($this->any()) |
|
| 47 | + ->method('remove') |
|
| 48 | + ->willReturnCallback(function ($key) use ($sourceCache) { |
|
| 49 | + return $sourceCache->remove($key); |
|
| 50 | + }); |
|
| 51 | + return $mock; |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | - public function testCasNotChanged(): void { |
|
| 55 | - $cache = $this->getCache(); |
|
| 56 | - $cache->set('foo', 'bar'); |
|
| 57 | - $this->assertTrue($cache->cas('foo', 'bar', 'asd')); |
|
| 58 | - $this->assertEquals('asd', $cache->get('foo')); |
|
| 59 | - } |
|
| 54 | + public function testCasNotChanged(): void { |
|
| 55 | + $cache = $this->getCache(); |
|
| 56 | + $cache->set('foo', 'bar'); |
|
| 57 | + $this->assertTrue($cache->cas('foo', 'bar', 'asd')); |
|
| 58 | + $this->assertEquals('asd', $cache->get('foo')); |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - public function testCasChanged(): void { |
|
| 62 | - $cache = $this->getCache(); |
|
| 63 | - $cache->set('foo', 'bar1'); |
|
| 64 | - $this->assertFalse($cache->cas('foo', 'bar', 'asd')); |
|
| 65 | - $this->assertEquals('bar1', $cache->get('foo')); |
|
| 66 | - } |
|
| 61 | + public function testCasChanged(): void { |
|
| 62 | + $cache = $this->getCache(); |
|
| 63 | + $cache->set('foo', 'bar1'); |
|
| 64 | + $this->assertFalse($cache->cas('foo', 'bar', 'asd')); |
|
| 65 | + $this->assertEquals('bar1', $cache->get('foo')); |
|
| 66 | + } |
|
| 67 | 67 | } |
@@ -10,15 +10,15 @@ |
||
| 10 | 10 | use OC\Memcache\CASTrait; |
| 11 | 11 | |
| 12 | 12 | class CasTraitTestClass { |
| 13 | - use CASTrait; |
|
| 13 | + use CASTrait; |
|
| 14 | 14 | |
| 15 | - // abstract methods from Memcache |
|
| 16 | - public function set($key, $value, $ttl = 0) { |
|
| 17 | - } |
|
| 18 | - public function get($key) { |
|
| 19 | - } |
|
| 20 | - public function add($key, $value, $ttl = 0) { |
|
| 21 | - } |
|
| 22 | - public function remove($key) { |
|
| 23 | - } |
|
| 15 | + // abstract methods from Memcache |
|
| 16 | + public function set($key, $value, $ttl = 0) { |
|
| 17 | + } |
|
| 18 | + public function get($key) { |
|
| 19 | + } |
|
| 20 | + public function add($key, $value, $ttl = 0) { |
|
| 21 | + } |
|
| 22 | + public function remove($key) { |
|
| 23 | + } |
|
| 24 | 24 | } |
@@ -16,71 +16,71 @@ |
||
| 16 | 16 | * This class provides tests for the MemoryInfo class. |
| 17 | 17 | */ |
| 18 | 18 | class MemoryInfoTest extends TestCase { |
| 19 | - /** |
|
| 20 | - * The "memory_limit" value before tests. |
|
| 21 | - */ |
|
| 22 | - private string $iniSettingBeforeTest; |
|
| 19 | + /** |
|
| 20 | + * The "memory_limit" value before tests. |
|
| 21 | + */ |
|
| 22 | + private string $iniSettingBeforeTest; |
|
| 23 | 23 | |
| 24 | - #[\PHPUnit\Framework\Attributes\BeforeClass()] |
|
| 25 | - public function backupMemoryInfoIniSetting() { |
|
| 26 | - $this->iniSettingBeforeTest = ini_get('memory_limit'); |
|
| 27 | - } |
|
| 24 | + #[\PHPUnit\Framework\Attributes\BeforeClass()] |
|
| 25 | + public function backupMemoryInfoIniSetting() { |
|
| 26 | + $this->iniSettingBeforeTest = ini_get('memory_limit'); |
|
| 27 | + } |
|
| 28 | 28 | |
| 29 | - #[\PHPUnit\Framework\Attributes\AfterClass()] |
|
| 30 | - public function restoreMemoryInfoIniSetting() { |
|
| 31 | - ini_set('memory_limit', $this->iniSettingBeforeTest); |
|
| 32 | - } |
|
| 29 | + #[\PHPUnit\Framework\Attributes\AfterClass()] |
|
| 30 | + public function restoreMemoryInfoIniSetting() { |
|
| 31 | + ini_set('memory_limit', $this->iniSettingBeforeTest); |
|
| 32 | + } |
|
| 33 | 33 | |
| 34 | - public static function getMemoryLimitTestData(): array { |
|
| 35 | - return [ |
|
| 36 | - 'unlimited' => ['-1', -1,], |
|
| 37 | - '524288000 bytes' => ['524288000', 524288000,], |
|
| 38 | - '500M' => ['500M', 524288000,], |
|
| 39 | - '512000K' => ['512000K', 524288000,], |
|
| 40 | - '2G' => ['2G', 2147483648,], |
|
| 41 | - ]; |
|
| 42 | - } |
|
| 34 | + public static function getMemoryLimitTestData(): array { |
|
| 35 | + return [ |
|
| 36 | + 'unlimited' => ['-1', -1,], |
|
| 37 | + '524288000 bytes' => ['524288000', 524288000,], |
|
| 38 | + '500M' => ['500M', 524288000,], |
|
| 39 | + '512000K' => ['512000K', 524288000,], |
|
| 40 | + '2G' => ['2G', 2147483648,], |
|
| 41 | + ]; |
|
| 42 | + } |
|
| 43 | 43 | |
| 44 | - /** |
|
| 45 | - * Tests that getMemoryLimit works as expected. |
|
| 46 | - * |
|
| 47 | - * @param string $iniValue The "memory_limit" ini data. |
|
| 48 | - * @param int|float $expected The expected detected memory limit. |
|
| 49 | - */ |
|
| 50 | - #[\PHPUnit\Framework\Attributes\DataProvider('getMemoryLimitTestData')] |
|
| 51 | - public function testMemoryLimit(string $iniValue, int|float $expected): void { |
|
| 52 | - ini_set('memory_limit', $iniValue); |
|
| 53 | - $memoryInfo = new MemoryInfo(); |
|
| 54 | - self::assertEquals($expected, $memoryInfo->getMemoryLimit()); |
|
| 55 | - } |
|
| 44 | + /** |
|
| 45 | + * Tests that getMemoryLimit works as expected. |
|
| 46 | + * |
|
| 47 | + * @param string $iniValue The "memory_limit" ini data. |
|
| 48 | + * @param int|float $expected The expected detected memory limit. |
|
| 49 | + */ |
|
| 50 | + #[\PHPUnit\Framework\Attributes\DataProvider('getMemoryLimitTestData')] |
|
| 51 | + public function testMemoryLimit(string $iniValue, int|float $expected): void { |
|
| 52 | + ini_set('memory_limit', $iniValue); |
|
| 53 | + $memoryInfo = new MemoryInfo(); |
|
| 54 | + self::assertEquals($expected, $memoryInfo->getMemoryLimit()); |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | - public static function getSufficientMemoryTestData(): array { |
|
| 58 | - return [ |
|
| 59 | - 'unlimited' => [-1, true,], |
|
| 60 | - '512M' => [512 * 1024 * 1024, true,], |
|
| 61 | - '1G' => [1024 * 1024 * 1024, true,], |
|
| 62 | - '256M' => [256 * 1024 * 1024, false,], |
|
| 63 | - ]; |
|
| 64 | - } |
|
| 57 | + public static function getSufficientMemoryTestData(): array { |
|
| 58 | + return [ |
|
| 59 | + 'unlimited' => [-1, true,], |
|
| 60 | + '512M' => [512 * 1024 * 1024, true,], |
|
| 61 | + '1G' => [1024 * 1024 * 1024, true,], |
|
| 62 | + '256M' => [256 * 1024 * 1024, false,], |
|
| 63 | + ]; |
|
| 64 | + } |
|
| 65 | 65 | |
| 66 | - /** |
|
| 67 | - * Tests that isMemoryLimitSufficient returns the correct values. |
|
| 68 | - * |
|
| 69 | - * @param int $memoryLimit The memory limit |
|
| 70 | - * @param bool $expected If the memory limit is sufficient. |
|
| 71 | - */ |
|
| 72 | - #[\PHPUnit\Framework\Attributes\DataProvider('getSufficientMemoryTestData')] |
|
| 73 | - public function testIsMemoryLimitSufficient(int $memoryLimit, bool $expected): void { |
|
| 74 | - /* @var MemoryInfo|MockObject $memoryInfo */ |
|
| 75 | - $memoryInfo = $this->getMockBuilder(MemoryInfo::class) |
|
| 76 | - ->onlyMethods(['getMemoryLimit',]) |
|
| 77 | - ->getMock(); |
|
| 66 | + /** |
|
| 67 | + * Tests that isMemoryLimitSufficient returns the correct values. |
|
| 68 | + * |
|
| 69 | + * @param int $memoryLimit The memory limit |
|
| 70 | + * @param bool $expected If the memory limit is sufficient. |
|
| 71 | + */ |
|
| 72 | + #[\PHPUnit\Framework\Attributes\DataProvider('getSufficientMemoryTestData')] |
|
| 73 | + public function testIsMemoryLimitSufficient(int $memoryLimit, bool $expected): void { |
|
| 74 | + /* @var MemoryInfo|MockObject $memoryInfo */ |
|
| 75 | + $memoryInfo = $this->getMockBuilder(MemoryInfo::class) |
|
| 76 | + ->onlyMethods(['getMemoryLimit',]) |
|
| 77 | + ->getMock(); |
|
| 78 | 78 | |
| 79 | - $memoryInfo |
|
| 80 | - ->method('getMemoryLimit') |
|
| 81 | - ->willReturn($memoryLimit); |
|
| 79 | + $memoryInfo |
|
| 80 | + ->method('getMemoryLimit') |
|
| 81 | + ->willReturn($memoryLimit); |
|
| 82 | 82 | |
| 83 | - $isMemoryLimitSufficient = $memoryInfo->isMemoryLimitSufficient(); |
|
| 84 | - self::assertEquals($expected, $isMemoryLimitSufficient); |
|
| 85 | - } |
|
| 83 | + $isMemoryLimitSufficient = $memoryInfo->isMemoryLimitSufficient(); |
|
| 84 | + self::assertEquals($expected, $isMemoryLimitSufficient); |
|
| 85 | + } |
|
| 86 | 86 | } |
@@ -20,207 +20,207 @@ |
||
| 20 | 20 | |
| 21 | 21 | #[\PHPUnit\Framework\Attributes\Group('DB')] |
| 22 | 22 | class PartitionedQueryBuilderTest extends TestCase { |
| 23 | - private IDBConnection $connection; |
|
| 24 | - private ShardConnectionManager $shardConnectionManager; |
|
| 25 | - private AutoIncrementHandler $autoIncrementHandler; |
|
| 26 | - |
|
| 27 | - protected function setUp(): void { |
|
| 28 | - if (PHP_INT_SIZE < 8) { |
|
| 29 | - $this->markTestSkipped('Test requires 64bit'); |
|
| 30 | - return; |
|
| 31 | - } |
|
| 32 | - $this->connection = Server::get(IDBConnection::class); |
|
| 33 | - $this->shardConnectionManager = Server::get(ShardConnectionManager::class); |
|
| 34 | - $this->autoIncrementHandler = Server::get(AutoIncrementHandler::class); |
|
| 35 | - |
|
| 36 | - $this->setupFileCache(); |
|
| 37 | - } |
|
| 38 | - |
|
| 39 | - protected function tearDown(): void { |
|
| 40 | - // PHP unit also runs tearDown when the test is skipped, but we only initialized when using 64bit |
|
| 41 | - // see https://github.com/sebastianbergmann/phpunit/issues/6394 |
|
| 42 | - if (PHP_INT_SIZE >= 8) { |
|
| 43 | - $this->cleanupDb(); |
|
| 44 | - } |
|
| 45 | - parent::tearDown(); |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - |
|
| 49 | - private function getQueryBuilder(): PartitionedQueryBuilder { |
|
| 50 | - $builder = $this->connection->getQueryBuilder(); |
|
| 51 | - if ($builder instanceof PartitionedQueryBuilder) { |
|
| 52 | - return $builder; |
|
| 53 | - } else { |
|
| 54 | - return new PartitionedQueryBuilder($builder, [], $this->shardConnectionManager, $this->autoIncrementHandler); |
|
| 55 | - } |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - private function setupFileCache(): void { |
|
| 59 | - $this->cleanupDb(); |
|
| 60 | - $query = $this->getQueryBuilder(); |
|
| 61 | - $query->insert('storages') |
|
| 62 | - ->values([ |
|
| 63 | - 'numeric_id' => $query->createNamedParameter(1001001, IQueryBuilder::PARAM_INT), |
|
| 64 | - 'id' => $query->createNamedParameter('test1'), |
|
| 65 | - ]); |
|
| 66 | - $query->executeStatement(); |
|
| 67 | - |
|
| 68 | - $query = $this->getQueryBuilder(); |
|
| 69 | - $query->insert('filecache') |
|
| 70 | - ->values([ |
|
| 71 | - 'storage' => $query->createNamedParameter(1001001, IQueryBuilder::PARAM_INT), |
|
| 72 | - 'path' => $query->createNamedParameter('file1'), |
|
| 73 | - 'path_hash' => $query->createNamedParameter(md5('file1')), |
|
| 74 | - ]); |
|
| 75 | - $query->executeStatement(); |
|
| 76 | - $fileId = $query->getLastInsertId(); |
|
| 77 | - |
|
| 78 | - $query = $this->getQueryBuilder(); |
|
| 79 | - $query->insert('filecache_extended') |
|
| 80 | - ->hintShardKey('storage', 1001001) |
|
| 81 | - ->values([ |
|
| 82 | - 'fileid' => $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT), |
|
| 83 | - 'upload_time' => $query->createNamedParameter(1234, IQueryBuilder::PARAM_INT), |
|
| 84 | - ]); |
|
| 85 | - $query->executeStatement(); |
|
| 86 | - |
|
| 87 | - $query = $this->getQueryBuilder(); |
|
| 88 | - $query->insert('mounts') |
|
| 89 | - ->values([ |
|
| 90 | - 'storage_id' => $query->createNamedParameter(1001001, IQueryBuilder::PARAM_INT), |
|
| 91 | - 'user_id' => $query->createNamedParameter('partitioned_test'), |
|
| 92 | - 'mount_point' => $query->createNamedParameter('/mount/point'), |
|
| 93 | - 'mount_provider_class' => $query->createNamedParameter('test'), |
|
| 94 | - 'root_id' => $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT), |
|
| 95 | - ]); |
|
| 96 | - $query->executeStatement(); |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - private function cleanupDb(): void { |
|
| 100 | - $query = $this->getQueryBuilder(); |
|
| 101 | - $query->delete('storages') |
|
| 102 | - ->where($query->expr()->gt('numeric_id', $query->createNamedParameter(1000000, IQueryBuilder::PARAM_INT))); |
|
| 103 | - $query->executeStatement(); |
|
| 104 | - |
|
| 105 | - $query = $this->getQueryBuilder(); |
|
| 106 | - $query->delete('filecache') |
|
| 107 | - ->where($query->expr()->gt('storage', $query->createNamedParameter(1000000, IQueryBuilder::PARAM_INT))) |
|
| 108 | - ->runAcrossAllShards(); |
|
| 109 | - $query->executeStatement(); |
|
| 110 | - |
|
| 111 | - $query = $this->getQueryBuilder(); |
|
| 112 | - $query->delete('filecache_extended') |
|
| 113 | - ->runAcrossAllShards(); |
|
| 114 | - $query->executeStatement(); |
|
| 115 | - |
|
| 116 | - $query = $this->getQueryBuilder(); |
|
| 117 | - $query->delete('mounts') |
|
| 118 | - ->where($query->expr()->like('user_id', $query->createNamedParameter('partitioned_%'))); |
|
| 119 | - $query->executeStatement(); |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - public function testSimpleOnlyPartitionQuery(): void { |
|
| 123 | - $builder = $this->getQueryBuilder(); |
|
| 124 | - $builder->addPartition(new PartitionSplit('filecache', ['filecache'])); |
|
| 125 | - |
|
| 126 | - // query borrowed from UserMountCache |
|
| 127 | - $query = $builder->select('path') |
|
| 128 | - ->from('filecache') |
|
| 129 | - ->where($builder->expr()->eq('storage', $builder->createNamedParameter(1001001, IQueryBuilder::PARAM_INT))); |
|
| 130 | - |
|
| 131 | - $results = $query->executeQuery()->fetchAll(); |
|
| 132 | - $this->assertCount(1, $results); |
|
| 133 | - $this->assertEquals($results[0]['path'], 'file1'); |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - public function testSimplePartitionedQuery(): void { |
|
| 137 | - $builder = $this->getQueryBuilder(); |
|
| 138 | - $builder->addPartition(new PartitionSplit('filecache', ['filecache'])); |
|
| 139 | - |
|
| 140 | - // query borrowed from UserMountCache |
|
| 141 | - $query = $builder->select('storage_id', 'root_id', 'user_id', 'mount_point', 'mount_id', 'f.path', 'mount_provider_class') |
|
| 142 | - ->from('mounts', 'm') |
|
| 143 | - ->innerJoin('m', 'filecache', 'f', $builder->expr()->eq('m.root_id', 'f.fileid')) |
|
| 144 | - ->where($builder->expr()->eq('storage_id', $builder->createNamedParameter(1001001, IQueryBuilder::PARAM_INT))); |
|
| 145 | - |
|
| 146 | - $query->andWhere($builder->expr()->eq('user_id', $builder->createNamedParameter('partitioned_test'))); |
|
| 147 | - |
|
| 148 | - $this->assertEquals(2, $query->getPartitionCount()); |
|
| 149 | - |
|
| 150 | - $results = $query->executeQuery()->fetchAll(); |
|
| 151 | - $this->assertCount(1, $results); |
|
| 152 | - $this->assertEquals($results[0]['user_id'], 'partitioned_test'); |
|
| 153 | - $this->assertEquals($results[0]['mount_point'], '/mount/point'); |
|
| 154 | - $this->assertEquals($results[0]['mount_provider_class'], 'test'); |
|
| 155 | - $this->assertEquals($results[0]['path'], 'file1'); |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - public function testMultiTablePartitionedQuery(): void { |
|
| 159 | - $builder = $this->getQueryBuilder(); |
|
| 160 | - $builder->addPartition(new PartitionSplit('filecache', ['filecache', 'filecache_extended'])); |
|
| 161 | - |
|
| 162 | - $query = $builder->select('storage_id', 'root_id', 'user_id', 'mount_point', 'mount_id', 'f.path', 'mount_provider_class', 'fe.upload_time') |
|
| 163 | - ->from('mounts', 'm') |
|
| 164 | - ->innerJoin('m', 'filecache', 'f', $builder->expr()->eq('m.root_id', 'f.fileid')) |
|
| 165 | - ->innerJoin('f', 'filecache_extended', 'fe', $builder->expr()->eq('f.fileid', 'fe.fileid')) |
|
| 166 | - ->where($builder->expr()->eq('storage_id', $builder->createNamedParameter(1001001, IQueryBuilder::PARAM_INT))); |
|
| 167 | - |
|
| 168 | - $query->andWhere($builder->expr()->eq('user_id', $builder->createNamedParameter('partitioned_test'))); |
|
| 169 | - |
|
| 170 | - $this->assertEquals(2, $query->getPartitionCount()); |
|
| 171 | - |
|
| 172 | - $results = $query->executeQuery()->fetchAll(); |
|
| 173 | - $this->assertCount(1, $results); |
|
| 174 | - $this->assertEquals($results[0]['user_id'], 'partitioned_test'); |
|
| 175 | - $this->assertEquals($results[0]['mount_point'], '/mount/point'); |
|
| 176 | - $this->assertEquals($results[0]['mount_provider_class'], 'test'); |
|
| 177 | - $this->assertEquals($results[0]['path'], 'file1'); |
|
| 178 | - $this->assertEquals($results[0]['upload_time'], 1234); |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - public function testPartitionedQueryFromSplit(): void { |
|
| 182 | - $builder = $this->getQueryBuilder(); |
|
| 183 | - $builder->addPartition(new PartitionSplit('filecache', ['filecache'])); |
|
| 184 | - |
|
| 185 | - $query = $builder->select('storage', 'm.root_id', 'm.user_id', 'm.mount_point', 'm.mount_id', 'path', 'm.mount_provider_class') |
|
| 186 | - ->from('filecache', 'f') |
|
| 187 | - ->innerJoin('f', 'mounts', 'm', $builder->expr()->eq('m.root_id', 'f.fileid')); |
|
| 188 | - $query->where($builder->expr()->eq('storage', $builder->createNamedParameter(1001001, IQueryBuilder::PARAM_INT))); |
|
| 189 | - |
|
| 190 | - $query->andWhere($builder->expr()->eq('m.user_id', $builder->createNamedParameter('partitioned_test'))); |
|
| 191 | - |
|
| 192 | - $this->assertEquals(2, $query->getPartitionCount()); |
|
| 193 | - |
|
| 194 | - $results = $query->executeQuery()->fetchAll(); |
|
| 195 | - $this->assertCount(1, $results); |
|
| 196 | - $this->assertEquals($results[0]['user_id'], 'partitioned_test'); |
|
| 197 | - $this->assertEquals($results[0]['mount_point'], '/mount/point'); |
|
| 198 | - $this->assertEquals($results[0]['mount_provider_class'], 'test'); |
|
| 199 | - $this->assertEquals($results[0]['path'], 'file1'); |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - public function testMultiJoinPartitionedQuery(): void { |
|
| 203 | - $builder = $this->getQueryBuilder(); |
|
| 204 | - $builder->addPartition(new PartitionSplit('filecache', ['filecache'])); |
|
| 205 | - |
|
| 206 | - // query borrowed from UserMountCache |
|
| 207 | - $query = $builder->select('storage_id', 'root_id', 'user_id', 'mount_point', 'mount_id', 'f.path', 'mount_provider_class') |
|
| 208 | - ->selectAlias('s.id', 'storage_string_id') |
|
| 209 | - ->from('mounts', 'm') |
|
| 210 | - ->innerJoin('m', 'filecache', 'f', $builder->expr()->eq('m.root_id', 'f.fileid')) |
|
| 211 | - ->innerJoin('f', 'storages', 's', $builder->expr()->eq('f.storage', 's.numeric_id')) |
|
| 212 | - ->where($builder->expr()->eq('storage_id', $builder->createNamedParameter(1001001, IQueryBuilder::PARAM_INT))); |
|
| 213 | - |
|
| 214 | - $query->andWhere($builder->expr()->eq('user_id', $builder->createNamedParameter('partitioned_test'))); |
|
| 215 | - |
|
| 216 | - $this->assertEquals(3, $query->getPartitionCount()); |
|
| 217 | - |
|
| 218 | - $results = $query->executeQuery()->fetchAll(); |
|
| 219 | - $this->assertCount(1, $results); |
|
| 220 | - $this->assertEquals($results[0]['user_id'], 'partitioned_test'); |
|
| 221 | - $this->assertEquals($results[0]['mount_point'], '/mount/point'); |
|
| 222 | - $this->assertEquals($results[0]['mount_provider_class'], 'test'); |
|
| 223 | - $this->assertEquals($results[0]['path'], 'file1'); |
|
| 224 | - $this->assertEquals($results[0]['storage_string_id'], 'test1'); |
|
| 225 | - } |
|
| 23 | + private IDBConnection $connection; |
|
| 24 | + private ShardConnectionManager $shardConnectionManager; |
|
| 25 | + private AutoIncrementHandler $autoIncrementHandler; |
|
| 26 | + |
|
| 27 | + protected function setUp(): void { |
|
| 28 | + if (PHP_INT_SIZE < 8) { |
|
| 29 | + $this->markTestSkipped('Test requires 64bit'); |
|
| 30 | + return; |
|
| 31 | + } |
|
| 32 | + $this->connection = Server::get(IDBConnection::class); |
|
| 33 | + $this->shardConnectionManager = Server::get(ShardConnectionManager::class); |
|
| 34 | + $this->autoIncrementHandler = Server::get(AutoIncrementHandler::class); |
|
| 35 | + |
|
| 36 | + $this->setupFileCache(); |
|
| 37 | + } |
|
| 38 | + |
|
| 39 | + protected function tearDown(): void { |
|
| 40 | + // PHP unit also runs tearDown when the test is skipped, but we only initialized when using 64bit |
|
| 41 | + // see https://github.com/sebastianbergmann/phpunit/issues/6394 |
|
| 42 | + if (PHP_INT_SIZE >= 8) { |
|
| 43 | + $this->cleanupDb(); |
|
| 44 | + } |
|
| 45 | + parent::tearDown(); |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + |
|
| 49 | + private function getQueryBuilder(): PartitionedQueryBuilder { |
|
| 50 | + $builder = $this->connection->getQueryBuilder(); |
|
| 51 | + if ($builder instanceof PartitionedQueryBuilder) { |
|
| 52 | + return $builder; |
|
| 53 | + } else { |
|
| 54 | + return new PartitionedQueryBuilder($builder, [], $this->shardConnectionManager, $this->autoIncrementHandler); |
|
| 55 | + } |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + private function setupFileCache(): void { |
|
| 59 | + $this->cleanupDb(); |
|
| 60 | + $query = $this->getQueryBuilder(); |
|
| 61 | + $query->insert('storages') |
|
| 62 | + ->values([ |
|
| 63 | + 'numeric_id' => $query->createNamedParameter(1001001, IQueryBuilder::PARAM_INT), |
|
| 64 | + 'id' => $query->createNamedParameter('test1'), |
|
| 65 | + ]); |
|
| 66 | + $query->executeStatement(); |
|
| 67 | + |
|
| 68 | + $query = $this->getQueryBuilder(); |
|
| 69 | + $query->insert('filecache') |
|
| 70 | + ->values([ |
|
| 71 | + 'storage' => $query->createNamedParameter(1001001, IQueryBuilder::PARAM_INT), |
|
| 72 | + 'path' => $query->createNamedParameter('file1'), |
|
| 73 | + 'path_hash' => $query->createNamedParameter(md5('file1')), |
|
| 74 | + ]); |
|
| 75 | + $query->executeStatement(); |
|
| 76 | + $fileId = $query->getLastInsertId(); |
|
| 77 | + |
|
| 78 | + $query = $this->getQueryBuilder(); |
|
| 79 | + $query->insert('filecache_extended') |
|
| 80 | + ->hintShardKey('storage', 1001001) |
|
| 81 | + ->values([ |
|
| 82 | + 'fileid' => $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT), |
|
| 83 | + 'upload_time' => $query->createNamedParameter(1234, IQueryBuilder::PARAM_INT), |
|
| 84 | + ]); |
|
| 85 | + $query->executeStatement(); |
|
| 86 | + |
|
| 87 | + $query = $this->getQueryBuilder(); |
|
| 88 | + $query->insert('mounts') |
|
| 89 | + ->values([ |
|
| 90 | + 'storage_id' => $query->createNamedParameter(1001001, IQueryBuilder::PARAM_INT), |
|
| 91 | + 'user_id' => $query->createNamedParameter('partitioned_test'), |
|
| 92 | + 'mount_point' => $query->createNamedParameter('/mount/point'), |
|
| 93 | + 'mount_provider_class' => $query->createNamedParameter('test'), |
|
| 94 | + 'root_id' => $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT), |
|
| 95 | + ]); |
|
| 96 | + $query->executeStatement(); |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + private function cleanupDb(): void { |
|
| 100 | + $query = $this->getQueryBuilder(); |
|
| 101 | + $query->delete('storages') |
|
| 102 | + ->where($query->expr()->gt('numeric_id', $query->createNamedParameter(1000000, IQueryBuilder::PARAM_INT))); |
|
| 103 | + $query->executeStatement(); |
|
| 104 | + |
|
| 105 | + $query = $this->getQueryBuilder(); |
|
| 106 | + $query->delete('filecache') |
|
| 107 | + ->where($query->expr()->gt('storage', $query->createNamedParameter(1000000, IQueryBuilder::PARAM_INT))) |
|
| 108 | + ->runAcrossAllShards(); |
|
| 109 | + $query->executeStatement(); |
|
| 110 | + |
|
| 111 | + $query = $this->getQueryBuilder(); |
|
| 112 | + $query->delete('filecache_extended') |
|
| 113 | + ->runAcrossAllShards(); |
|
| 114 | + $query->executeStatement(); |
|
| 115 | + |
|
| 116 | + $query = $this->getQueryBuilder(); |
|
| 117 | + $query->delete('mounts') |
|
| 118 | + ->where($query->expr()->like('user_id', $query->createNamedParameter('partitioned_%'))); |
|
| 119 | + $query->executeStatement(); |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + public function testSimpleOnlyPartitionQuery(): void { |
|
| 123 | + $builder = $this->getQueryBuilder(); |
|
| 124 | + $builder->addPartition(new PartitionSplit('filecache', ['filecache'])); |
|
| 125 | + |
|
| 126 | + // query borrowed from UserMountCache |
|
| 127 | + $query = $builder->select('path') |
|
| 128 | + ->from('filecache') |
|
| 129 | + ->where($builder->expr()->eq('storage', $builder->createNamedParameter(1001001, IQueryBuilder::PARAM_INT))); |
|
| 130 | + |
|
| 131 | + $results = $query->executeQuery()->fetchAll(); |
|
| 132 | + $this->assertCount(1, $results); |
|
| 133 | + $this->assertEquals($results[0]['path'], 'file1'); |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + public function testSimplePartitionedQuery(): void { |
|
| 137 | + $builder = $this->getQueryBuilder(); |
|
| 138 | + $builder->addPartition(new PartitionSplit('filecache', ['filecache'])); |
|
| 139 | + |
|
| 140 | + // query borrowed from UserMountCache |
|
| 141 | + $query = $builder->select('storage_id', 'root_id', 'user_id', 'mount_point', 'mount_id', 'f.path', 'mount_provider_class') |
|
| 142 | + ->from('mounts', 'm') |
|
| 143 | + ->innerJoin('m', 'filecache', 'f', $builder->expr()->eq('m.root_id', 'f.fileid')) |
|
| 144 | + ->where($builder->expr()->eq('storage_id', $builder->createNamedParameter(1001001, IQueryBuilder::PARAM_INT))); |
|
| 145 | + |
|
| 146 | + $query->andWhere($builder->expr()->eq('user_id', $builder->createNamedParameter('partitioned_test'))); |
|
| 147 | + |
|
| 148 | + $this->assertEquals(2, $query->getPartitionCount()); |
|
| 149 | + |
|
| 150 | + $results = $query->executeQuery()->fetchAll(); |
|
| 151 | + $this->assertCount(1, $results); |
|
| 152 | + $this->assertEquals($results[0]['user_id'], 'partitioned_test'); |
|
| 153 | + $this->assertEquals($results[0]['mount_point'], '/mount/point'); |
|
| 154 | + $this->assertEquals($results[0]['mount_provider_class'], 'test'); |
|
| 155 | + $this->assertEquals($results[0]['path'], 'file1'); |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + public function testMultiTablePartitionedQuery(): void { |
|
| 159 | + $builder = $this->getQueryBuilder(); |
|
| 160 | + $builder->addPartition(new PartitionSplit('filecache', ['filecache', 'filecache_extended'])); |
|
| 161 | + |
|
| 162 | + $query = $builder->select('storage_id', 'root_id', 'user_id', 'mount_point', 'mount_id', 'f.path', 'mount_provider_class', 'fe.upload_time') |
|
| 163 | + ->from('mounts', 'm') |
|
| 164 | + ->innerJoin('m', 'filecache', 'f', $builder->expr()->eq('m.root_id', 'f.fileid')) |
|
| 165 | + ->innerJoin('f', 'filecache_extended', 'fe', $builder->expr()->eq('f.fileid', 'fe.fileid')) |
|
| 166 | + ->where($builder->expr()->eq('storage_id', $builder->createNamedParameter(1001001, IQueryBuilder::PARAM_INT))); |
|
| 167 | + |
|
| 168 | + $query->andWhere($builder->expr()->eq('user_id', $builder->createNamedParameter('partitioned_test'))); |
|
| 169 | + |
|
| 170 | + $this->assertEquals(2, $query->getPartitionCount()); |
|
| 171 | + |
|
| 172 | + $results = $query->executeQuery()->fetchAll(); |
|
| 173 | + $this->assertCount(1, $results); |
|
| 174 | + $this->assertEquals($results[0]['user_id'], 'partitioned_test'); |
|
| 175 | + $this->assertEquals($results[0]['mount_point'], '/mount/point'); |
|
| 176 | + $this->assertEquals($results[0]['mount_provider_class'], 'test'); |
|
| 177 | + $this->assertEquals($results[0]['path'], 'file1'); |
|
| 178 | + $this->assertEquals($results[0]['upload_time'], 1234); |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + public function testPartitionedQueryFromSplit(): void { |
|
| 182 | + $builder = $this->getQueryBuilder(); |
|
| 183 | + $builder->addPartition(new PartitionSplit('filecache', ['filecache'])); |
|
| 184 | + |
|
| 185 | + $query = $builder->select('storage', 'm.root_id', 'm.user_id', 'm.mount_point', 'm.mount_id', 'path', 'm.mount_provider_class') |
|
| 186 | + ->from('filecache', 'f') |
|
| 187 | + ->innerJoin('f', 'mounts', 'm', $builder->expr()->eq('m.root_id', 'f.fileid')); |
|
| 188 | + $query->where($builder->expr()->eq('storage', $builder->createNamedParameter(1001001, IQueryBuilder::PARAM_INT))); |
|
| 189 | + |
|
| 190 | + $query->andWhere($builder->expr()->eq('m.user_id', $builder->createNamedParameter('partitioned_test'))); |
|
| 191 | + |
|
| 192 | + $this->assertEquals(2, $query->getPartitionCount()); |
|
| 193 | + |
|
| 194 | + $results = $query->executeQuery()->fetchAll(); |
|
| 195 | + $this->assertCount(1, $results); |
|
| 196 | + $this->assertEquals($results[0]['user_id'], 'partitioned_test'); |
|
| 197 | + $this->assertEquals($results[0]['mount_point'], '/mount/point'); |
|
| 198 | + $this->assertEquals($results[0]['mount_provider_class'], 'test'); |
|
| 199 | + $this->assertEquals($results[0]['path'], 'file1'); |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + public function testMultiJoinPartitionedQuery(): void { |
|
| 203 | + $builder = $this->getQueryBuilder(); |
|
| 204 | + $builder->addPartition(new PartitionSplit('filecache', ['filecache'])); |
|
| 205 | + |
|
| 206 | + // query borrowed from UserMountCache |
|
| 207 | + $query = $builder->select('storage_id', 'root_id', 'user_id', 'mount_point', 'mount_id', 'f.path', 'mount_provider_class') |
|
| 208 | + ->selectAlias('s.id', 'storage_string_id') |
|
| 209 | + ->from('mounts', 'm') |
|
| 210 | + ->innerJoin('m', 'filecache', 'f', $builder->expr()->eq('m.root_id', 'f.fileid')) |
|
| 211 | + ->innerJoin('f', 'storages', 's', $builder->expr()->eq('f.storage', 's.numeric_id')) |
|
| 212 | + ->where($builder->expr()->eq('storage_id', $builder->createNamedParameter(1001001, IQueryBuilder::PARAM_INT))); |
|
| 213 | + |
|
| 214 | + $query->andWhere($builder->expr()->eq('user_id', $builder->createNamedParameter('partitioned_test'))); |
|
| 215 | + |
|
| 216 | + $this->assertEquals(3, $query->getPartitionCount()); |
|
| 217 | + |
|
| 218 | + $results = $query->executeQuery()->fetchAll(); |
|
| 219 | + $this->assertCount(1, $results); |
|
| 220 | + $this->assertEquals($results[0]['user_id'], 'partitioned_test'); |
|
| 221 | + $this->assertEquals($results[0]['mount_point'], '/mount/point'); |
|
| 222 | + $this->assertEquals($results[0]['mount_provider_class'], 'test'); |
|
| 223 | + $this->assertEquals($results[0]['path'], 'file1'); |
|
| 224 | + $this->assertEquals($results[0]['storage_string_id'], 'test1'); |
|
| 225 | + } |
|
| 226 | 226 | } |
@@ -20,44 +20,44 @@ |
||
| 20 | 20 | #[CoversClass(PreviewService::class)] |
| 21 | 21 | #[\PHPUnit\Framework\Attributes\Group('DB')] |
| 22 | 22 | class PreviewServiceTest extends TestCase { |
| 23 | - private PreviewService $previewService; |
|
| 24 | - private PreviewMapper $previewMapper; |
|
| 25 | - private IGenerator $snowflakeGenerator; |
|
| 26 | - |
|
| 27 | - protected function setUp(): void { |
|
| 28 | - parent::setUp(); |
|
| 29 | - $this->previewService = Server::get(PreviewService::class); |
|
| 30 | - $this->previewMapper = Server::get(PreviewMapper::class); |
|
| 31 | - $this->snowflakeGenerator = Server::get(IGenerator::class); |
|
| 32 | - $this->previewService->deleteAll(); |
|
| 33 | - } |
|
| 34 | - |
|
| 35 | - public function tearDown(): void { |
|
| 36 | - $this->previewService->deleteAll(); |
|
| 37 | - parent::tearDown(); |
|
| 38 | - } |
|
| 39 | - |
|
| 40 | - public function testGetAvailableFileIds(): void { |
|
| 41 | - foreach (range(1, 20) as $i) { |
|
| 42 | - $preview = new Preview(); |
|
| 43 | - $preview->setId($this->snowflakeGenerator->nextId()); |
|
| 44 | - $preview->setFileId($i % 10); |
|
| 45 | - $preview->setStorageId(1); |
|
| 46 | - $preview->setWidth($i); |
|
| 47 | - $preview->setHeight($i); |
|
| 48 | - $preview->setMax(true); |
|
| 49 | - $preview->setSourceMimeType('image/jpeg'); |
|
| 50 | - $preview->setCropped(true); |
|
| 51 | - $preview->setEncrypted(false); |
|
| 52 | - $preview->setMimetype('image/jpeg'); |
|
| 53 | - $preview->setEtag('abc'); |
|
| 54 | - $preview->setMtime((new \DateTime())->getTimestamp()); |
|
| 55 | - $preview->setSize(0); |
|
| 56 | - $this->previewMapper->insert($preview); |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - $files = iterator_to_array($this->previewService->getAvailableFileIds()); |
|
| 60 | - $this->assertCount(1, $files); |
|
| 61 | - $this->assertCount(10, $files[0]['fileIds']); |
|
| 62 | - } |
|
| 23 | + private PreviewService $previewService; |
|
| 24 | + private PreviewMapper $previewMapper; |
|
| 25 | + private IGenerator $snowflakeGenerator; |
|
| 26 | + |
|
| 27 | + protected function setUp(): void { |
|
| 28 | + parent::setUp(); |
|
| 29 | + $this->previewService = Server::get(PreviewService::class); |
|
| 30 | + $this->previewMapper = Server::get(PreviewMapper::class); |
|
| 31 | + $this->snowflakeGenerator = Server::get(IGenerator::class); |
|
| 32 | + $this->previewService->deleteAll(); |
|
| 33 | + } |
|
| 34 | + |
|
| 35 | + public function tearDown(): void { |
|
| 36 | + $this->previewService->deleteAll(); |
|
| 37 | + parent::tearDown(); |
|
| 38 | + } |
|
| 39 | + |
|
| 40 | + public function testGetAvailableFileIds(): void { |
|
| 41 | + foreach (range(1, 20) as $i) { |
|
| 42 | + $preview = new Preview(); |
|
| 43 | + $preview->setId($this->snowflakeGenerator->nextId()); |
|
| 44 | + $preview->setFileId($i % 10); |
|
| 45 | + $preview->setStorageId(1); |
|
| 46 | + $preview->setWidth($i); |
|
| 47 | + $preview->setHeight($i); |
|
| 48 | + $preview->setMax(true); |
|
| 49 | + $preview->setSourceMimeType('image/jpeg'); |
|
| 50 | + $preview->setCropped(true); |
|
| 51 | + $preview->setEncrypted(false); |
|
| 52 | + $preview->setMimetype('image/jpeg'); |
|
| 53 | + $preview->setEtag('abc'); |
|
| 54 | + $preview->setMtime((new \DateTime())->getTimestamp()); |
|
| 55 | + $preview->setSize(0); |
|
| 56 | + $this->previewMapper->insert($preview); |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + $files = iterator_to_array($this->previewService->getAvailableFileIds()); |
|
| 60 | + $this->assertCount(1, $files); |
|
| 61 | + $this->assertCount(10, $files[0]['fileIds']); |
|
| 62 | + } |
|
| 63 | 63 | } |
@@ -19,82 +19,82 @@ |
||
| 19 | 19 | |
| 20 | 20 | #[\PHPUnit\Framework\Attributes\Group('DB')] |
| 21 | 21 | class PreviewMapperTest extends TestCase { |
| 22 | - private PreviewMapper $previewMapper; |
|
| 23 | - private IDBConnection $connection; |
|
| 24 | - private IGenerator $snowflake; |
|
| 25 | - |
|
| 26 | - public function setUp(): void { |
|
| 27 | - parent::setUp(); |
|
| 28 | - $this->previewMapper = Server::get(PreviewMapper::class); |
|
| 29 | - $this->connection = Server::get(IDBConnection::class); |
|
| 30 | - $this->snowflake = Server::get(IGenerator::class); |
|
| 31 | - |
|
| 32 | - $qb = $this->connection->getQueryBuilder(); |
|
| 33 | - $qb->delete('preview_locations')->executeStatement(); |
|
| 34 | - |
|
| 35 | - $qb = $this->connection->getQueryBuilder(); |
|
| 36 | - $qb->delete('preview_versions')->executeStatement(); |
|
| 37 | - |
|
| 38 | - $qb = $this->connection->getQueryBuilder(); |
|
| 39 | - $qb->delete('previews')->executeStatement(); |
|
| 40 | - } |
|
| 41 | - |
|
| 42 | - public function tearDown(): void { |
|
| 43 | - $this->previewMapper->deleteAll(); |
|
| 44 | - parent::tearDown(); |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - public function testGetAvailablePreviews(): void { |
|
| 48 | - // Empty |
|
| 49 | - $this->assertEquals([], $this->previewMapper->getAvailablePreviews([])); |
|
| 50 | - |
|
| 51 | - // No preview available |
|
| 52 | - $this->assertEquals([42 => []], $this->previewMapper->getAvailablePreviews([42])); |
|
| 53 | - |
|
| 54 | - $this->createPreviewForFileId(42); |
|
| 55 | - $previews = $this->previewMapper->getAvailablePreviews([42]); |
|
| 56 | - $this->assertNotEmpty($previews[42]); |
|
| 57 | - $this->assertNull($previews[42][0]->getLocationId()); |
|
| 58 | - $this->assertNull($previews[42][0]->getBucketName()); |
|
| 59 | - $this->assertNull($previews[42][0]->getObjectStoreName()); |
|
| 60 | - |
|
| 61 | - $this->createPreviewForFileId(43, 2); |
|
| 62 | - $previews = $this->previewMapper->getAvailablePreviews([43]); |
|
| 63 | - $this->assertNotEmpty($previews[43]); |
|
| 64 | - $this->assertEquals('preview-2', $previews[43][0]->getBucketName()); |
|
| 65 | - $this->assertEquals('default', $previews[43][0]->getObjectStoreName()); |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - private function createPreviewForFileId(int $fileId, ?int $bucket = null): void { |
|
| 69 | - $locationId = null; |
|
| 70 | - if ($bucket) { |
|
| 71 | - $qb = $this->connection->getQueryBuilder(); |
|
| 72 | - $locationId = $this->snowflake->nextId(); |
|
| 73 | - $qb->insert('preview_locations') |
|
| 74 | - ->values([ |
|
| 75 | - 'id' => $locationId, |
|
| 76 | - 'bucket_name' => $qb->createNamedParameter('preview-' . $bucket), |
|
| 77 | - 'object_store_name' => $qb->createNamedParameter('default'), |
|
| 78 | - ]); |
|
| 79 | - $qb->executeStatement(); |
|
| 80 | - } |
|
| 81 | - $preview = new Preview(); |
|
| 82 | - $preview->setId($this->snowflake->nextId()); |
|
| 83 | - $preview->setFileId($fileId); |
|
| 84 | - $preview->setStorageId(1); |
|
| 85 | - $preview->setCropped(true); |
|
| 86 | - $preview->setMax(true); |
|
| 87 | - $preview->setWidth(100); |
|
| 88 | - $preview->setSourceMimeType('image/jpeg'); |
|
| 89 | - $preview->setHeight(100); |
|
| 90 | - $preview->setSize(100); |
|
| 91 | - $preview->setMtime(time()); |
|
| 92 | - $preview->setMimetype('image/jpeg'); |
|
| 93 | - $preview->setEtag('abcdefg'); |
|
| 94 | - |
|
| 95 | - if ($locationId !== null) { |
|
| 96 | - $preview->setLocationId($locationId); |
|
| 97 | - } |
|
| 98 | - $this->previewMapper->insert($preview); |
|
| 99 | - } |
|
| 22 | + private PreviewMapper $previewMapper; |
|
| 23 | + private IDBConnection $connection; |
|
| 24 | + private IGenerator $snowflake; |
|
| 25 | + |
|
| 26 | + public function setUp(): void { |
|
| 27 | + parent::setUp(); |
|
| 28 | + $this->previewMapper = Server::get(PreviewMapper::class); |
|
| 29 | + $this->connection = Server::get(IDBConnection::class); |
|
| 30 | + $this->snowflake = Server::get(IGenerator::class); |
|
| 31 | + |
|
| 32 | + $qb = $this->connection->getQueryBuilder(); |
|
| 33 | + $qb->delete('preview_locations')->executeStatement(); |
|
| 34 | + |
|
| 35 | + $qb = $this->connection->getQueryBuilder(); |
|
| 36 | + $qb->delete('preview_versions')->executeStatement(); |
|
| 37 | + |
|
| 38 | + $qb = $this->connection->getQueryBuilder(); |
|
| 39 | + $qb->delete('previews')->executeStatement(); |
|
| 40 | + } |
|
| 41 | + |
|
| 42 | + public function tearDown(): void { |
|
| 43 | + $this->previewMapper->deleteAll(); |
|
| 44 | + parent::tearDown(); |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + public function testGetAvailablePreviews(): void { |
|
| 48 | + // Empty |
|
| 49 | + $this->assertEquals([], $this->previewMapper->getAvailablePreviews([])); |
|
| 50 | + |
|
| 51 | + // No preview available |
|
| 52 | + $this->assertEquals([42 => []], $this->previewMapper->getAvailablePreviews([42])); |
|
| 53 | + |
|
| 54 | + $this->createPreviewForFileId(42); |
|
| 55 | + $previews = $this->previewMapper->getAvailablePreviews([42]); |
|
| 56 | + $this->assertNotEmpty($previews[42]); |
|
| 57 | + $this->assertNull($previews[42][0]->getLocationId()); |
|
| 58 | + $this->assertNull($previews[42][0]->getBucketName()); |
|
| 59 | + $this->assertNull($previews[42][0]->getObjectStoreName()); |
|
| 60 | + |
|
| 61 | + $this->createPreviewForFileId(43, 2); |
|
| 62 | + $previews = $this->previewMapper->getAvailablePreviews([43]); |
|
| 63 | + $this->assertNotEmpty($previews[43]); |
|
| 64 | + $this->assertEquals('preview-2', $previews[43][0]->getBucketName()); |
|
| 65 | + $this->assertEquals('default', $previews[43][0]->getObjectStoreName()); |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + private function createPreviewForFileId(int $fileId, ?int $bucket = null): void { |
|
| 69 | + $locationId = null; |
|
| 70 | + if ($bucket) { |
|
| 71 | + $qb = $this->connection->getQueryBuilder(); |
|
| 72 | + $locationId = $this->snowflake->nextId(); |
|
| 73 | + $qb->insert('preview_locations') |
|
| 74 | + ->values([ |
|
| 75 | + 'id' => $locationId, |
|
| 76 | + 'bucket_name' => $qb->createNamedParameter('preview-' . $bucket), |
|
| 77 | + 'object_store_name' => $qb->createNamedParameter('default'), |
|
| 78 | + ]); |
|
| 79 | + $qb->executeStatement(); |
|
| 80 | + } |
|
| 81 | + $preview = new Preview(); |
|
| 82 | + $preview->setId($this->snowflake->nextId()); |
|
| 83 | + $preview->setFileId($fileId); |
|
| 84 | + $preview->setStorageId(1); |
|
| 85 | + $preview->setCropped(true); |
|
| 86 | + $preview->setMax(true); |
|
| 87 | + $preview->setWidth(100); |
|
| 88 | + $preview->setSourceMimeType('image/jpeg'); |
|
| 89 | + $preview->setHeight(100); |
|
| 90 | + $preview->setSize(100); |
|
| 91 | + $preview->setMtime(time()); |
|
| 92 | + $preview->setMimetype('image/jpeg'); |
|
| 93 | + $preview->setEtag('abcdefg'); |
|
| 94 | + |
|
| 95 | + if ($locationId !== null) { |
|
| 96 | + $preview->setLocationId($locationId); |
|
| 97 | + } |
|
| 98 | + $this->previewMapper->insert($preview); |
|
| 99 | + } |
|
| 100 | 100 | } |
@@ -13,94 +13,94 @@ |
||
| 13 | 13 | use PHPUnit\Framework\MockObject\Rule\AnyInvokedCount; |
| 14 | 14 | |
| 15 | 15 | trait ClientServiceTrait { |
| 16 | - /** @var IClientService|\PHPUnit\Framework\MockObject\MockObject */ |
|
| 17 | - private $clientService; |
|
| 18 | - /** @var IClient|\PHPUnit\Framework\MockObject\MockObject */ |
|
| 19 | - private $client; |
|
| 20 | - private $expectedGetRequests = []; |
|
| 21 | - private $expectedPostRequests = []; |
|
| 16 | + /** @var IClientService|\PHPUnit\Framework\MockObject\MockObject */ |
|
| 17 | + private $clientService; |
|
| 18 | + /** @var IClient|\PHPUnit\Framework\MockObject\MockObject */ |
|
| 19 | + private $client; |
|
| 20 | + private $expectedGetRequests = []; |
|
| 21 | + private $expectedPostRequests = []; |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * Wrapper to be forward compatible to phpunit 5.4+ |
|
| 25 | - * |
|
| 26 | - * @param string $originalClassName |
|
| 27 | - * @return \PHPUnit\Framework\MockObject\MockObject |
|
| 28 | - */ |
|
| 29 | - abstract protected function createMock(string $originalClassName); |
|
| 23 | + /** |
|
| 24 | + * Wrapper to be forward compatible to phpunit 5.4+ |
|
| 25 | + * |
|
| 26 | + * @param string $originalClassName |
|
| 27 | + * @return \PHPUnit\Framework\MockObject\MockObject |
|
| 28 | + */ |
|
| 29 | + abstract protected function createMock(string $originalClassName); |
|
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * Returns a matcher that matches when the method is executed |
|
| 33 | - * zero or more times. |
|
| 34 | - * |
|
| 35 | - * @since Method available since Release 3.0.0 |
|
| 36 | - * @since 33.0.0 - non static and hard typed return type due to phpunit upgrade to v11 |
|
| 37 | - */ |
|
| 38 | - abstract public function any(): AnyInvokedCount; |
|
| 31 | + /** |
|
| 32 | + * Returns a matcher that matches when the method is executed |
|
| 33 | + * zero or more times. |
|
| 34 | + * |
|
| 35 | + * @since Method available since Release 3.0.0 |
|
| 36 | + * @since 33.0.0 - non static and hard typed return type due to phpunit upgrade to v11 |
|
| 37 | + */ |
|
| 38 | + abstract public function any(): AnyInvokedCount; |
|
| 39 | 39 | |
| 40 | - protected function setUpClientServiceTrait() { |
|
| 41 | - $this->clientService = $this->createMock(IClientService::class); |
|
| 42 | - $this->client = $this->createMock(IClient::class); |
|
| 43 | - $this->clientService->expects($this->any()) |
|
| 44 | - ->method('newClient') |
|
| 45 | - ->willReturn($this->client); |
|
| 46 | - $this->client->expects($this->any()) |
|
| 47 | - ->method('get') |
|
| 48 | - ->willReturnCallback(function ($url) { |
|
| 49 | - if (!isset($this->expectedGetRequests[$url])) { |
|
| 50 | - throw new \Exception('unexpected request: ' . $url); |
|
| 51 | - } |
|
| 52 | - $result = $this->expectedGetRequests[$url]; |
|
| 40 | + protected function setUpClientServiceTrait() { |
|
| 41 | + $this->clientService = $this->createMock(IClientService::class); |
|
| 42 | + $this->client = $this->createMock(IClient::class); |
|
| 43 | + $this->clientService->expects($this->any()) |
|
| 44 | + ->method('newClient') |
|
| 45 | + ->willReturn($this->client); |
|
| 46 | + $this->client->expects($this->any()) |
|
| 47 | + ->method('get') |
|
| 48 | + ->willReturnCallback(function ($url) { |
|
| 49 | + if (!isset($this->expectedGetRequests[$url])) { |
|
| 50 | + throw new \Exception('unexpected request: ' . $url); |
|
| 51 | + } |
|
| 52 | + $result = $this->expectedGetRequests[$url]; |
|
| 53 | 53 | |
| 54 | - if ($result instanceof \Exception) { |
|
| 55 | - throw $result; |
|
| 56 | - } else { |
|
| 57 | - $response = $this->createMock(IResponse::class); |
|
| 58 | - $response->expects($this->any()) |
|
| 59 | - ->method('getBody') |
|
| 60 | - ->willReturn($result); |
|
| 61 | - return $response; |
|
| 62 | - } |
|
| 63 | - }); |
|
| 64 | - $this->client->expects($this->any()) |
|
| 65 | - ->method('post') |
|
| 66 | - ->willReturnCallback(function ($url) { |
|
| 67 | - if (!isset($this->expectedPostRequests[$url])) { |
|
| 68 | - throw new \Exception('unexpected request: ' . $url); |
|
| 69 | - } |
|
| 70 | - $result = $this->expectedPostRequests[$url]; |
|
| 54 | + if ($result instanceof \Exception) { |
|
| 55 | + throw $result; |
|
| 56 | + } else { |
|
| 57 | + $response = $this->createMock(IResponse::class); |
|
| 58 | + $response->expects($this->any()) |
|
| 59 | + ->method('getBody') |
|
| 60 | + ->willReturn($result); |
|
| 61 | + return $response; |
|
| 62 | + } |
|
| 63 | + }); |
|
| 64 | + $this->client->expects($this->any()) |
|
| 65 | + ->method('post') |
|
| 66 | + ->willReturnCallback(function ($url) { |
|
| 67 | + if (!isset($this->expectedPostRequests[$url])) { |
|
| 68 | + throw new \Exception('unexpected request: ' . $url); |
|
| 69 | + } |
|
| 70 | + $result = $this->expectedPostRequests[$url]; |
|
| 71 | 71 | |
| 72 | - if ($result instanceof \Exception) { |
|
| 73 | - throw $result; |
|
| 74 | - } else { |
|
| 75 | - $response = $this->createMock(IResponse::class); |
|
| 76 | - $response->expects($this->any()) |
|
| 77 | - ->method('getBody') |
|
| 78 | - ->willReturn($result); |
|
| 79 | - return $response; |
|
| 80 | - } |
|
| 81 | - }); |
|
| 82 | - } |
|
| 72 | + if ($result instanceof \Exception) { |
|
| 73 | + throw $result; |
|
| 74 | + } else { |
|
| 75 | + $response = $this->createMock(IResponse::class); |
|
| 76 | + $response->expects($this->any()) |
|
| 77 | + ->method('getBody') |
|
| 78 | + ->willReturn($result); |
|
| 79 | + return $response; |
|
| 80 | + } |
|
| 81 | + }); |
|
| 82 | + } |
|
| 83 | 83 | |
| 84 | - /** |
|
| 85 | - * @param string $url |
|
| 86 | - * @param string|\Exception $result |
|
| 87 | - */ |
|
| 88 | - protected function expectGetRequest($url, $result) { |
|
| 89 | - $this->expectedGetRequests[$url] = $result; |
|
| 90 | - } |
|
| 84 | + /** |
|
| 85 | + * @param string $url |
|
| 86 | + * @param string|\Exception $result |
|
| 87 | + */ |
|
| 88 | + protected function expectGetRequest($url, $result) { |
|
| 89 | + $this->expectedGetRequests[$url] = $result; |
|
| 90 | + } |
|
| 91 | 91 | |
| 92 | - /** |
|
| 93 | - * @param string $url |
|
| 94 | - * @param string|\Exception $result |
|
| 95 | - */ |
|
| 96 | - protected function expectPostRequest($url, $result) { |
|
| 97 | - $this->expectedPostRequests[$url] = $result; |
|
| 98 | - } |
|
| 92 | + /** |
|
| 93 | + * @param string $url |
|
| 94 | + * @param string|\Exception $result |
|
| 95 | + */ |
|
| 96 | + protected function expectPostRequest($url, $result) { |
|
| 97 | + $this->expectedPostRequests[$url] = $result; |
|
| 98 | + } |
|
| 99 | 99 | |
| 100 | - /** |
|
| 101 | - * @return IClientService|\PHPUnit\Framework\MockObject\MockObject |
|
| 102 | - */ |
|
| 103 | - protected function getClientService() { |
|
| 104 | - return $this->clientService; |
|
| 105 | - } |
|
| 100 | + /** |
|
| 101 | + * @return IClientService|\PHPUnit\Framework\MockObject\MockObject |
|
| 102 | + */ |
|
| 103 | + protected function getClientService() { |
|
| 104 | + return $this->clientService; |
|
| 105 | + } |
|
| 106 | 106 | } |
@@ -22,44 +22,44 @@ |
||
| 22 | 22 | * @package Test\Avatar |
| 23 | 23 | */ |
| 24 | 24 | class GuestAvatarTest extends TestCase { |
| 25 | - /** |
|
| 26 | - * @var GuestAvatar |
|
| 27 | - */ |
|
| 28 | - private $guestAvatar; |
|
| 25 | + /** |
|
| 26 | + * @var GuestAvatar |
|
| 27 | + */ |
|
| 28 | + private $guestAvatar; |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * Setups a guest avatar instance for tests. |
|
| 32 | - */ |
|
| 33 | - #[\PHPUnit\Framework\Attributes\Before()] |
|
| 34 | - public function setupGuestAvatar(): void { |
|
| 35 | - /* @var MockObject|LoggerInterface $logger */ |
|
| 36 | - $logger = $this->createMock(LoggerInterface::class); |
|
| 37 | - $config = $this->createMock(IConfig::class); |
|
| 38 | - $this->guestAvatar = new GuestAvatar('einstein', $config, $logger); |
|
| 39 | - } |
|
| 30 | + /** |
|
| 31 | + * Setups a guest avatar instance for tests. |
|
| 32 | + */ |
|
| 33 | + #[\PHPUnit\Framework\Attributes\Before()] |
|
| 34 | + public function setupGuestAvatar(): void { |
|
| 35 | + /* @var MockObject|LoggerInterface $logger */ |
|
| 36 | + $logger = $this->createMock(LoggerInterface::class); |
|
| 37 | + $config = $this->createMock(IConfig::class); |
|
| 38 | + $this->guestAvatar = new GuestAvatar('einstein', $config, $logger); |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * Asserts that testGet() returns the expected avatar. |
|
| 43 | - * |
|
| 44 | - * For the test a static name "einstein" is used and |
|
| 45 | - * the generated image is compared with an expected one. |
|
| 46 | - */ |
|
| 47 | - public function testGet(): void { |
|
| 48 | - $this->markTestSkipped('TODO: Disable because fails on drone'); |
|
| 49 | - $avatar = $this->guestAvatar->getFile(32); |
|
| 50 | - self::assertInstanceOf(InMemoryFile::class, $avatar); |
|
| 51 | - $expectedFile = file_get_contents( |
|
| 52 | - __DIR__ . '/../../data/guest_avatar_einstein_32.png' |
|
| 53 | - ); |
|
| 54 | - self::assertEquals(trim($expectedFile), trim($avatar->getContent())); |
|
| 55 | - } |
|
| 41 | + /** |
|
| 42 | + * Asserts that testGet() returns the expected avatar. |
|
| 43 | + * |
|
| 44 | + * For the test a static name "einstein" is used and |
|
| 45 | + * the generated image is compared with an expected one. |
|
| 46 | + */ |
|
| 47 | + public function testGet(): void { |
|
| 48 | + $this->markTestSkipped('TODO: Disable because fails on drone'); |
|
| 49 | + $avatar = $this->guestAvatar->getFile(32); |
|
| 50 | + self::assertInstanceOf(InMemoryFile::class, $avatar); |
|
| 51 | + $expectedFile = file_get_contents( |
|
| 52 | + __DIR__ . '/../../data/guest_avatar_einstein_32.png' |
|
| 53 | + ); |
|
| 54 | + self::assertEquals(trim($expectedFile), trim($avatar->getContent())); |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | - /** |
|
| 58 | - * Asserts that "testIsCustomAvatar" returns false for guests. |
|
| 59 | - * |
|
| 60 | - * @return void |
|
| 61 | - */ |
|
| 62 | - public function testIsCustomAvatar(): void { |
|
| 63 | - self::assertFalse($this->guestAvatar->isCustomAvatar()); |
|
| 64 | - } |
|
| 57 | + /** |
|
| 58 | + * Asserts that "testIsCustomAvatar" returns false for guests. |
|
| 59 | + * |
|
| 60 | + * @return void |
|
| 61 | + */ |
|
| 62 | + public function testIsCustomAvatar(): void { |
|
| 63 | + self::assertFalse($this->guestAvatar->isCustomAvatar()); |
|
| 64 | + } |
|
| 65 | 65 | } |