Skip to content

Commit 6718a99

Browse files
author
Jaroslav Polakovič
authored
Merge pull request #1171 from xwp/tests/user-switching-connector
Tests/user switching connector
2 parents 77ec1ce + ad3cdfa commit 6718a99

File tree

5 files changed

+188
-8
lines changed

5 files changed

+188
-8
lines changed

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
"wp-phpunit/wp-phpunit": "^5.4",
2424
"wpsh/local": "^0.2.3",
2525
"wpackagist-plugin/advanced-custom-fields": "5.8.12",
26-
"wpackagist-plugin/easy-digital-downloads": "^2.9.23"
26+
"wpackagist-plugin/easy-digital-downloads": "^2.9.23",
27+
"wpackagist-plugin/user-switching": "^1.5.5"
2728
},
2829
"config": {
2930
"process-timeout": 600,

composer.lock

+19-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

connectors/class-connector-user-switching.php

+1-5
Original file line numberDiff line numberDiff line change
@@ -219,13 +219,9 @@ public function callback_switch_back_user( $user_id, $old_user_id ) {
219219
public function callback_switch_off_user( $old_user_id ) {
220220

221221
$old_user = get_userdata( $old_user_id );
222-
$message = __(
223-
'Switched off',
224-
'stream'
225-
);
226222

227223
$this->log(
228-
$message,
224+
__( 'Switched off', 'stream' ),
229225
array(),
230226
$old_user->ID,
231227
'sessions',

phpunit.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<php>
1111
<const
1212
name="WP_TEST_ACTIVATED_PLUGINS"
13-
value="advanced-custom-fields/acf.php,easy-digital-downloads/easy-digital-downloads.php"
13+
value="advanced-custom-fields/acf.php,easy-digital-downloads/easy-digital-downloads.php,user-switching/user-switching.php"
1414
/>
1515
</php>
1616
<testsuites>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
<?php
2+
/**
3+
* WP Integration Test w/ User Switching plugin
4+
*
5+
* Tests for User Switching connector class callbacks.
6+
*
7+
* @package WP_Stream
8+
*/
9+
10+
namespace WP_Stream;
11+
12+
class Test_WP_Stream_Connector_User_Switching extends WP_StreamTestCase {
13+
/**
14+
* Runs before each test
15+
*/
16+
public function setUp() {
17+
parent::setUp();
18+
19+
$this->plugin->connectors->unload_connectors();
20+
21+
// Make partial of Connector_User_Switching class, with mocked "log" function.
22+
$this->mock = $this->getMockBuilder( Connector_User_Switching::class )
23+
->setMethods( array( 'log' ) )
24+
->getMock();
25+
26+
// Register connector.
27+
$this->mock->register();
28+
}
29+
30+
/**
31+
* Confirm that User Switching is installed and active.
32+
*/
33+
public function test_user_switching_installed_and_activated() {
34+
$this->assertTrue( class_exists( 'user_switching' ) );
35+
}
36+
37+
public function test_callback_switch_to_user() {
38+
// Create and authenticate user to be switched from.
39+
$old_user_id = self::factory()->user->create(
40+
array(
41+
'user_login' => 'oldtestuser',
42+
'user_role' => 'adminstrator',
43+
'display_name' => 'oldtestuserdisplay',
44+
)
45+
);
46+
wp_set_current_user( $old_user_id );
47+
48+
// Create user ID for destination user.
49+
$user_id = self::factory()->user->create(
50+
array(
51+
'user_login' => 'testuser',
52+
'user_role' => 'adminstrator',
53+
'display_name' => 'testuserdisplay',
54+
)
55+
);
56+
57+
// Expected log calls.
58+
$this->mock->expects( $this->once() )
59+
->method( 'log' )
60+
->with(
61+
$this->equalTo(
62+
_x(
63+
'Switched user to %1$s (%2$s)',
64+
'1: User display name, 2: User login',
65+
'stream'
66+
)
67+
),
68+
$this->equalTo(
69+
array(
70+
'display_name' => 'testuserdisplay',
71+
'user_login' => 'testuser',
72+
)
73+
),
74+
$this->equalTo( $old_user_id ),
75+
$this->equalTo( 'sessions' ),
76+
$this->equalTo( 'switched-to' ),
77+
$this->equalTo( $old_user_id )
78+
);
79+
80+
// Switch to user to trigger callback.
81+
\switch_to_user( $user_id );
82+
83+
// Check callback test action.
84+
$this->assertGreaterThan( 0, did_action( 'wp_stream_test_callback_switch_to_user' ) );
85+
}
86+
87+
public function test_callback_switch_back_user() {
88+
// Create and authenticate users for later use.
89+
$old_user_id = self::factory()->user->create(
90+
array(
91+
'user_login' => 'oldtestuser',
92+
'user_role' => 'adminstrator',
93+
'display_name' => 'oldtestuserdisplay',
94+
)
95+
);
96+
$user_id = self::factory()->user->create(
97+
array(
98+
'user_login' => 'testuser',
99+
'user_role' => 'adminstrator',
100+
'display_name' => 'testuserdisplay',
101+
)
102+
);
103+
104+
wp_set_current_user( $old_user_id );
105+
\switch_to_user( $user_id );
106+
107+
// Expected log calls.
108+
$this->mock->expects( $this->once() )
109+
->method( 'log' )
110+
->with(
111+
$this->equalTo(
112+
_x(
113+
'Switched back to %1$s (%2$s)',
114+
'1: User display name, 2: User login',
115+
'stream'
116+
)
117+
),
118+
$this->equalTo(
119+
array(
120+
'display_name' => 'oldtestuserdisplay',
121+
'user_login' => 'oldtestuser',
122+
)
123+
),
124+
$this->equalTo( $user_id ),
125+
$this->equalTo( 'sessions' ),
126+
$this->equalTo( 'switched-back' ),
127+
$this->equalTo( $user_id )
128+
);
129+
130+
// Switch to user to trigger callback.
131+
\switch_to_user( $old_user_id, false, false );
132+
133+
// Check callback test action.
134+
$this->assertGreaterThan( 0, did_action( 'wp_stream_test_callback_switch_back_user' ) );
135+
}
136+
137+
public function test_callback_switch_off_user() {
138+
// Create/authenticate user for later use.
139+
$user_id = self::factory()->user->create(
140+
array(
141+
'user_login' => 'testuser',
142+
'user_role' => 'adminstrator',
143+
'display_name' => 'testuserdisplay',
144+
)
145+
);
146+
wp_set_current_user( $user_id );
147+
148+
$this->mock->expects( $this->once() )
149+
->method( 'log' )
150+
->with(
151+
$this->equalTo( __( 'Switched off', 'stream' ) ),
152+
$this->equalTo( array() ),
153+
$this->equalTo( $user_id ),
154+
$this->equalTo( 'sessions' ),
155+
$this->equalTo( 'switched-off' ),
156+
$this->equalTo( $user_id )
157+
);
158+
159+
// Switch to user to trigger callback.
160+
\switch_off_user();
161+
162+
// Check callback test action.
163+
$this->assertGreaterThan( 0, did_action( 'wp_stream_test_callback_switch_off_user' ) );
164+
}
165+
}

0 commit comments

Comments
 (0)