Skip to content

Commit

Permalink
Misc changes to the unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
renatonascalves committed Feb 1, 2024
1 parent 6f0861d commit a0639fc
Show file tree
Hide file tree
Showing 18 changed files with 145 additions and 79 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ This library adheres to [Semantic Versioning](https://semver.org/) and [Keep a C
* `prevent_framing`: Added a feature to prevent framing of the site via the
`X-Frame-Options` header.

### Changed

* Unit tests: misc changes and fixes.
* Unit tests: the `$feature` property uses the main feature class for better IDE intelephense support.
* Unit tests: all test cases use `declare( strict_types=1 );`.
* Unit tests: added test to confirm the attachment rewrite rules are removed

## 2.3.1

### Changed
Expand Down
1 change: 1 addition & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
bootstrap="tests/bootstrap.php"
backupGlobals="false"
colors="true"
convertDeprecationsToExceptions="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

namespace Alley\WP\Alleyvate\Features;

use WP_Admin_Bar;
use WP_Query;
use Alley\WP\Alleyvate\Feature;

/**
Expand Down Expand Up @@ -48,10 +50,9 @@ public static function filter__rewrite_rules_array( $rules ): array {
/**
* Remove the attachment link.
*
* @param string $link Attachment link.
* @return string
*/
public static function filter__attachment_link( $link ): string {
public static function filter__attachment_link(): string {
return '';
}

Expand All @@ -60,7 +61,7 @@ public static function filter__attachment_link( $link ): string {
*
* @param WP_Query $query WP_Query object.
*/
public static function action__pre_get_posts( $query ) {
public static function action__pre_get_posts( $query ): void {
if ( is_admin() || ! $query->is_main_query() ) {
return;
}
Expand All @@ -77,7 +78,7 @@ public static function action__pre_get_posts( $query ) {
/**
* Remove attachment link from admin bar.
*
* @param \WP_Admin_Bar $wp_admin_bar Admin bar class.
* @param WP_Admin_Bar $wp_admin_bar Admin bar class.
*/
public static function action__admin_bar_menu( $wp_admin_bar ): void {
if ( 'attachment' === get_post_type() ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,18 @@
* @package wp-alleyvate
*/

declare( strict_types=1 );

namespace Alley\WP\Alleyvate\Features\Concerns;

use Alley\WP\Alleyvate\Feature;
use Mantle\Testing\Concerns\Admin_Screen;

/**
* Test the removal of a meta box.
*/
trait Remove_Meta_Box {
use \Mantle\Testing\Concerns\Admin_Screen;
use Admin_Screen;

/**
* Test the removal of a meta box.
Expand All @@ -30,7 +33,6 @@ trait Remove_Meta_Box {
* @param string $screen Screen to test on.
* @param string $context Meta box context.
* @param string $priority Meta box priority.
* @return void
*/
protected function assertMetaBoxRemoval( Feature $feature, string $id, string $screen = 'post', string $context = 'normal', string $priority = 'default' ): void {
$post = self::factory()->post->create_and_get();
Expand Down
23 changes: 14 additions & 9 deletions tests/alley/wp/alleyvate/features/test-clean-admin-bar.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,26 @@
* @package wp-alleyvate
*/

declare( strict_types=1 );

namespace Alley\WP\Alleyvate\Features;

use Alley\WP\Alleyvate\Feature;
use WP_Admin_Bar;
use Mantle\Testkit\Test_Case;
use Mantle\Testing\Concerns\Refresh_Database;

/**
* Tests for the cleaning of the admin bar.
*/
final class Test_Clean_Admin_Bar extends Test_Case {
use \Mantle\Testing\Concerns\Refresh_Database;
use Refresh_Database;

/**
* Feature instance.
*
* @var Feature
* @var Clean_Admin_Bar
*/
private Feature $feature;
private Clean_Admin_Bar $feature;

/**
* Set up.
Expand All @@ -40,8 +43,7 @@ protected function setUp(): void {
/**
* Test default admin bar cleaning.
*/
public function test_remove_admin_bar_nodes() {

public function test_remove_admin_bar_nodes(): void {
$admin_bar = $this->apply_admin_bar();

// Get nodes to compare.
Expand Down Expand Up @@ -73,8 +75,7 @@ public function test_remove_admin_bar_nodes() {
/**
* Test admin bar cleaning using filter.
*/
public function test_filter() {

public function test_filter(): void {
$admin_bar = $this->apply_admin_bar();
$node = 'my-account';

Expand Down Expand Up @@ -105,8 +106,12 @@ function ( $disposable_nodes ) use ( $node ) {

/**
* Apply the admin bar.
*
* @global WP_Admin_Bar $wp_admin_bar Core class used to implement the Toolbar API.
*
* @return WP_Admin_Bar
*/
public function apply_admin_bar() {
public function apply_admin_bar(): WP_Admin_Bar {
// Load file required to work with the admin bar.
require_once ABSPATH . WPINC . '/class-wp-admin-bar.php';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,25 @@
* @package wp-alleyvate
*/

declare( strict_types=1 );

namespace Alley\WP\Alleyvate\Features;

use Alley\WP\Alleyvate\Feature;
use Mantle\Testing\Concerns\Refresh_Database;
use Mantle\Testkit\Test_Case;

/**
* Tests for fully disabling attachment routing.
*/
final class Test_Disable_Attachment_Routing extends Test_Case {
use \Mantle\Testing\Concerns\Admin_Screen;
use \Mantle\Testing\Concerns\Refresh_Database;
use Refresh_Database;

/**
* Feature instance.
*
* @var Feature
* @var Disable_Attachment_Routing
*/
private Feature $feature;
private Disable_Attachment_Routing $feature;

/**
* Set up.
Expand All @@ -38,6 +39,35 @@ protected function setUp(): void {
$this->feature = new Disable_Attachment_Routing();
}

/**
* Test that the feature removes rewrite rules related to attachments.
*/
public function test_attachment_rewrile_rules_are_removed(): void {
$rewrite_rules = get_option( 'rewrite_rules' );

/**
* Attachment paths extracted from WordPress Develop.
*
* @see https://github.com/WordPress/wordpress-develop/blob/5b46851f7c52c2548630314d456b6e058d32a645/tests/phpunit/tests/query/conditionals.php#L774-L789
*/

$this->assertArrayHasKey( '[0-9]{4}/[0-9]{1,2}/[0-9]{1,2}/[^/]+/attachment/([^/]+)/?$', $rewrite_rules );
$this->assertArrayHasKey( '[0-9]{4}/[0-9]{1,2}/[0-9]{1,2}/[^/]+/attachment/([^/]+)/trackback/?$', $rewrite_rules );
$this->assertArrayHasKey( '[0-9]{4}/[0-9]{1,2}/[0-9]{1,2}/[^/]+/attachment/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$', $rewrite_rules );
$this->assertArrayHasKey( '[0-9]{4}/[0-9]{1,2}/[0-9]{1,2}/[^/]+/attachment/([^/]+)/(feed|rdf|rss|rss2|atom)/?$', $rewrite_rules );

$this->feature->boot();

flush_rewrite_rules( false ); // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.flush_rewrite_rules_flush_rewrite_rules

$rewrite_rules = get_option( 'rewrite_rules' );

$this->assertArrayNotHasKey( '[0-9]{4}/[0-9]{1,2}/[0-9]{1,2}/[^/]+/attachment/([^/]+)/?$', $rewrite_rules );
$this->assertArrayNotHasKey( '[0-9]{4}/[0-9]{1,2}/[0-9]{1,2}/[^/]+/attachment/([^/]+)/trackback/?$', $rewrite_rules );
$this->assertArrayNotHasKey( '[0-9]{4}/[0-9]{1,2}/[0-9]{1,2}/[^/]+/attachment/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$', $rewrite_rules );
$this->assertArrayNotHasKey( '[0-9]{4}/[0-9]{1,2}/[0-9]{1,2}/[^/]+/attachment/([^/]+)/(feed|rdf|rss|rss2|atom)/?$', $rewrite_rules );
}

/**
* Test that the attachment permalink is empty.
*/
Expand Down Expand Up @@ -72,7 +102,7 @@ public function test_attachment_page(): void {
* Test that the attachment pages are disabled using the new
* 'wp_attachment_pages_enabled' option from WordPress 6.4.
*/
public function test_attachment_pages_disabled_using_option() {
public function test_attachment_pages_disabled_using_option(): void {
update_option( 'wp_attachment_pages_enabled', '1' );

$this->assertEquals( '1', get_option( 'wp_attachment_pages_enabled' ) );
Expand Down
12 changes: 7 additions & 5 deletions tests/alley/wp/alleyvate/features/test-disable-comments.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,27 @@
* @package wp-alleyvate
*/

declare( strict_types=1 );

namespace Alley\WP\Alleyvate\Features;

use Alley\WP\Alleyvate\Feature;
use Mantle\Testkit\Test_Case;
use Mantle\Testing\Concerns\{Admin_Screen, Refresh_Database};

/**
* Tests for fully disabling comment functionality.
*/
final class Test_Disable_Comments extends Test_Case {
use Concerns\Remove_Meta_Box;
use \Mantle\Testing\Concerns\Admin_Screen;
use \Mantle\Testing\Concerns\Refresh_Database;
use Admin_Screen;
use Refresh_Database;

/**
* Feature instance.
*
* @var Feature
* @var Disable_Comments
*/
private Feature $feature;
private Disable_Comments $feature;

/**
* Set up.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
* @package wp-alleyvate
*/

declare( strict_types=1 );

namespace Alley\WP\Alleyvate\Features;

use Alley\WP\Alleyvate\Feature;
use Mantle\Testkit\Test_Case;

/**
Expand All @@ -24,9 +25,9 @@ final class Test_Disable_Custom_Fields_Meta_Box extends Test_Case {
/**
* Feature instance.
*
* @var Feature
* @var Disable_Custom_Fields_Meta_Box
*/
private Feature $feature;
private Disable_Custom_Fields_Meta_Box $feature;

/**
* Set up.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,26 @@
* @package wp-alleyvate
*/

declare( strict_types=1 );

namespace Alley\WP\Alleyvate\Features;

use Alley\WP\Alleyvate\Feature;
use Mantle\Testing\Concerns\{Admin_Screen, Refresh_Database};
use Mantle\Testkit\Test_Case;

/**
* Tests for disabling selected unpopular dashboard widgets.
*/
final class Test_Disable_Dashboard_Widgets extends Test_Case {
use \Mantle\Testing\Concerns\Admin_Screen;
use \Mantle\Testing\Concerns\Refresh_Database;
use Admin_Screen;
use Refresh_Database;

/**
* Feature instance.
*
* @var Feature
* @var Disable_Dashboard_Widgets
*/
private Feature $feature;
private Disable_Dashboard_Widgets $feature;

/**
* Set up.
Expand All @@ -44,7 +46,7 @@ protected function setUp(): void {
/**
* Test that widgets have been removed.
*/
public function test_action__disable_dashboard_widgets() {
public function test_action__disable_dashboard_widgets(): void {

// Load files required to get wp_meta_boxes global.
require_once ABSPATH . 'wp-admin/includes/misc.php';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,25 @@
* @package wp-alleyvate
*/

declare( strict_types=1 );

namespace Alley\WP\Alleyvate\Features;

use Alley\WP\Alleyvate\Feature;
use Mantle\Testing\Concerns\Refresh_Database;
use Mantle\Testkit\Test_Case;

/**
* Tests for disabling the password change notification.
*/
final class Test_Disable_Password_Change_Notification extends Test_Case {
use \Mantle\Testing\Concerns\Refresh_Database;
use Refresh_Database;

/**
* Feature instance.
*
* @var Feature
* @var Disable_Password_Change_Notification
*/
private Feature $feature;
private Disable_Password_Change_Notification $feature;

/**
* Set up.
Expand All @@ -38,9 +40,9 @@ protected function setUp(): void {
}

/**
* Test that the feature.
* Test disable password change notification hook.
*/
public function test_disable_password_change_notification_hook() {
public function test_disable_password_change_notification_hook(): void {
$this->acting_as( 'administrator' );

$this->assertTrue(
Expand Down
Loading

0 comments on commit a0639fc

Please sign in to comment.