|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Tests for the functions contained in includes/admin/wcs-admin-functions.php. |
| 5 | + */ |
| 6 | +class WCS_Admin_Functions_Test extends WP_UnitTestCase { |
| 7 | + /** |
| 8 | + * User ID of an administrator-level test user. |
| 9 | + * |
| 10 | + * @var int |
| 11 | + */ |
| 12 | + private static $admin_id; |
| 13 | + |
| 14 | + /** |
| 15 | + * User ID of a contributor-level test user. |
| 16 | + * |
| 17 | + * @var int |
| 18 | + */ |
| 19 | + private static $contributor_id; |
| 20 | + |
| 21 | + /** |
| 22 | + * Ensure the admin functions are loaded in preparation for our tests. |
| 23 | + * |
| 24 | + * @return void |
| 25 | + */ |
| 26 | + public static function set_up_before_class() { |
| 27 | + parent::set_up_before_class(); |
| 28 | + |
| 29 | + if ( ! function_exists( 'wcs_admin_notice' ) ) { |
| 30 | + require_once __DIR__ . '/../../includes/admin/wcs-admin-functions.php'; |
| 31 | + } |
| 32 | + |
| 33 | + self::$admin_id = self::factory()->user->create( array( 'role' => 'administrator' ) ); |
| 34 | + self::$contributor_id = self::factory()->user->create( array( 'role' => 'contributor' ) ); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Admin notices should target a specific user, and should not be visible to other users. |
| 39 | + * |
| 40 | + * @see wcs_add_admin_notice() |
| 41 | + * @see wcs_clear_admin_notices() |
| 42 | + * @see wcs_display_admin_notices() |
| 43 | + * |
| 44 | + * @return void |
| 45 | + */ |
| 46 | + public function test_wcs_admin_notice_visibility_by_user() { |
| 47 | + $message_text = 'The first rule of subscription club, is you do not talk about subscription club.'; |
| 48 | + |
| 49 | + wp_set_current_user( self::$admin_id ); |
| 50 | + wcs_add_admin_notice( $message_text ); |
| 51 | + |
| 52 | + wp_set_current_user( self::$contributor_id ); |
| 53 | + $this->assertEquals( |
| 54 | + '', |
| 55 | + $this->capture_wcs_admin_notice_text(), |
| 56 | + 'The message was not exposed to the wrong user.' |
| 57 | + ); |
| 58 | + |
| 59 | + wp_set_current_user( self::$admin_id ); |
| 60 | + $this->assertStringContainsString( |
| 61 | + $message_text, |
| 62 | + $this->capture_wcs_admin_notice_text(), |
| 63 | + 'The expected message was shared with the user.' |
| 64 | + ); |
| 65 | + |
| 66 | + wcs_add_admin_notice( $message_text, 'success', self::$contributor_id ); |
| 67 | + $this->assertEquals( |
| 68 | + '', |
| 69 | + $this->capture_wcs_admin_notice_text(), |
| 70 | + 'The message (which does not target the current user) is not inadvertently shown to the current user.' |
| 71 | + ); |
| 72 | + |
| 73 | + wp_set_current_user( self::$contributor_id ); |
| 74 | + $this->assertStringContainsString( |
| 75 | + $message_text, |
| 76 | + $this->capture_wcs_admin_notice_text(), |
| 77 | + 'The expected message was shared with the correct user.' |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Admin notices should not be accepted if a user is not actually logged in. |
| 83 | + * |
| 84 | + * This covers an edge case that generally should not arise. However, if it did, we would want to avoid |
| 85 | + * a scenario in which a '_wcs_admin_notices_0' transient is created and starts to balloon in size. |
| 86 | + * |
| 87 | + * @return void |
| 88 | + */ |
| 89 | + public function test_wcs_admin_notices_are_only_added_when_a_user_is_logged_in() { |
| 90 | + $logged_messages = []; |
| 91 | + $logging_monitor = function ( $message ) use ( &$logged_messages ) { |
| 92 | + $logged_messages[] = $message; |
| 93 | + }; |
| 94 | + |
| 95 | + add_filter( 'woocommerce_logger_log_message', $logging_monitor ); |
| 96 | + wp_set_current_user( 0 ); |
| 97 | + wcs_add_admin_notice( "You're gonna need a bigger subscription." ); |
| 98 | + remove_filter( 'woocommerce_logger_log_message', $logging_monitor ); |
| 99 | + |
| 100 | + $this->assertEquals( |
| 101 | + '', |
| 102 | + $this->capture_wcs_admin_notice_text(), |
| 103 | + 'If a user is not logged in, admin notifications are not accepted.' |
| 104 | + ); |
| 105 | + |
| 106 | + $this->assertStringContainsString( |
| 107 | + 'Admin notices can only be added if a user is currently logged in', |
| 108 | + $logged_messages[0], |
| 109 | + 'If an attempt is made to add an admin notice when nobody is logged in, a warning is logged.' |
| 110 | + ); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Admin notices can target a specific admin screen, and should not render outside of that context. |
| 115 | + * |
| 116 | + * @see wcs_add_admin_notice() |
| 117 | + * @see wcs_clear_admin_notices() |
| 118 | + * @see wcs_display_admin_notices() |
| 119 | + * |
| 120 | + * @return void |
| 121 | + */ |
| 122 | + public function test_wcs_admin_notice_visibility_by_screen() { |
| 123 | + global $current_screen; |
| 124 | + |
| 125 | + $original_screen = $current_screen; |
| 126 | + $message_text = 'The second rule of subscription club, is you DO NOT talk about subscription club.'; |
| 127 | + |
| 128 | + wp_set_current_user( self::$admin_id ); |
| 129 | + wcs_add_admin_notice( $message_text, 'error', null, 'subscriptions-dashboard' ); |
| 130 | + |
| 131 | + $this->assertEquals( |
| 132 | + '', |
| 133 | + $this->capture_wcs_admin_notice_text(), |
| 134 | + 'The message was not exposed outside of the specified screen.' |
| 135 | + ); |
| 136 | + |
| 137 | + $test_screen = WP_Screen::get( 'subscriptions-dashboard' ); |
| 138 | + set_current_screen( $test_screen ); |
| 139 | + |
| 140 | + $this->assertStringContainsString( |
| 141 | + $message_text, |
| 142 | + $this->capture_wcs_admin_notice_text(), |
| 143 | + 'The message was displayed in the context of the specified screen.' |
| 144 | + ); |
| 145 | + |
| 146 | + set_current_screen( $original_screen ); |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Admin notices generally act as 'flash messages' and are removed from the queue after they |
| 151 | + * have rendered. However, the system also allows for them to stay in the queue. |
| 152 | + * |
| 153 | + * @return void |
| 154 | + */ |
| 155 | + public function test_wcs_admin_notice_queue_clearance() { |
| 156 | + wp_set_current_user( self::$admin_id ); |
| 157 | + $message_text = "That's no moon, it's a subscription notice."; |
| 158 | + wcs_add_admin_notice( $message_text, 'error' ); |
| 159 | + |
| 160 | + $this->assertStringContainsString( |
| 161 | + esc_html( $message_text ), |
| 162 | + $this->capture_wcs_admin_notice_text( false ), |
| 163 | + 'The admin notice is displayed as expected.' |
| 164 | + ); |
| 165 | + |
| 166 | + $this->assertStringContainsString( |
| 167 | + esc_html( $message_text ), |
| 168 | + $this->capture_wcs_admin_notice_text(), |
| 169 | + 'The admin notice is displayed a second time, because it was not cleared last time.' |
| 170 | + ); |
| 171 | + |
| 172 | + $this->assertEquals( |
| 173 | + '', |
| 174 | + $this->capture_wcs_admin_notice_text(), |
| 175 | + 'The admin notice does not display, because it was cleared from the queue.' |
| 176 | + ); |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * Equivalent to calling wcs_display_admin_notices() directly, except the function output is |
| 181 | + * captured and returned in a string. |
| 182 | + * |
| 183 | + * @param bool $clear If the message queue should be cleared after getting/displaying the messages. |
| 184 | + * |
| 185 | + * @return string |
| 186 | + */ |
| 187 | + private function capture_wcs_admin_notice_text( $clear = true ) { |
| 188 | + ob_start(); |
| 189 | + wcs_display_admin_notices( $clear ); |
| 190 | + return (string) ob_get_clean(); |
| 191 | + } |
| 192 | +} |
0 commit comments