@@ -27,16 +27,15 @@ class SessionStatisticsTest extends TestCase {
2727 private ITimeFactory &MockObject $ timeFactory ;
2828 private IDBConnection $ connection ;
2929 private SessionStatistics $ instance ;
30- private const TABLE = 'authtoken ' ;
30+ private int $ currentTime ;
31+ private const TABLE = 'preferences ' ;
3132 private const OFFSET_5MIN = 300 ;
3233 private const OFFSET_1HOUR = 3600 ;
3334 private const OFFSET_1DAY = 86400 ;
34- private const OFFSET_7DAYS = 604800 ;
3535 private const OFFSET_1MONTH = 2592000 ;
3636 private const OFFSET_3MONTHS = 7776000 ;
3737 private const OFFSET_6MONTHS = 15552000 ;
3838 private const OFFSET_1YEAR = 31536000 ;
39- private const CURRENT_TIME = 100000000 ;
4039
4140
4241 protected function setUp (): void {
@@ -47,54 +46,74 @@ protected function setUp(): void {
4746 $ this ->connection = Server::get (IDBConnection::class);
4847
4948 $ this ->instance = new SessionStatistics ($ this ->connection , $ this ->timeFactory );
49+
50+ // when running the tests locally, you may have other lastLogin values in the database.
51+ // using a timestamp in the future to workaround.
52+ $ this ->currentTime = time () + (400 * 24 * 60 * 60 );
53+
54+ $ this ->removeDummyValues ();
55+ $ this ->addDummyValues ();
56+ }
57+
58+ protected function tearDown (): void {
59+ $ this ->removeDummyValues ();
60+ }
61+
62+ protected function removeDummyValues (): void {
63+ $ qb = $ this ->connection ->getQueryBuilder ();
64+ $ qb ->delete ('preferences ' )
65+ ->where ($ qb ->expr ()->eq ('appid ' , $ qb ->createNamedParameter ('login ' )))
66+ ->andWhere ($ qb ->expr ()->eq ('configkey ' , $ qb ->createNamedParameter ('lastLogin ' )))
67+ ->andWhere ($ qb ->expr ()->like ('userid ' , $ qb ->createNamedParameter ('session-statistics-test% ' )));
68+ $ qb ->executeStatement ();
5069 }
5170
5271 private function addDummyValues (): void {
53- $ this ->addDummyValuesWithLastLogin (self ::CURRENT_TIME - self ::OFFSET_5MIN + 1 , 10 );
54- $ this ->addDummyValuesWithLastLogin (self ::CURRENT_TIME - self ::OFFSET_1HOUR + 1 , 20 );
55- $ this ->addDummyValuesWithLastLogin (self ::CURRENT_TIME - self ::OFFSET_1DAY + 1 , 30 );
56- $ this ->addDummyValuesWithLastLogin (self ::CURRENT_TIME - self ::OFFSET_7DAYS + 1 , 40 );
57- $ this ->addDummyValuesWithLastLogin (self ::CURRENT_TIME - self ::OFFSET_1MONTH + 1 , 50 );
58- $ this ->addDummyValuesWithLastLogin (self ::CURRENT_TIME - self ::OFFSET_3MONTHS + 1 , 60 );
59- $ this ->addDummyValuesWithLastLogin (self ::CURRENT_TIME - self ::OFFSET_6MONTHS + 1 , 70 );
60- $ this ->addDummyValuesWithLastLogin (self ::CURRENT_TIME - self ::OFFSET_1YEAR + 1 , 80 );
72+ $ this ->addDummyValuesWithLastLogin (self ::OFFSET_5MIN , 10 );
73+ $ this ->addDummyValuesWithLastLogin (self ::OFFSET_5MIN , 11 );
74+ $ this ->addDummyValuesWithLastLogin (self ::OFFSET_1HOUR , 20 );
75+ $ this ->addDummyValuesWithLastLogin (self ::OFFSET_1HOUR , 21 );
76+ $ this ->addDummyValuesWithLastLogin (self ::OFFSET_1HOUR , 22 );
77+ $ this ->addDummyValuesWithLastLogin (self ::OFFSET_1DAY , 30 );
78+ $ this ->addDummyValuesWithLastLogin (self ::OFFSET_1MONTH , 50 );
79+ $ this ->addDummyValuesWithLastLogin (self ::OFFSET_3MONTHS , 60 );
80+ $ this ->addDummyValuesWithLastLogin (self ::OFFSET_6MONTHS , 70 );
81+ $ this ->addDummyValuesWithLastLogin (self ::OFFSET_1YEAR , 80 );
82+ $ this ->addDummyValuesWithLastLogin (self ::OFFSET_1YEAR , 81 );
83+ $ this ->addDummyValuesWithLastLogin (self ::OFFSET_1YEAR , 82 );
6184 }
6285
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- }
86+ private function addDummyValuesWithLastLogin (int $ offset , int $ id ): void {
87+ $ query = $ this ->connection ->getQueryBuilder ();
88+ $ query ->insert (self ::TABLE )
89+ ->values (
90+ [
91+ 'userid ' => $ query ->createNamedParameter ("session-statistics-test $ id " ),
92+ 'appid ' => $ query ->createNamedParameter ('login ' ),
93+ 'configkey ' => $ query ->createNamedParameter ('lastLogin ' ),
94+ 'configvalue ' => $ query ->createNamedParameter ((string )($ this ->currentTime - $ offset + 1 )),
95+ 'lazy ' => $ query ->createNamedParameter (0 ),
96+ 'type ' => $ query ->createNamedParameter (0 ),
97+ 'flags ' => $ query ->createNamedParameter (0 ),
98+ ]
99+ );
100+ $ query ->executeStatement ();
81101 }
82102
83103 public function testGetSessionStatistics () {
84- $ this ->addDummyValues ();
85104 $ this ->timeFactory ->expects ($ this ->any ())->method ('getTime ' )
86- ->willReturn (self :: CURRENT_TIME );
105+ ->willReturn ($ this -> currentTime );
87106
88107 $ result = $ this ->instance ->getSessionStatistics ();
89108
90109 $ 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 ' ]);
110+ $ this ->assertSame (2 , $ result ['last5minutes ' ]); // 2 users in last 5 minutes
111+ $ this ->assertSame (5 , $ result ['last1hour ' ]); // 2 + 3 users in last hour
112+ $ this ->assertSame (6 , $ result ['last24hours ' ]); // 2 + 3 + 1 users in last day
113+ $ this ->assertSame (6 , $ result ['last7days ' ]); // 2 + 3 + 1 + 0 users in last week
114+ $ this ->assertSame (7 , $ result ['last1month ' ]); // 2 + 3 + 1 + 0 + 1 users in last month
115+ $ this ->assertSame (8 , $ result ['last3months ' ]); // 2 + 3 + 1 + 0 + 1 + 1 users in last 3 months
116+ $ this ->assertSame (9 , $ result ['last6months ' ]); // 2 + 3 + 1 + 0 + 1 + 1 + 1 users in last 6 months
117+ $ this ->assertSame (12 , $ result ['lastyear ' ]); // 2 + 3 + 1 + 0 + 1 + 1 + 1 + 3 users in last year
99118 }
100119}
0 commit comments