Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
R
Redis User Session Storage
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
WP Plugins
Redis User Session Storage
Commits
a524b090
Commit
a524b090
authored
3 years ago
by
Erick Hitter
Browse files
Options
Downloads
Patches
Plain Diff
Test coverage
parent
f147e03d
Branches
Branches containing commit
Tags
Tags containing commit
1 merge request
!6
Add tests
Pipeline
#5043
failed
3 years ago
Stage: test
Stage: deploy
Changes
3
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
inc/class-plugin.php
+1
-33
1 addition, 33 deletions
inc/class-plugin.php
phpunit.xml
+1
-1
1 addition, 1 deletion
phpunit.xml
tests/inc/class-test-plugin.php
+166
-6
166 additions, 6 deletions
tests/inc/class-test-plugin.php
with
168 additions
and
40 deletions
inc/class-plugin.php
+
1
−
33
View file @
a524b090
...
...
@@ -104,9 +104,6 @@ class Plugin extends WP_Session_Tokens {
/**
* Get all sessions of a user.
*
* @since 0.1
* @access protected
*
* @return array Sessions of a user.
*/
protected
function
get_sessions
()
{
...
...
@@ -146,9 +143,6 @@ class Plugin extends WP_Session_Tokens {
/**
* Retrieve a session by its verifier (token hash).
*
* @since 0.1
* @access protected
*
* @param string $verifier Verifier of the session to retrieve.
* @return array|null The session, or null if it does not exist
*/
...
...
@@ -165,9 +159,6 @@ class Plugin extends WP_Session_Tokens {
/**
* Update a session by its verifier.
*
* @since 0.1
* @access protected
*
* @param string $verifier Verifier of the session to update.
* @param array $session Optional. Session. Omitting this argument destroys the session.
*/
...
...
@@ -186,9 +177,6 @@ class Plugin extends WP_Session_Tokens {
/**
* Update a user's sessions in Redis.
*
* @since 0.1
* @access protected
*
* @param array $sessions Sessions.
*/
protected
function
update_sessions
(
$sessions
)
{
...
...
@@ -196,10 +184,6 @@ class Plugin extends WP_Session_Tokens {
return
;
}
if
(
!
has_filter
(
'attach_session_information'
)
)
{
$sessions
=
wp_list_pluck
(
$sessions
,
'expiration'
);
}
$key
=
$this
->
get_key
();
if
(
$sessions
)
{
...
...
@@ -212,9 +196,6 @@ class Plugin extends WP_Session_Tokens {
/**
* Destroy all session tokens for a user, except a single session passed.
*
* @since 0.1
* @access protected
*
* @param string $verifier Verifier of the session to keep.
*/
protected
function
destroy_other_sessions
(
$verifier
)
{
...
...
@@ -224,9 +205,6 @@ class Plugin extends WP_Session_Tokens {
/**
* Destroy all session tokens for a user.
*
* @since 0.1
* @access protected
*/
protected
function
destroy_all_sessions
()
{
$this
->
update_sessions
(
array
()
);
...
...
@@ -235,10 +213,6 @@ class Plugin extends WP_Session_Tokens {
/**
* Destroy all session tokens for all users.
*
* @since 0.1
* @access public
* @static
*
* @return bool
*/
public
static
function
drop_sessions
()
{
...
...
@@ -248,9 +222,6 @@ class Plugin extends WP_Session_Tokens {
/**
* Empty database, clearing all tokens.
*
* @since 0.2
* @access protected
*
* @return bool
*/
protected
function
flush_redis_db
()
{
...
...
@@ -258,10 +229,7 @@ class Plugin extends WP_Session_Tokens {
}
/**
* Build key for current user
*
* @since 0.1
* @access protected
* Build key for current user.
*
* @return string
*/
...
...
This diff is collapsed.
Click to expand it.
phpunit.xml
+
1
−
1
View file @
a524b090
...
...
@@ -8,7 +8,7 @@
convertWarningsToExceptions=
"true"
>
<testsuites>
<testsuite>
<testsuite
name=
"wpruss"
>
<directory
suffix=
".php"
>
./tests/
</directory>
<exclude>
./tests/bootstrap.php
</exclude>
</testsuite>
...
...
This diff is collapsed.
Click to expand it.
tests/inc/class-test-plugin.php
+
166
−
6
View file @
a524b090
...
...
@@ -9,6 +9,7 @@ namespace Redis_User_Session_Storage\Tests\Inc;
use
Redis
;
use
Redis_User_Session_Storage
\Plugin
;
use
ReflectionClass
;
use
WP_Session_Tokens
;
use
WP_UnitTestCase
;
...
...
@@ -18,6 +19,16 @@ use WP_UnitTestCase;
* @coversDefaultClass \Redis_User_Session_Storage\Plugin
*/
class
Test_Plugin
extends
WP_UnitTestCase
{
/**
* Clear stored sessions after each test, as factory can create user with
* same ID as previous test.
*/
public
function
tear_down
()
{
parent
::
tear_down
();
$this
->
_invoke_method
(
0
,
'flush_redis_db'
);
}
/**
* Test construction.
*
...
...
@@ -25,14 +36,163 @@ class Test_Plugin extends WP_UnitTestCase {
* @return void
*/
public
function
test__construct
()
{
$user_id
=
$this
->
factory
->
user
->
create
();
$this
->
plugin
=
new
Plugin
(
$user_id
);
$user_id
=
$this
->
factory
->
user
->
create
();
$object
=
new
Plugin
(
$user_id
);
$this
->
assertInstanceOf
(
WP_Session_Tokens
::
class
,
$object
,
'Failed to assert that plugin class is an instance of `WP_Session_Tokens`.'
);
$this
->
assertEquals
(
'wpruss'
,
$object
->
prefix
);
$this
->
assertTrue
(
$this
->
_get_property
(
$user_id
,
'redis_connected'
),
'Failed to assert that Redis is connected.'
);
$this
->
assertInstanceOf
(
Redis
::
class
,
$this
->
_get_property
(
$user_id
,
'redis'
),
'Failed to assert that Redis client is an instance of `Redis`.'
);
}
/**
* Test `get_sessions()` method.
*
* @covers ::get_sessions()
* @return void
*/
public
function
test_get_sessions
()
{
$user_id
=
$this
->
factory
->
user
->
create
();
$plugin
=
new
Plugin
(
$user_id
);
$this
->
assertEmpty
(
$this
->
_invoke_method
(
$user_id
,
'get_sessions'
),
'Failed to assert that no sessions are returned before user logs in.'
);
$plugin
->
create
(
time
()
+
60
);
$this
->
assertNotEmpty
(
$this
->
_invoke_method
(
$user_id
,
'get_sessions'
),
'Failed to assert that session token is stored in Redis.'
);
}
/**
* Test `get_session()` method.
*
* @covers ::get_session()
* @return void
*/
public
function
test_get_session
()
{
$user_id
=
$this
->
factory
->
user
->
create
();
$plugin
=
new
Plugin
(
$user_id
);
$this
->
assertEmpty
(
$this
->
_invoke_method
(
$user_id
,
'get_session'
,
array
(
'abcdef0123456789'
,
)
),
'Failed to assert that arbitrary verifier does not return a session.'
);
$expiration
=
time
()
+
60
;
$plugin
->
create
(
$expiration
);
$tokens
=
$this
->
_invoke_method
(
$user_id
,
'get_sessions'
);
$verifier
=
array_keys
(
$tokens
)[
0
];
$session_data
=
$this
->
_invoke_method
(
$user_id
,
'get_session'
,
array
(
$verifier
,
)
);
$this
->
assertEquals
(
$session_data
[
'expiration'
],
$expiration
,
'Failed to assert that session expiration is stored in Redis.'
);
}
/**
* Test `prepare_session()` method.
*
* @covers ::prepare_session()
* @return void
*/
public
function
test_prepare_session
()
{
$this
->
assertEquals
(
array
(
'expiration'
=>
1
,
),
$this
->
_invoke_method
(
0
,
'prepare_session'
,
array
(
1
,
)
),
'Failed to assert that session data is transformed as expected.'
);
$this
->
assertTrue
(
class_exists
(
Redis
::
class
,
false
)
);
$test_data
=
array
(
'expiration'
=>
2
,
'foo'
=>
'bar'
,
);
$this
->
assertInstanceOf
(
Plugin
::
class
,
$this
->
plugin
);
$this
->
assertInstanceOf
(
WP_Session_Tokens
::
class
,
$this
->
plugin
);
$this
->
assertEquals
(
$test_data
,
$this
->
_invoke_method
(
0
,
'prepare_session'
,
array
(
$test_data
,
)
),
'Failed to assert that session data is not transformed if it is already prepared.'
);
}
/**
* Invoke a non-public class method.
*
* @param int $user_id WP User ID.
* @param string $method_name Method name.
* @param array $args Method arguments.
* @return mixed
*/
protected
function
_invoke_method
(
$user_id
,
$method_name
,
$args
=
[]
)
{
$object
=
new
Plugin
(
$user_id
);
$reflection
=
new
ReflectionClass
(
$object
);
$method
=
$reflection
->
getMethod
(
$method_name
);
$method
->
setAccessible
(
true
);
return
$method
->
invokeArgs
(
$object
,
$args
);
}
/**
* Get value of non-public property.
*
* @param int $user_id WP User ID.
* @param string $property_name Property name.
* @return mixed
*/
protected
function
_get_property
(
$user_id
,
$property_name
)
{
$object
=
new
Plugin
(
$user_id
);
$reflection
=
new
ReflectionClass
(
$object
);
$property
=
$reflection
->
getProperty
(
$property_name
);
$property
->
setAccessible
(
true
);
$this
->
assertEquals
(
'wpruss'
,
$this
->
plugin
->
prefix
);
return
$property
->
getValue
(
$object
);
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment