From 070680d5bbb13dab4207492cce50546d736a0989 Mon Sep 17 00:00:00 2001 From: Joe Maller Date: Fri, 6 Jan 2023 16:18:47 -0800 Subject: [PATCH] LastLogin admin column and tests --- README.md | 2 +- src/ThemeInit/Admin/LastLogin.php | 20 ++++++++++- tests/LastLoginTest.php | 56 +++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 277902f..47607af 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,7 @@ A common baseline of repeated functions, filters and actions used across our Wor Adds a Template column to Pages admin and a summary table to the Appearance menu showing which templates have been assigned to pages. - **Record Users' Last Login time** - Timestamps of successful logins are stored in user_meta and displayed in the WordPress Admin dashboard User listings for administrators. + User's last successful login are recorded and added to the WordPress Admin User table. - **Enable and limit WP_POST_REVISIONS** Revisions are set to 6, this overrides any constants set in wp-config.php. diff --git a/src/ThemeInit/Admin/LastLogin.php b/src/ThemeInit/Admin/LastLogin.php index 767134d..b4ce1d8 100644 --- a/src/ThemeInit/Admin/LastLogin.php +++ b/src/ThemeInit/Admin/LastLogin.php @@ -1,8 +1,15 @@ setTimezone($timezone); + $date_format = get_option('date_format'); + $time_format = get_option('time_format'); + $date = sprintf( + '%s at %s', + $dateTime->format($date_format), + $dateTime->format($time_format) + ); + return sprintf('%s ago', $date, human_time_diff($last)); } return $output; diff --git a/tests/LastLoginTest.php b/tests/LastLoginTest.php index fad4f9f..840d573 100644 --- a/tests/LastLoginTest.php +++ b/tests/LastLoginTest.php @@ -40,4 +40,60 @@ public function testLogLastLogin() $this->assertEquals($user_meta[$User->ID][0]['meta_key'], 'last_login'); $this->assertIsInt($user_meta[$User->ID][0]['meta_value']); } + + public function testAddColumn() + { + $LastLogin = new Admin\LastLogin(); + $cols = ['cb' => 'checkbox', 'posts' => 'Posts']; + $actual = $LastLogin->add_column($cols); + + $this->assertArrayHasKey('last-login', $actual); + $this->assertArrayHasKey('posts', $actual); + $this->assertArrayHasKey('cb', $actual); + } + + public function testColumnContent() + { + global $user_meta, $options, $human_time_diff; + $options['date_format'] = 'F j, Y'; + $options['time_format'] = 'H:i:s'; + + $human_time_diff = '37 mins'; + + $timezone = new \DateTimeZone('America/New_York'); + $dateTime = \DateTimeImmutable::createFromFormat( + 'Y-m-d H:i:s', + '2023-01-25 23:45:56', + $timezone + ); + $user_meta = $dateTime->format('U'); + + $LastLogin = new Admin\LastLogin(); + $actual = $LastLogin->column_content('output', 'last-login', 123); + + $this->assertStringContainsString('assertStringContainsString('37 mins ago', $actual); + $this->assertStringContainsString('2023', $actual); + $this->assertStringContainsString('January 25, 2023 at 23:45:56', $actual); + $this->assertStringContainsString('45', $actual); + + $expected = 'output'; + $actual = $LastLogin->column_content($expected, 'not-a-column', 123); + $this->assertEquals('output', $actual); + + $user_meta = 0; + $actual = $LastLogin->column_content('output', 'last-login', 123); + $this->assertEquals('--', $actual); + } + + public function testAdminStyles() + { + global $inline_styles; + $LastLogin = new Admin\LastLogin(); + + $LastLogin->admin_styles(); + $actual = end($inline_styles); + $this->assertArrayHasKey('handle', $actual); + $this->assertStringContainsString('.column-last-login', $actual['data']); + } }