1111
1212use OCA \ServerInfo \LoginStats ;
1313use OCP \DB \QueryBuilder \IQueryBuilder ;
14+ use OCP \IConfig ;
1415use OCP \IDBConnection ;
1516use OCP \Server ;
17+ use PHPUnit \Framework \MockObject \MockObject ;
1618use Test \TestCase ;
1719
1820/**
1921 * @group DB
2022 */
2123class LoginStatsTest extends TestCase {
2224 private IDBConnection $ db ;
25+ private IConfig &MockObject $ config ;
2326 private LoginStats $ instance ;
2427
2528 private const IP_A = '10.0.0.1 ' ;
@@ -29,7 +32,10 @@ class LoginStatsTest extends TestCase {
2932 protected function setUp (): void {
3033 parent ::setUp ();
3134 $ this ->db = Server::get (IDBConnection::class);
32- $ this ->instance = new LoginStats ($ this ->db );
35+ $ this ->config = $ this ->createMock (IConfig::class);
36+ $ this ->config ->method ('getSystemValueBool ' )->willReturn (false );
37+ $ this ->config ->method ('getSystemValueString ' )->willReturn ('' );
38+ $ this ->instance = new LoginStats ($ this ->config , $ this ->db );
3339 $ this ->cleanUp ();
3440 }
3541
@@ -61,6 +67,29 @@ private function insertAttempt(string $ip, int $occurred): void {
6167 $ qb ->executeStatement ();
6268 }
6369
70+ public function testRedisBackendReturnsUnavailable (): void {
71+ $ config = $ this ->createMock (IConfig::class);
72+ $ config ->method ('getSystemValueBool ' )->willReturn (false );
73+ $ config ->method ('getSystemValueString ' )->willReturn ('OC\Memcache\Redis ' );
74+ $ instance = new LoginStats ($ config , $ this ->db );
75+
76+ $ result = $ instance ->getStats ();
77+
78+ $ this ->assertFalse ($ result ['available ' ]);
79+ $ this ->assertSame ('redis_backend ' , $ result ['reason ' ]);
80+ }
81+
82+ public function testForceDatabaseOverridesRedis (): void {
83+ $ config = $ this ->createMock (IConfig::class);
84+ $ config ->method ('getSystemValueBool ' )->willReturn (true );
85+ $ config ->method ('getSystemValueString ' )->willReturn ('OC\Memcache\Redis ' );
86+ $ instance = new LoginStats ($ config , $ this ->db );
87+
88+ $ result = $ instance ->getStats ();
89+
90+ $ this ->assertTrue ($ result ['available ' ]);
91+ }
92+
6493 public function testReturnShape (): void {
6594 $ result = $ this ->instance ->getStats ();
6695
@@ -114,6 +143,7 @@ public function testTopIpsShape(): void {
114143
115144 $ result = $ this ->instance ->getStats ();
116145
146+ $ this ->assertIsArray ($ result ['topIps ' ]);
117147 foreach ($ result ['topIps ' ] as $ entry ) {
118148 $ this ->assertArrayHasKey ('ip ' , $ entry );
119149 $ this ->assertArrayHasKey ('count ' , $ entry );
0 commit comments