Skip to content

Commit

Permalink
added - unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveJonesDev committed Mar 4, 2024
1 parent 532d9ab commit ca6d06b
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 71 deletions.
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@
"composer/installers": "^1.12.0"
},
"autoload": {
"classmap": []
"classmap": [
"inc"
]
},
"autoload-dev": {
"classmap": []
Expand Down
79 changes: 9 additions & 70 deletions enable-contributor-uploads.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,80 +18,19 @@
* Text Domain: enable-contributor-uploads
*/

use EDECU\Inc\Plugin;

// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}

/**
* Class EnableContributorUploads
*
* Main plugin class.
*/
class EnableContributorUploads {

/**
* Constructor.
*/
public function __construct() {
// Intentionally left blank.
}

/**
* Initialize the plugin.
*
* Registers activation and deactivation hooks, and adds action to load text domain.
*/
public function init() {
register_activation_hook( __FILE__, array( $this, 'activate' ) );
register_deactivation_hook( __FILE__, array( $this, 'deactivate' ) );
add_action( 'init', array( $this, 'load_textdomain' ) );
}

/**
* Handles tasks to perform upon plugin activation.
*
* Adds the 'upload_files' capability to the 'contributor' role.
*/
public function activate() {
$contributor = get_role( 'contributor' );
if ( null !== $contributor ) {
$contributor->add_cap( 'upload_files' );
}
}

/**
* Handles tasks to perform upon plugin deactivation.
*
* Removes the 'upload_files' capability from the 'contributor' role.
*/
public function deactivate() {
$contributor = get_role( 'contributor' );
if ( null !== $contributor ) {
$contributor->remove_cap( 'upload_files' );
}
}

/**
* Load the plugin text domain for translation.
*/
public function load_textdomain() {
load_plugin_textdomain( 'enable-contributor-uploads', false, basename( __DIR__ ) . '/languages' );
}

/**
* Handles tasks to perform upon plugin uninstallation.
*
* Removes the 'upload_files' capability from the 'contributor' role.
* Note: This method should be called statically and is intended for registration with register_uninstall_hook.
*/
public static function uninstall() {
$contributor = get_role( 'contributor' );
if ( null !== $contributor ) {
$contributor->remove_cap( 'upload_files' );
}
}
// Autoloads classes from the 'inc' directory.
if ( file_exists( plugin_dir_path( __FILE__ ) . 'vendor/autoload.php' ) ) {
include_once plugin_dir_path( __FILE__ ) . 'vendor/autoload.php';
}

// Instantiating the class and calling the init method in one line.
( new EnableContributorUploads() )->init();
// Initialize the plugin.
if ( class_exists( 'EDAC\Inc\Plugin' ) ) {
( new Plugin() )->init();
}
78 changes: 78 additions & 0 deletions inc/class-plugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php
/**
* Class file for the Accessibility Checker plugin.
*
* @package Accessibility_Checker
*/

namespace EDECU\Inc;

/**
* Class Plugin
*
* Main plugin functionality class.
*/
class Plugin {

/**
* Constructor.
*/
public function __construct() {
// Intentionally left blank.
}

/**
* Initialize the plugin.
*
* Registers activation and deactivation hooks, and adds action to load text domain.
*/
public function init() {
register_activation_hook( __FILE__, array( $this, 'activate' ) );
register_deactivation_hook( __FILE__, array( $this, 'deactivate' ) );
add_action( 'init', array( $this, 'load_textdomain' ) );
}

/**
* Handles tasks to perform upon plugin activation.
*
* Adds the 'upload_files' capability to the 'contributor' role.
*/
public function activate() {
$contributor = get_role( 'contributor' );
if ( null !== $contributor ) {
$contributor->add_cap( 'upload_files' );
}
}

/**
* Handles tasks to perform upon plugin deactivation.
*
* Removes the 'upload_files' capability from the 'contributor' role.
*/
public function deactivate() {
$contributor = get_role( 'contributor' );
if ( null !== $contributor ) {
$contributor->remove_cap( 'upload_files' );
}
}

/**
* Load the plugin text domain for translation.
*/
public function load_textdomain() {
load_plugin_textdomain( 'enable-contributor-uploads', false, basename( __DIR__ ) . '/languages' );
}

/**
* Handles tasks to perform upon plugin uninstallation.
*
* Removes the 'upload_files' capability from the 'contributor' role.
* Note: This method should be called statically and is intended for registration with register_uninstall_hook.
*/
public static function uninstall() {
$contributor = get_role( 'contributor' );
if ( null !== $contributor ) {
$contributor->remove_cap( 'upload_files' );
}
}
}
93 changes: 93 additions & 0 deletions tests/phpunit/edecdu-plugin-test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php
/**
* Class EDECU_Plugin_Test
*
* @package Enable_Contributor_Uploads
*/

use EDECU\Inc\Plugin;

/**
* Plugin test case.
*/
class EDECU_Plugin_Test extends WP_UnitTestCase {

/**
* Plugin instance.
*
* @var Plugin
*/
private $plugin;

/**
* Test setup.
*/
public function setUp() {
parent::setUp();
$this->plugin = new Plugin();
}

/**
* Test teardown.
*/
public function tearDown() {
parent::tearDown();
unset( $this->plugin );
}

/**
* Test initialization of the plugin.
*/
public function test_init() {
// Simulate the plugin initialization.
$this->plugin->init();

// Assert that the textdomain is loaded.
$this->assertTrue( is_textdomain_loaded( 'enable-contributor-uploads' ), 'Text domain should be loaded' );
}

/**
* Test plugin activation.
*/
public function test_activate() {
// Activate the plugin.
$this->plugin->activate();

// Retrieve the 'contributor' role.
$contributor = get_role( 'contributor' );

// Assert that 'upload_files' capability is added to 'contributor' role.
$this->assertTrue( $contributor->has_cap( 'upload_files' ), 'Contributor role should have the upload_files capability after activation' );
}

/**
* Test plugin deactivation.
*/
public function test_deactivate() {
// First, activate the plugin to ensure 'contributor' has the 'upload_files' capability.
$this->plugin->activate();

// Then, deactivate the plugin.
$this->plugin->deactivate();

// Retrieve the 'contributor' role again.
$contributor = get_role( 'contributor' );

// Assert that 'upload_files' capability is removed from 'contributor' role.
$this->assertFalse( $contributor->has_cap( 'upload_files' ), 'Contributor role should not have the upload_files capability after deactivation' );
}

/**
* Test plugin uninstallation.
*/
public function test_uninstall() {
// Simulate plugin uninstallation.
Plugin::uninstall();

// Retrieve the 'contributor' role.
$contributor = get_role( 'contributor' );

// Assert that 'upload_files' capability is removed from 'contributor' role.
$this->assertFalse( $contributor->has_cap( 'upload_files' ), 'Contributor role should not have the upload_files capability after uninstallation' );
}
}

0 comments on commit ca6d06b

Please sign in to comment.