From 40507696441a4bb9507eba47f3df9e9f0d37aae6 Mon Sep 17 00:00:00 2001 From: Renatho De Carli Rosa Date: Fri, 5 Apr 2024 11:02:23 -0300 Subject: [PATCH 01/10] Add changelog --- changelog/change-setup-wizard-redirect | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 changelog/change-setup-wizard-redirect diff --git a/changelog/change-setup-wizard-redirect b/changelog/change-setup-wizard-redirect new file mode 100644 index 0000000000..fdd3fa30a7 --- /dev/null +++ b/changelog/change-setup-wizard-redirect @@ -0,0 +1,4 @@ +Significance: minor +Type: changed + +Open Setup Wizard when navigating through relevant pages on admin if it didn't open yet From 8771e3c0b8f3ef5db8ecb0bf2ca9f943349942b6 Mon Sep 17 00:00:00 2001 From: Renatho De Carli Rosa Date: Fri, 5 Apr 2024 11:11:53 -0300 Subject: [PATCH 02/10] Add custom capability --- phpcs.xml.dist | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/phpcs.xml.dist b/phpcs.xml.dist index 3c96bd0323..07e443962a 100644 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -81,4 +81,13 @@ **/views/* tests/bootstrap.php + + + + + + + + + From a62211db8322450e16656634158457b123c6592d Mon Sep 17 00:00:00 2001 From: Renatho De Carli Rosa Date: Fri, 5 Apr 2024 11:13:53 -0300 Subject: [PATCH 03/10] Fix PHPCS comment --- includes/admin/class-sensei-setup-wizard.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/admin/class-sensei-setup-wizard.php b/includes/admin/class-sensei-setup-wizard.php index 88e995890b..b0a01cb1bb 100644 --- a/includes/admin/class-sensei-setup-wizard.php +++ b/includes/admin/class-sensei-setup-wizard.php @@ -545,7 +545,7 @@ public static function close_wccom_install() { if ( isset( $_SERVER['HTTP_REFERER'] ) && - 0 === strpos( $_SERVER['HTTP_REFERER'], 'https://woocommerce.com/checkout' ) && // phpcs:ignore sanitization ok. + 0 === strpos( $_SERVER['HTTP_REFERER'], 'https://woocommerce.com/checkout' ) && // phpcs:ignore -- sanitization ok. false !== get_transient( $wccom_installing_transient ) ) { delete_transient( $wccom_installing_transient ); From 534655a8ce8e80273817871eebbf6f40a04faca6 Mon Sep 17 00:00:00 2001 From: Renatho De Carli Rosa Date: Fri, 5 Apr 2024 11:15:06 -0300 Subject: [PATCH 04/10] Update logic to open Setup Wizard on the same pages the notice was displayed --- includes/admin/class-sensei-setup-wizard.php | 16 +++++++++------- includes/class-sensei-data-cleaner.php | 3 ++- includes/class-sensei.php | 2 +- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/includes/admin/class-sensei-setup-wizard.php b/includes/admin/class-sensei-setup-wizard.php index b0a01cb1bb..17fd7fee05 100644 --- a/includes/admin/class-sensei-setup-wizard.php +++ b/includes/admin/class-sensei-setup-wizard.php @@ -93,7 +93,7 @@ public function __construct() { add_action( 'current_screen', [ $this, 'remove_notices_from_setup_wizard' ] ); add_action( 'admin_notices', [ $this, 'setup_wizard_notice' ] ); add_action( 'admin_init', [ $this, 'skip_setup_wizard' ] ); - add_action( 'admin_init', [ $this, 'activation_redirect' ] ); + add_action( 'current_screen', [ $this, 'activation_redirect' ] ); add_action( 'current_screen', [ $this, 'add_setup_wizard_help_tab' ] ); // Maybe prevent WooCommerce help tab. @@ -189,18 +189,17 @@ public function prepare_wizard_page() { public function activation_redirect() { if ( // Check if activation redirect is needed. - ! get_transient( 'sensei_activation_redirect' ) + ! get_option( 'sensei_activation_redirect', false ) // Test whether the context of execution comes from async action scheduler. // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Arguments used for comparison. || ( isset( $_REQUEST['action'] ) && 'as_async_request_queue_runner' === $_REQUEST['action'] ) // On these pages, or during these events, postpone the redirect. || wp_doing_ajax() || wp_doing_cron() || is_network_admin() || ! current_user_can( 'manage_sensei' ) + || ! $this->should_current_page_display_wizard() ) { return; } - delete_transient( 'sensei_activation_redirect' ); - // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Arguments used for comparison. if ( isset( $_GET['activate-multi'] ) ) { return; @@ -221,6 +220,8 @@ protected function redirect_to_setup_wizard() { * Render app container for setup wizard. */ public function render_wizard_page() { + // Delete option when the Setup Wizard is loaded, so it doesn't redirect anymore. + delete_option( 'sensei_activation_redirect' ); ?>
@@ -230,7 +231,8 @@ public function render_wizard_page() { } /** - * Check if current screen is selected to display the wizard notice. + * Check if current screen is selected to display the wizard notice and + * automatically open the wizard. * * @return boolean */ @@ -446,7 +448,7 @@ public function get_woocommerce_connect_data() { * * @return stdClass Extension with status. */ - private function get_feature_with_status( $extension, $installing_plugins, $selected_plugins ) { + private function get_feature_with_status( $extension, $installing_plugins, $selected_plugins ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed -- Called by a public deprecated method. _deprecated_function( __METHOD__, '4.8.0' ); $installing_key = array_search( $extension->product_slug, wp_list_pluck( $installing_plugins, 'product_slug' ), true ); @@ -496,7 +498,7 @@ public function get_sensei_extensions( $clear_active_plugins_cache = false ) { } $extensions = array_map( - function( $extension ) use ( $installing_plugins, $selected_plugins ) { + function ( $extension ) use ( $installing_plugins, $selected_plugins ) { // Decode price. if ( isset( $extension->price ) && 0 !== $extension->price ) { $extension->price = html_entity_decode( $extension->price ); diff --git a/includes/class-sensei-data-cleaner.php b/includes/class-sensei-data-cleaner.php index 99a45e32e0..a05e3692b2 100644 --- a/includes/class-sensei-data-cleaner.php +++ b/includes/class-sensei-data-cleaner.php @@ -99,6 +99,7 @@ class Sensei_Data_Cleaner { 'sensei_home_tasks_dismissed', 'sensei_home_tasks_list_is_completed', 'sensei-home-task-pro-upsell', + 'sensei_activation_redirect', ); /** @@ -233,7 +234,7 @@ class Sensei_Data_Cleaner { 'sensei_answers_feedback_[0-9]+_[0-9]+', 'quiz_grades_[0-9]+_[0-9]+', 'sensei_comment_counts_[0-9]+', - 'sensei_activation_redirect', + 'sensei_activation_redirect', // @deprecated $$next-version$$ Changed to an option. 'sensei_woocommerce_plugin_information', 'sensei_extensions_.*', 'sensei_background_job_.*', diff --git a/includes/class-sensei.php b/includes/class-sensei.php index a8e46617bb..d14d95d8b8 100644 --- a/includes/class-sensei.php +++ b/includes/class-sensei.php @@ -1166,7 +1166,7 @@ public function activate_sensei() { // Do not enable the wizard for sites that are created with the onboarding flow. if ( 'sensei' !== get_option( 'site_intent' ) ) { - set_transient( 'sensei_activation_redirect', 1, 30 ); + update_option( 'sensei_activation_redirect', 1 ); update_option( Sensei_Setup_Wizard::SUGGEST_SETUP_WIZARD_OPTION, 1 ); } else { From 05c4bb15e0233dfbe712b664f3a3a22ac2e90236 Mon Sep 17 00:00:00 2001 From: Renatho De Carli Rosa Date: Fri, 5 Apr 2024 14:01:47 -0300 Subject: [PATCH 05/10] Improve code documentation --- includes/admin/class-sensei-setup-wizard.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/includes/admin/class-sensei-setup-wizard.php b/includes/admin/class-sensei-setup-wizard.php index 17fd7fee05..93128d07d1 100644 --- a/includes/admin/class-sensei-setup-wizard.php +++ b/includes/admin/class-sensei-setup-wizard.php @@ -194,7 +194,10 @@ public function activation_redirect() { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Arguments used for comparison. || ( isset( $_REQUEST['action'] ) && 'as_async_request_queue_runner' === $_REQUEST['action'] ) // On these pages, or during these events, postpone the redirect. - || wp_doing_ajax() || wp_doing_cron() || is_network_admin() || ! current_user_can( 'manage_sensei' ) + || wp_doing_ajax() || wp_doing_cron() || is_network_admin() + // Only redirects for admin users. + || ! current_user_can( 'manage_sensei' ) + // Check if it's an admin screen that should redirect. || ! $this->should_current_page_display_wizard() ) { return; From e645908eefe250bd03e3d5148f0e3647014124a7 Mon Sep 17 00:00:00 2001 From: Renatho De Carli Rosa Date: Fri, 5 Apr 2024 14:25:18 -0300 Subject: [PATCH 06/10] Add tests --- .../admin/test-class-sensei-setup-wizard.php | 39 ++++++++++++++++--- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/tests/unit-tests/admin/test-class-sensei-setup-wizard.php b/tests/unit-tests/admin/test-class-sensei-setup-wizard.php index 08cc20c349..97812105b4 100644 --- a/tests/unit-tests/admin/test-class-sensei-setup-wizard.php +++ b/tests/unit-tests/admin/test-class-sensei-setup-wizard.php @@ -214,8 +214,9 @@ public function testActivationRedirect() { // Create and login as administrator. $admin_id = $this->factory->user->create( array( 'role' => 'administrator' ) ); wp_set_current_user( $admin_id ); + set_current_screen( 'dashboard' ); - set_transient( 'sensei_activation_redirect', 1, 30 ); + update_option( 'sensei_activation_redirect', 1 ); $setup_wizard_mock = $this->getMockBuilder( 'Sensei_Setup_Wizard' ) ->setMethods( [ 'redirect_to_setup_wizard' ] ) @@ -225,8 +226,27 @@ public function testActivationRedirect() { ->method( 'redirect_to_setup_wizard' ); $setup_wizard_mock->activation_redirect(); + } + + /* + * Testing if activation doesn't redirect for no Sensei screens. + */ + public function testActivationRedirectNoSenseiScreen() { + // Create and login as administrator. + $admin_id = $this->factory->user->create( array( 'role' => 'administrator' ) ); + wp_set_current_user( $admin_id ); + set_current_screen( 'any_other' ); + + update_option( 'sensei_activation_redirect', 1 ); + + $setup_wizard_mock = $this->getMockBuilder( 'Sensei_Setup_Wizard' ) + ->setMethods( [ 'redirect_to_setup_wizard' ] ) + ->getMock(); + + $setup_wizard_mock->expects( $this->never() ) + ->method( 'redirect_to_setup_wizard' ); - $this->assertFalse( get_transient( 'sensei_activation_redirect' ), 'Transient should be removed' ); + $setup_wizard_mock->activation_redirect(); } /** @@ -237,7 +257,7 @@ public function testActivationRedirectNoAdmin() { $subscriber_id = $this->factory->user->create( array( 'role' => 'subscriber' ) ); wp_set_current_user( $subscriber_id ); - set_transient( 'sensei_activation_redirect', 1, 30 ); + update_option( 'sensei_activation_redirect', 1 ); $setup_wizard_mock = $this->getMockBuilder( 'Sensei_Setup_Wizard' ) ->setMethods( [ 'redirect_to_setup_wizard' ] ) @@ -247,8 +267,6 @@ public function testActivationRedirectNoAdmin() { ->method( 'redirect_to_setup_wizard' ); $setup_wizard_mock->activation_redirect(); - - $this->assertNotFalse( get_transient( 'sensei_activation_redirect' ), 'Transient should not be removed' ); } /** @@ -269,6 +287,17 @@ public function testActivationRedirectWithoutTransient() { $setup_wizard_mock->activation_redirect(); } + /** + * Testing if redirect option is cleared on setup wizard rendering. + */ + public function testRenderWizardPageClearsRedirectOption() { + update_option( 'sensei_activation_redirect', 1 ); + + Sensei()->setup_wizard->render_wizard_page(); + + $this->assertFalse( get_option( 'sensei_activation_redirect', false ) ); + } + /** * Test if WooCommerce help tab is being prevented in the Sensei pages. */ From 4b4ddf5b3ccadc2edd780e5b80930e95183b5b14 Mon Sep 17 00:00:00 2001 From: Renatho De Carli Rosa Date: Fri, 5 Apr 2024 15:43:49 -0300 Subject: [PATCH 07/10] Add Sensei Home to the list of pages to open the setup wizard --- includes/admin/class-sensei-setup-wizard.php | 1 + 1 file changed, 1 insertion(+) diff --git a/includes/admin/class-sensei-setup-wizard.php b/includes/admin/class-sensei-setup-wizard.php index 93128d07d1..9504cd410b 100644 --- a/includes/admin/class-sensei-setup-wizard.php +++ b/includes/admin/class-sensei-setup-wizard.php @@ -249,6 +249,7 @@ private function should_current_page_display_wizard() { $screens_without_sensei_prefix = [ 'dashboard', 'plugins', + 'toplevel_page_sensei', 'edit-sensei_message', 'edit-course', 'edit-course-category', From 564fdde22fd1ad38692850eaf26cdd7aef746d94 Mon Sep 17 00:00:00 2001 From: Renatho De Carli Rosa Date: Fri, 5 Apr 2024 16:08:42 -0300 Subject: [PATCH 08/10] Update check to use one list for the notice and another one for the redirect --- includes/admin/class-sensei-setup-wizard.php | 60 ++++++++++++++++---- 1 file changed, 50 insertions(+), 10 deletions(-) diff --git a/includes/admin/class-sensei-setup-wizard.php b/includes/admin/class-sensei-setup-wizard.php index 9504cd410b..2be3d0312e 100644 --- a/includes/admin/class-sensei-setup-wizard.php +++ b/includes/admin/class-sensei-setup-wizard.php @@ -198,7 +198,7 @@ public function activation_redirect() { // Only redirects for admin users. || ! current_user_can( 'manage_sensei' ) // Check if it's an admin screen that should redirect. - || ! $this->should_current_page_display_wizard() + || ! $this->should_current_page_redirect_to_wizard() ) { return; } @@ -234,18 +234,39 @@ public function render_wizard_page() { } /** - * Check if current screen is selected to display the wizard notice and - * automatically open the wizard. + * Check if current screen is selected to display the wizard notice. * * @return boolean */ - private function should_current_page_display_wizard() { - $screen = get_current_screen(); + private function should_current_page_display_wizard_notice() { + // Dashboard, plugins, and Sensei pages, except Sensei Home. + $screens_without_sensei_prefix = [ + 'dashboard', + 'plugins', + 'edit-sensei_message', + 'edit-course', + 'edit-course-category', + 'admin_page_course-order', + 'edit-module', + 'admin_page_module-order', + 'edit-lesson', + 'edit-lesson-tag', + 'admin_page_lesson-order', + 'edit-question', + 'question', + 'edit-question-category', + ]; - if ( false !== strpos( $screen->id, 'sensei-lms_page_sensei' ) ) { - return true; - } + return $this->check_sensei_screen( $screens_without_sensei_prefix ); + } + /** + * Check if current screen is selected to redirect to the wizard. + * + * @return boolean + */ + private function should_current_page_redirect_to_wizard() { + // Dashboard, plugins, and Sensei pages. $screens_without_sensei_prefix = [ 'dashboard', 'plugins', @@ -264,7 +285,26 @@ private function should_current_page_display_wizard() { 'edit-question-category', ]; - return in_array( $screen->id, $screens_without_sensei_prefix, true ); + return $this->check_sensei_screen( $screens_without_sensei_prefix ); + } + + /** + * Check if current screen is a Sensei screen. + * The default check verifies if the screen ID contains 'sensei-lms_page_sensei'. + * For more screens to be checked, pass the IDs as an array. + * + * @param array $other_screens Other screens to check. + * + * @return boolean + */ + private function check_sensei_screen( $other_screens = [] ) { + $screen = get_current_screen(); + + if ( false !== strpos( $screen->id, 'sensei-lms_page_sensei' ) ) { + return true; + } + + return in_array( $screen->id, $other_screens, true ); } /** @@ -274,7 +314,7 @@ private function should_current_page_display_wizard() { */ public function setup_wizard_notice() { if ( - ! $this->should_current_page_display_wizard() + ! $this->should_current_page_display_wizard_notice() || ! get_option( self::SUGGEST_SETUP_WIZARD_OPTION, 0 ) || ! current_user_can( 'manage_sensei' ) ) { From 2d7b8eb903dbb8dc00b466e502505cda51ce9c79 Mon Sep 17 00:00:00 2001 From: Renatho De Carli Rosa Date: Mon, 8 Apr 2024 16:59:10 -0300 Subject: [PATCH 09/10] Update tests format --- .../admin/test-class-sensei-setup-wizard.php | 98 +++++++++++++++---- 1 file changed, 77 insertions(+), 21 deletions(-) diff --git a/tests/unit-tests/admin/test-class-sensei-setup-wizard.php b/tests/unit-tests/admin/test-class-sensei-setup-wizard.php index 97812105b4..1601e45b4c 100644 --- a/tests/unit-tests/admin/test-class-sensei-setup-wizard.php +++ b/tests/unit-tests/admin/test-class-sensei-setup-wizard.php @@ -58,14 +58,16 @@ public function tearDown(): void { /** * Testing the setup wizard class to make sure it is loaded. */ - public function testClassInstance() { + public function testClassInstance_Always_Exists() { + // Assert. $this->assertTrue( class_exists( 'Sensei_Setup_Wizard' ), 'Sensei Setup Wizard class does not exist' ); } /** * Test setup wizard notice in dashboard. */ - public function testSetupWizardNoticeInDashboard() { + public function testSetupWizardNotice_WhenSuggestSetupWizardOptionIsOneAndScreenIsDashboard_DisplaysNotice() { + // Arrange. // Create and login as admin. $admin_id = $this->factory->user->create( array( 'role' => 'administrator' ) ); wp_set_current_user( $admin_id ); @@ -73,19 +75,22 @@ public function testSetupWizardNoticeInDashboard() { set_current_screen( 'dashboard' ); update_option( \Sensei_Setup_Wizard::SUGGEST_SETUP_WIZARD_OPTION, 1 ); + // Act. ob_start(); Sensei()->setup_wizard->setup_wizard_notice(); $html = ob_get_clean(); $pos_setup_button = strpos( $html, 'Run the Setup Wizard' ); + // Assert. $this->assertNotFalse( $pos_setup_button, 'Should return the notice HTML' ); } /** * Test setup wizard notice in screen with Sensei prefix. */ - public function testSetupWizardNoticeInSenseiScreen() { + public function testSetupWizardNotice_WhenSuggestSetupWizardOptionIsOneAndScreenIsSenseiPage_DisplaysNotice() { + // Arrange. // Create and login as admin. $admin_id = $this->factory->user->create( array( 'role' => 'administrator' ) ); wp_set_current_user( $admin_id ); @@ -93,19 +98,22 @@ public function testSetupWizardNoticeInSenseiScreen() { set_current_screen( 'sensei-lms_page_sensei_test' ); update_option( \Sensei_Setup_Wizard::SUGGEST_SETUP_WIZARD_OPTION, 1 ); + // Act. ob_start(); Sensei()->setup_wizard->setup_wizard_notice(); $html = ob_get_clean(); $pos_setup_button = strpos( $html, 'Run the Setup Wizard' ); + // Assert. $this->assertNotFalse( $pos_setup_button, 'Should return the notice HTML' ); } /** * Test setup wizard notice in no Sensei screen. */ - public function testSetupWizardNoticeInOtherScreen() { + public function testSetupWizardNotice_WhenInOtherScreen_DoesNotDisplayNotice() { + // Arrange. // Create and login as admin. $admin_id = $this->factory->user->create( array( 'role' => 'administrator' ) ); wp_set_current_user( $admin_id ); @@ -113,17 +121,20 @@ public function testSetupWizardNoticeInOtherScreen() { set_current_screen( 'other' ); update_option( \Sensei_Setup_Wizard::SUGGEST_SETUP_WIZARD_OPTION, 1 ); + // Act. ob_start(); Sensei()->setup_wizard->setup_wizard_notice(); $html = ob_get_clean(); + // Assert. $this->assertEmpty( $html, 'Should return empty string' ); } /** * Test setup wizard notice with suggest option as 0. */ - public function testSetupWizardNoticeSuggestOptionAsZero() { + public function testSetupWizardNotice_WhenSuggestOptionIsZero_DoesNotDisplayNotice() { + // Arrange. // Create and login as admin. $admin_id = $this->factory->user->create( array( 'role' => 'administrator' ) ); wp_set_current_user( $admin_id ); @@ -131,52 +142,61 @@ public function testSetupWizardNoticeSuggestOptionAsZero() { set_current_screen( 'dashboard' ); update_option( \Sensei_Setup_Wizard::SUGGEST_SETUP_WIZARD_OPTION, 0 ); + // Act. ob_start(); Sensei()->setup_wizard->setup_wizard_notice(); $html = ob_get_clean(); + // Assert. $this->assertEmpty( $html, 'Should return empty string' ); } /** * Test setup wizard notice with suggest option empty. */ - public function testSetupWizardNoticeSuggestOptionEmpty() { + public function testSetupWizardNotice_WhenSuggestOptionIsEmpty_DoesNotDisplayNotice() { + // Arrange. // Create and login as admin. $admin_id = $this->factory->user->create( array( 'role' => 'administrator' ) ); wp_set_current_user( $admin_id ); set_current_screen( 'dashboard' ); + // Act. ob_start(); Sensei()->setup_wizard->setup_wizard_notice(); $html = ob_get_clean(); + // Assert. $this->assertEmpty( $html, 'Should return empty string' ); } /** * Test setup wizard notice for no admin user. */ - public function testSetupWizardNoticeNoAdmin() { + public function testSetupWizardNotice_WhenUserIsNoAdmin_DoesNotDisplayNotice() { + // Arrange. // Create and login as teacher. $teacher_id = $this->factory->user->create( array( 'role' => 'teacher' ) ); wp_set_current_user( $teacher_id ); set_current_screen( 'dashboard' ); - update_option( \Sensei_Setup_Wizard::SUGGEST_SETUP_WIZARD_OPTION, 0 ); + update_option( \Sensei_Setup_Wizard::SUGGEST_SETUP_WIZARD_OPTION, 1 ); + // Act. ob_start(); Sensei()->setup_wizard->setup_wizard_notice(); $html = ob_get_clean(); + // Assert. $this->assertEmpty( $html, 'Should return empty string' ); } /** * Test skip setup wizard. */ - public function testSkipSetupWizard() { + public function testSkipSetupWizard_WhenArgumentsAreSet_UpdatesOptionToZero() { + // Arrange. // Create and login as admin. $admin_id = $this->factory->user->create( array( 'role' => 'administrator' ) ); wp_set_current_user( $admin_id ); @@ -184,16 +204,19 @@ public function testSkipSetupWizard() { $_GET['sensei_skip_setup_wizard'] = '1'; $_GET['_wpnonce'] = wp_create_nonce( 'sensei_skip_setup_wizard' ); + // Act. Sensei()->setup_wizard->skip_setup_wizard(); $option_value = get_option( \Sensei_Setup_Wizard::SUGGEST_SETUP_WIZARD_OPTION, false ); + // Assert. $this->assertEquals( '0', $option_value, 'Should update option to 0' ); } /** * Test skip setup wizard. */ - public function testSkipSetupWizardNoAdmin() { + public function testSkipSetupWizard_WhenUserIsNoAdmin_DoesNotUpdateOption() { + // Arrange. // Create and login as teacher. $teacher_id = $this->factory->user->create( array( 'role' => 'teacher' ) ); wp_set_current_user( $teacher_id ); @@ -201,16 +224,19 @@ public function testSkipSetupWizardNoAdmin() { $_GET['sensei_skip_setup_wizard'] = '1'; $_GET['_wpnonce'] = wp_create_nonce( 'sensei_skip_setup_wizard' ); + // Act. Sensei()->setup_wizard->skip_setup_wizard(); $option_value = get_option( \Sensei_Setup_Wizard::SUGGEST_SETUP_WIZARD_OPTION, false ); + // Assert. $this->assertFalse( $option_value, 'Should not update option' ); } /* * Testing if activation redirect works properly. */ - public function testActivationRedirect() { + public function testActivationRedirect_WhenRedirectOptionIsOne_CallsRedirect() { + // Arrange. // Create and login as administrator. $admin_id = $this->factory->user->create( array( 'role' => 'administrator' ) ); wp_set_current_user( $admin_id ); @@ -222,16 +248,19 @@ public function testActivationRedirect() { ->setMethods( [ 'redirect_to_setup_wizard' ] ) ->getMock(); + // Assert. $setup_wizard_mock->expects( $this->once() ) ->method( 'redirect_to_setup_wizard' ); + // Act. $setup_wizard_mock->activation_redirect(); } /* * Testing if activation doesn't redirect for no Sensei screens. */ - public function testActivationRedirectNoSenseiScreen() { + public function testActivationRedirect_WhenInAPageNotRelatedToSensei_DoesNotCallRedirect() { + // Arrange. // Create and login as administrator. $admin_id = $this->factory->user->create( array( 'role' => 'administrator' ) ); wp_set_current_user( $admin_id ); @@ -243,16 +272,19 @@ public function testActivationRedirectNoSenseiScreen() { ->setMethods( [ 'redirect_to_setup_wizard' ] ) ->getMock(); + // Assert. $setup_wizard_mock->expects( $this->never() ) ->method( 'redirect_to_setup_wizard' ); + // Act. $setup_wizard_mock->activation_redirect(); } /** * Testing if activation doesn't redirect for no admin user. */ - public function testActivationRedirectNoAdmin() { + public function testActivationRedirect_WhenUserIsNoAdmin_DoesNotCallRedirect() { + // Arrange. // Create and login as subscriber. $subscriber_id = $this->factory->user->create( array( 'role' => 'subscriber' ) ); wp_set_current_user( $subscriber_id ); @@ -263,16 +295,19 @@ public function testActivationRedirectNoAdmin() { ->setMethods( [ 'redirect_to_setup_wizard' ] ) ->getMock(); + // Assert. $setup_wizard_mock->expects( $this->never() ) ->method( 'redirect_to_setup_wizard' ); + // Act. $setup_wizard_mock->activation_redirect(); } /** - * Testing if activation doesn't redirect when transient is not defined. + * Testing if activation doesn't redirect when option does not exist. */ - public function testActivationRedirectWithoutTransient() { + public function testActivationRedirect_WhenRedirectOptionDoesNotExist_DoesNotCallRedirect() { + // Arrange. // Create and login as administrator. $admin_id = $this->factory->user->create( array( 'role' => 'administrator' ) ); wp_set_current_user( $admin_id ); @@ -281,29 +316,36 @@ public function testActivationRedirectWithoutTransient() { ->setMethods( [ 'redirect_to_setup_wizard' ] ) ->getMock(); + // Assert. $setup_wizard_mock->expects( $this->never() ) ->method( 'redirect_to_setup_wizard' ); + // Act. $setup_wizard_mock->activation_redirect(); } /** * Testing if redirect option is cleared on setup wizard rendering. */ - public function testRenderWizardPageClearsRedirectOption() { + public function testRenderWizardPage_WhenRendered_ClearsRedirectOption() { + // Arrange. update_option( 'sensei_activation_redirect', 1 ); + // Act. Sensei()->setup_wizard->render_wizard_page(); + // Assert. $this->assertFalse( get_option( 'sensei_activation_redirect', false ) ); } /** * Test if WooCommerce help tab is being prevented in the Sensei pages. */ - public function testShouldEnableWooCommerceHelpTab() { + public function testWooCommerceHelpTab_WhenOnCoursePage_ShouldNotPreventTab() { + // Arrange. $_GET['post_type'] = 'course'; + // Act & Assert. $this->assertFalse( Sensei()->setup_wizard->should_enable_woocommerce_help_tab( true ), 'Should not allow WooCommerce help tab for course post type' @@ -313,9 +355,11 @@ public function testShouldEnableWooCommerceHelpTab() { /** * Test if WooCommerce help tab is being untouched in no Sensei pages. */ - public function testShouldEnableWooCommerceHelpTabNoSenseiPage() { + public function testWooCommerceHelpTab_WhenOnNoSenseiPage_ShouldNotChangeValue() { + // Arrange. $_GET['post_type'] = 'woocommerce'; + // Act & Assert. $this->assertTrue( Sensei()->setup_wizard->should_enable_woocommerce_help_tab( true ), 'Should not touch WooCommerce help tab for no Sensei pages' @@ -325,7 +369,8 @@ public function testShouldEnableWooCommerceHelpTabNoSenseiPage() { /** * Test add setup wizard help tab to edit course screen. */ - public function testAddSetupWizardHelpTab() { + public function testAddSetupWizardHelpTab_WhenInEditCourse_ShouldAddTab() { + // Arrange. // Create and login as administrator. $admin_id = $this->factory->user->create( array( 'role' => 'administrator' ) ); wp_set_current_user( $admin_id ); @@ -334,16 +379,20 @@ public function testAddSetupWizardHelpTab() { $screen = get_current_screen(); $screen->remove_help_tab( 'sensei_lms_setup_wizard_tab' ); + + // Act. Sensei()->setup_wizard->add_setup_wizard_help_tab( $screen ); $created_tab = $screen->get_help_tab( 'sensei_lms_setup_wizard_tab' ); + // Assert. $this->assertNotNull( $created_tab, 'Should create the setup wizard tab to edit course screens.' ); } /** * Test add setup wizard help tab in non edit course screens. */ - public function testAddSetupWizardHelpTabNonEditCourseScreen() { + public function testAddSetupWizardHelpTab_WhenNotInEditCourse_ShouldNotAddTab() { + // Arrange. // Create and login as administrator. $admin_id = $this->factory->user->create( array( 'role' => 'administrator' ) ); wp_set_current_user( $admin_id ); @@ -352,16 +401,20 @@ public function testAddSetupWizardHelpTabNonEditCourseScreen() { $screen = get_current_screen(); $screen->remove_help_tab( 'sensei_lms_setup_wizard_tab' ); + + // Act. Sensei()->setup_wizard->add_setup_wizard_help_tab( $screen ); $created_tab = $screen->get_help_tab( 'sensei_lms_setup_wizard_tab' ); + // Assert. $this->assertNull( $created_tab, 'Should not create the setup wizard tab to non edit course screens.' ); } /** * Test add setup wizard help tab for no admin user. */ - public function testAddSetupWizardHelpTabNoAdmin() { + public function testAddSetupWizardHelpTab_WhenUserIsNoAdmin_ShouldNotAddTab() { + // Arrange. // Create and login as teacher. $teacher_id = $this->factory->user->create( array( 'role' => 'teacher' ) ); wp_set_current_user( $teacher_id ); @@ -370,9 +423,12 @@ public function testAddSetupWizardHelpTabNoAdmin() { $screen = get_current_screen(); $screen->remove_help_tab( 'sensei_lms_setup_wizard_tab' ); + + // Act. Sensei()->setup_wizard->add_setup_wizard_help_tab( $screen ); $created_tab = $screen->get_help_tab( 'sensei_lms_setup_wizard_tab' ); + // Assert. $this->assertNull( $created_tab, 'Should not create the setup wizard tab to no admin user.' ); } From 693851ea06dfbb787ccb31db0826f6c19cf648ae Mon Sep 17 00:00:00 2001 From: Renatho De Carli Rosa Date: Mon, 8 Apr 2024 17:24:58 -0300 Subject: [PATCH 10/10] Use redirect helpers to test the setup wizard redirect --- .../admin/test-class-sensei-setup-wizard.php | 73 ++++++++++--------- 1 file changed, 40 insertions(+), 33 deletions(-) diff --git a/tests/unit-tests/admin/test-class-sensei-setup-wizard.php b/tests/unit-tests/admin/test-class-sensei-setup-wizard.php index 1601e45b4c..cdc03f6254 100644 --- a/tests/unit-tests/admin/test-class-sensei-setup-wizard.php +++ b/tests/unit-tests/admin/test-class-sensei-setup-wizard.php @@ -12,6 +12,8 @@ * @covers Sensei_Setup_Wizard */ class Sensei_Setup_Wizard_Test extends WP_UnitTestCase { + use Sensei_Test_Redirect_Helpers; + /** * Set up before the class. */ @@ -238,22 +240,24 @@ public function testSkipSetupWizard_WhenUserIsNoAdmin_DoesNotUpdateOption() { public function testActivationRedirect_WhenRedirectOptionIsOne_CallsRedirect() { // Arrange. // Create and login as administrator. - $admin_id = $this->factory->user->create( array( 'role' => 'administrator' ) ); + $expected_redirect = admin_url( 'admin.php?page=sensei_setup_wizard' ); + $admin_id = $this->factory->user->create( array( 'role' => 'administrator' ) ); + $this->prevent_wp_redirect(); wp_set_current_user( $admin_id ); set_current_screen( 'dashboard' ); update_option( 'sensei_activation_redirect', 1 ); - $setup_wizard_mock = $this->getMockBuilder( 'Sensei_Setup_Wizard' ) - ->setMethods( [ 'redirect_to_setup_wizard' ] ) - ->getMock(); + // Act. + $redirect_location = ''; + try { + Sensei()->setup_wizard->activation_redirect(); + } catch ( Sensei_WP_Redirect_Exception $e ) { + $redirect_location = $e->getMessage(); + } // Assert. - $setup_wizard_mock->expects( $this->once() ) - ->method( 'redirect_to_setup_wizard' ); - - // Act. - $setup_wizard_mock->activation_redirect(); + $this->assertSame( $expected_redirect, $redirect_location ); } /* @@ -263,21 +267,22 @@ public function testActivationRedirect_WhenInAPageNotRelatedToSensei_DoesNotCall // Arrange. // Create and login as administrator. $admin_id = $this->factory->user->create( array( 'role' => 'administrator' ) ); + $this->prevent_wp_redirect(); wp_set_current_user( $admin_id ); set_current_screen( 'any_other' ); update_option( 'sensei_activation_redirect', 1 ); - $setup_wizard_mock = $this->getMockBuilder( 'Sensei_Setup_Wizard' ) - ->setMethods( [ 'redirect_to_setup_wizard' ] ) - ->getMock(); + // Act. + $redirect_location = ''; + try { + Sensei()->setup_wizard->activation_redirect(); + } catch ( Sensei_WP_Redirect_Exception $e ) { + $redirect_location = $e->getMessage(); + } // Assert. - $setup_wizard_mock->expects( $this->never() ) - ->method( 'redirect_to_setup_wizard' ); - - // Act. - $setup_wizard_mock->activation_redirect(); + $this->assertEmpty( $redirect_location ); } /** @@ -287,20 +292,21 @@ public function testActivationRedirect_WhenUserIsNoAdmin_DoesNotCallRedirect() { // Arrange. // Create and login as subscriber. $subscriber_id = $this->factory->user->create( array( 'role' => 'subscriber' ) ); + $this->prevent_wp_redirect(); wp_set_current_user( $subscriber_id ); update_option( 'sensei_activation_redirect', 1 ); - $setup_wizard_mock = $this->getMockBuilder( 'Sensei_Setup_Wizard' ) - ->setMethods( [ 'redirect_to_setup_wizard' ] ) - ->getMock(); + // Act. + $redirect_location = ''; + try { + Sensei()->setup_wizard->activation_redirect(); + } catch ( Sensei_WP_Redirect_Exception $e ) { + $redirect_location = $e->getMessage(); + } // Assert. - $setup_wizard_mock->expects( $this->never() ) - ->method( 'redirect_to_setup_wizard' ); - - // Act. - $setup_wizard_mock->activation_redirect(); + $this->assertEmpty( $redirect_location ); } /** @@ -310,18 +316,19 @@ public function testActivationRedirect_WhenRedirectOptionDoesNotExist_DoesNotCal // Arrange. // Create and login as administrator. $admin_id = $this->factory->user->create( array( 'role' => 'administrator' ) ); + $this->prevent_wp_redirect(); wp_set_current_user( $admin_id ); - $setup_wizard_mock = $this->getMockBuilder( 'Sensei_Setup_Wizard' ) - ->setMethods( [ 'redirect_to_setup_wizard' ] ) - ->getMock(); + // Act. + $redirect_location = ''; + try { + Sensei()->setup_wizard->activation_redirect(); + } catch ( Sensei_WP_Redirect_Exception $e ) { + $redirect_location = $e->getMessage(); + } // Assert. - $setup_wizard_mock->expects( $this->never() ) - ->method( 'redirect_to_setup_wizard' ); - - // Act. - $setup_wizard_mock->activation_redirect(); + $this->assertEmpty( $redirect_location ); } /**