@@ -27,11 +27,10 @@ class SessionStatisticsTest extends TestCase {
2727 private ITimeFactory &MockObject $ timeFactory ;
2828 private IDBConnection $ connection ;
2929 private SessionStatistics $ instance ;
30- private const TABLE = 'authtoken ' ;
30+ private const TABLE = 'preferences ' ;
3131 private const OFFSET_5MIN = 300 ;
3232 private const OFFSET_1HOUR = 3600 ;
3333 private const OFFSET_1DAY = 86400 ;
34- private const OFFSET_7DAYS = 604800 ;
3534 private const OFFSET_1MONTH = 2592000 ;
3635 private const OFFSET_3MONTHS = 7776000 ;
3736 private const OFFSET_6MONTHS = 15552000 ;
@@ -47,54 +46,70 @@ protected function setUp(): void {
4746 $ this ->connection = Server::get (IDBConnection::class);
4847
4948 $ this ->instance = new SessionStatistics ($ this ->connection , $ this ->timeFactory );
49+
50+ $ this ->removeDummyValues ();
51+ $ this ->addDummyValues ();
52+ }
53+
54+ protected function tearDown (): void {
55+ $ this ->removeDummyValues ();
56+ }
57+
58+ protected function removeDummyValues (): void {
59+ $ qb = $ this ->connection ->getQueryBuilder ();
60+ $ qb ->delete ('preferences ' )
61+ ->where ($ qb ->expr ()->eq ('appid ' , $ qb ->createNamedParameter ('login ' )))
62+ ->andWhere ($ qb ->expr ()->eq ('configkey ' , $ qb ->createNamedParameter ('lastLogin ' )))
63+ ->andWhere ($ qb ->expr ()->like ('userid ' , $ qb ->createNamedParameter ('session-statistics-test% ' )));
64+ $ qb ->executeStatement ();
5065 }
5166
5267 private function addDummyValues (): void {
5368 $ this ->addDummyValuesWithLastLogin (self ::CURRENT_TIME - self ::OFFSET_5MIN + 1 , 10 );
69+ $ this ->addDummyValuesWithLastLogin (self ::CURRENT_TIME - self ::OFFSET_5MIN + 1 , 11 );
5470 $ this ->addDummyValuesWithLastLogin (self ::CURRENT_TIME - self ::OFFSET_1HOUR + 1 , 20 );
71+ $ this ->addDummyValuesWithLastLogin (self ::CURRENT_TIME - self ::OFFSET_1HOUR + 1 , 21 );
72+ $ this ->addDummyValuesWithLastLogin (self ::CURRENT_TIME - self ::OFFSET_1HOUR + 1 , 22 );
5573 $ this ->addDummyValuesWithLastLogin (self ::CURRENT_TIME - self ::OFFSET_1DAY + 1 , 30 );
56- $ this ->addDummyValuesWithLastLogin (self ::CURRENT_TIME - self ::OFFSET_7DAYS + 1 , 40 );
5774 $ this ->addDummyValuesWithLastLogin (self ::CURRENT_TIME - self ::OFFSET_1MONTH + 1 , 50 );
5875 $ this ->addDummyValuesWithLastLogin (self ::CURRENT_TIME - self ::OFFSET_3MONTHS + 1 , 60 );
5976 $ this ->addDummyValuesWithLastLogin (self ::CURRENT_TIME - self ::OFFSET_6MONTHS + 1 , 70 );
6077 $ this ->addDummyValuesWithLastLogin (self ::CURRENT_TIME - self ::OFFSET_1YEAR + 1 , 80 );
78+ $ this ->addDummyValuesWithLastLogin (self ::CURRENT_TIME - self ::OFFSET_1YEAR + 1 , 81 );
79+ $ this ->addDummyValuesWithLastLogin (self ::CURRENT_TIME - self ::OFFSET_1YEAR + 1 , 82 );
6180 }
6281
63- private function addDummyValuesWithLastLogin ($ lastActivity , $ numOfEntries ): void {
64- for ($ i = 0 ; $ i < $ numOfEntries ; $ i ++) {
65- $ query = $ this ->connection ->getQueryBuilder ();
66- $ query ->insert (self ::TABLE )
67- ->values (
68- [
69- 'uid ' => $ query ->createNamedParameter ('user- ' . ($ numOfEntries + $ i % 2 )),
70- 'login_name ' => $ query ->createNamedParameter ('user- ' . ($ numOfEntries + $ i % 2 )),
71- 'password ' => $ query ->createNamedParameter ('password ' ),
72- 'name ' => $ query ->createNamedParameter ('user agent ' ),
73- 'token ' => $ query ->createNamedParameter ('token- ' . $ this ->getUniqueID ()),
74- 'type ' => $ query ->createNamedParameter (0 ),
75- 'last_activity ' => $ query ->createNamedParameter ($ lastActivity ),
76- 'last_check ' => $ query ->createNamedParameter ($ lastActivity ),
77- ]
78- );
79- $ query ->executeStatement ();
80- }
82+ private function addDummyValuesWithLastLogin ($ lastActivity , $ id ): void {
83+ $ query = $ this ->connection ->getQueryBuilder ();
84+ $ query ->insert (self ::TABLE )
85+ ->values (
86+ [
87+ 'userid ' => $ query ->createNamedParameter ("session-statistics-test $ id " ),
88+ 'appid ' => $ query ->createNamedParameter ('login ' ),
89+ 'configkey ' => $ query ->createNamedParameter ('lastLogin ' ),
90+ 'configvalue ' => $ query ->createNamedParameter ((string )$ lastActivity ),
91+ 'lazy ' => $ query ->createNamedParameter (0 ),
92+ 'type ' => $ query ->createNamedParameter (0 ),
93+ 'flags ' => $ query ->createNamedParameter (0 ),
94+ ]
95+ );
96+ $ query ->executeStatement ();
8197 }
8298
8399 public function testGetSessionStatistics () {
84- $ this ->addDummyValues ();
85100 $ this ->timeFactory ->expects ($ this ->any ())->method ('getTime ' )
86101 ->willReturn (self ::CURRENT_TIME );
87102
88103 $ result = $ this ->instance ->getSessionStatistics ();
89104
90105 $ this ->assertSame (8 , count ($ result ));
91- $ this ->assertSame (2 , $ result ['last5minutes ' ]);
92- $ this ->assertSame (4 , $ result ['last1hour ' ]);
93- $ this ->assertSame (6 , $ result ['last24hours ' ]);
94- $ this ->assertSame (8 , $ result ['last7days ' ]);
95- $ this ->assertSame (10 , $ result ['last1month ' ]);
96- $ this ->assertSame (12 , $ result ['last3months ' ]);
97- $ this ->assertSame (14 , $ result ['last6months ' ]);
98- $ this ->assertSame (16 , $ result ['lastyear ' ]);
106+ $ this ->assertSame (2 , $ result ['last5minutes ' ]); // 2 users in last 5 minutes
107+ $ this ->assertSame (5 , $ result ['last1hour ' ]); // 2 + 3 users in last hour
108+ $ this ->assertSame (6 , $ result ['last24hours ' ]); // 2 + 3 + 1 users in last day
109+ $ this ->assertSame (6 , $ result ['last7days ' ]); // 2 + 3 + 1 + 0 users in last week
110+ $ this ->assertSame (7 , $ result ['last1month ' ]); // 2 + 3 + 1 + 0 + 1 users in last month
111+ $ this ->assertSame (8 , $ result ['last3months ' ]); // 2 + 3 + 1 + 0 + 1 + 1 users in last 3 months
112+ $ this ->assertSame (9 , $ result ['last6months ' ]); // 2 + 3 + 1 + 0 + 1 + 1 + 1 users in last 6 months
113+ $ this ->assertSame (12 , $ result ['lastyear ' ]); // 2 + 3 + 1 + 0 + 1 + 1 + 1 + 3 users in last year
99114 }
100115}
0 commit comments