From a4785b7c2a634b6cc6a0bc6865aa04347024bacb Mon Sep 17 00:00:00 2001 From: Joshua T Flowers Date: Tue, 2 Mar 2021 10:15:53 -0500 Subject: [PATCH] Add navigation favorites tests (https://github.com/woocommerce/woocommerce-admin/pull/6409) * Add navigation favorites tests * Fix copy pasta * Fix test comment * Add changelog entry --- plugins/woocommerce-admin/readme.txt | 1 + .../tests/navigation/class-wc-favorites.php | 103 ++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 plugins/woocommerce-admin/tests/navigation/class-wc-favorites.php diff --git a/plugins/woocommerce-admin/readme.txt b/plugins/woocommerce-admin/readme.txt index 2d0dfa04ef6..9a1691bb26f 100644 --- a/plugins/woocommerce-admin/readme.txt +++ b/plugins/woocommerce-admin/readme.txt @@ -77,6 +77,7 @@ Release and roadmap notes are available on the [WooCommerce Developers Blog](htt - Fix: Add check for navigating being enabled. #6462 - Dev: Add a changelog lint check to PRs. #6414 +- Dev: Add navigation favorites tests #6409 - Fix: Move the shipping input and text 1px lower. #6408 - Dev: support use of Array.flat in client and packages. #6411 - Fix: Correct the Klarna slug #6440 diff --git a/plugins/woocommerce-admin/tests/navigation/class-wc-favorites.php b/plugins/woocommerce-admin/tests/navigation/class-wc-favorites.php new file mode 100644 index 00000000000..b0498189bac --- /dev/null +++ b/plugins/woocommerce-admin/tests/navigation/class-wc-favorites.php @@ -0,0 +1,103 @@ +instance = new Favorites(); + $this->user = $this->factory->user->create( + array( + 'role' => 'administrator', + ) + ); + } + + /** + * Test that errors are given when no user is provided or logged in. + */ + public function test_favorites_without_user() { + $result = $this->instance->add_item( 'menu-item' ); + $this->assertInstanceOf( 'WP_Error', $result ); + + $result = $this->instance->remove_item( 'menu-item' ); + $this->assertInstanceOf( 'WP_Error', $result ); + + $result = $this->instance->get_all(); + $this->assertInstanceOf( 'WP_Error', $result ); + } + + /** + * Test that favorites can be added. + */ + public function test_add_favorites() { + wp_set_current_user( $this->user ); + + $result = $this->instance->add_item( 'menu-item' ); + $this->assertTrue( $result ); + $result = $this->instance->add_item( 'menu-item2' ); + $this->assertTrue( $result ); + + $favorites = $this->instance->get_all(); + $this->assertContains( 'menu-item', $favorites ); + $this->assertContains( 'menu-item2', $favorites ); + } + + /** + * Test that favorites can be removed. + */ + public function test_remove_favorites() { + wp_set_current_user( $this->user ); + + $result = $this->instance->add_item( 'item-to-remove' ); + $this->assertTrue( $result ); + + $favorites = $this->instance->get_all(); + $this->assertContains( 'item-to-remove', $favorites ); + + $result = $this->instance->remove_item( 'item-to-remove' ); + $favorites = $this->instance->get_all(); + $this->assertNotContains( 'item-to-remove', $favorites ); + } + + /** + * Test that existing favorites can not be added again. + */ + public function test_add_previously_added_favorite() { + wp_set_current_user( $this->user ); + + $result = $this->instance->add_item( 'duplicate-item' ); + $this->assertTrue( $result ); + + $result = $this->instance->add_item( 'duplicate-item' ); + $this->assertInstanceOf( 'WP_Error', $result ); + } + + /** + * Test removing a favorite that does not exist. + */ + public function test_remove_invalid_favorite() { + wp_set_current_user( $this->user ); + + $result = $this->instance->remove_item( 'does-not-exist' ); + $this->assertInstanceOf( 'WP_Error', $result ); + } +}