Skip to content

Commit

Permalink
Add test to demonstrate the issue.
Browse files Browse the repository at this point in the history
  • Loading branch information
costdev committed Jun 7, 2024
1 parent 1c8733e commit 013005a
Showing 1 changed file with 92 additions and 0 deletions.
92 changes: 92 additions & 0 deletions tests/phpunit/tests/admin/includes/wpGetPluginActionButton.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

/**
* Tests for wp_get_plugin_action_button().
*
* @group plugins
* @group admin
*
* @covers ::wp_get_plugin_action_button
*/
class Tests_Admin_Includes_WpGetPluginActionButton extends WP_UnitTestCase {

/**
* Administrator user ID.
*
* @var int
*/
private static $admin_user_id;

/**
* Test plugin data.
*
* @var stdClass
*/
private static $test_plugin;

/**
* Sets up properties and adds a test plugin before any tests run.
*/
public static function set_up_before_class() {
parent::set_up_before_class();

require_once ABSPATH . 'wp-admin/includes/plugin-install.php';

self::$admin_user_id = self::factory()->user->create( array( 'role' => 'administrator' ) );
self::$test_plugin = (object) array(
'name' => 'My Plugin',
'slug' => 'my-plugin',
'version' => '1.0.0',
);

mkdir( WP_PLUGIN_DIR . '/' . self::$test_plugin->slug );
file_put_contents(
WP_PLUGIN_DIR . '/' . self::$test_plugin->slug . '/my_plugin.php',
"<?php\n/**\n* Plugin Name: " . self::$test_plugin->name . "\n* Version: " . self::$test_plugin->version . "\n*/"
);
}

/**
* Removes the test plugin and its directory after all tests run.
*/
public static function tear_down_after_class() {
parent::tear_down_after_class();

unlink( WP_PLUGIN_DIR . '/' . self::$test_plugin->slug . '/my_plugin.php' );
rmdir( WP_PLUGIN_DIR . '/' . self::$test_plugin->slug );
}

/**
* Tests that an empty string is returned when the user does not have the
* correct capabilities.
*
* @ticket
*/
public function test_should_return_empty_string_without_proper_capabilities() {
$actual = wp_get_plugin_action_button(
self::$test_plugin->name,
self::$test_plugin,
true,
true
);
$this->assertSame( '', $actual, 'No button markup should be returned.' );
}

/**
* Tests that an empty string is not returned when the user has the
* correct capabilities.
*
* @ticket
*/
public function test_should_not_return_empty_string_with_proper_capabilities() {
wp_set_current_user( self::$admin_user_id );

$actual = wp_get_plugin_action_button(
self::$test_plugin->name,
self::$test_plugin,
true,
true
);
$this->assertNotSame( '', $actual, 'Button markup should be returned.' );
}
}

0 comments on commit 013005a

Please sign in to comment.