|
| 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