Unit Testing action hook - wordpress

I want to perform unit tests on a Class, my goal is: I want to check if the plugin is activated or not by using the function: is_plugin_active
class WC_Custom_Variable_Products_Dependencies {
public function __construct() {
add_action( 'admin_init', [$this, 'check_environment']);
}
public function check_environment(){
return is_plugin_active(
'woocommerce-custom-variable-products/woocommerce-custom-variable-products.php'
);
}
}
CLass de test :
require_once 'class-wc-custom-variable-products-dependencies.php';
class WC_Custom_Variable_Products_DependenciesTest extends WP_UnitTestCase {
public function setUp() {
parent::setUp();
$this->class_instance = new WC_Custom_Variable_Products_Dependencies();
}
public function test_check_environment(){
$result = $this->class_instance->check_environment();
$this->assertTrue($result);
}
The assertion return always False .
My plugin is activated, and the function is_plugin_active returns True if I execute it from the browser:
add_action('admin_init', function(){
var_dump(is_plugin_active(
'woocommerce-custom-variable-products/woocommerce-custom-variable-products.php'
));
});
I think the admin_init hook is not executed in the test. is it true or not?

I found out why. here is the solution: you have to activate the plugin in the tests / bootstrap.php file:
$GLOBALS[ 'wp_tests_options' ] = array(
'active_plugins' => array(
'YOUR-PLUGIN/YOUR-PLUGIN.php'
)
)

Related

register_rest_route is not passing me the callback function parameters

I have a class to register and execute my custom endpoints.
If I register my endpoints outside a class I can get the parameters of my endpoint in my callback function. But inside a class the parameters are not passing
Here the code:
class MYPLUGIN_RestAPI {
public function __construct() {
add_action('rest_api_init', array($this, register_routes));
}
public function register_routes (){
register_rest_route (
'myplugin/v1',
'/book/(?P<id>[a-z0-9 .\-]+)',
array(
'methods' => 'GET',
'callback' => array($this, "endpoint_book_cb")
)
);
}
public function endpoint_book_cb($data){
$result['code'] = 200;
$result['message'] = "Horrayyy!!!!!!!";
$result['data'] = $data;
return $result;
}
}
new MYPLUGIN_RestAPI();
Here what I get when I run this endpoint
{"code":200,"message":"Horrayyy!!!!!!!","data":{}}
Any Ideas why?
Found the solution:
public function wpfm_endpoints_cb(WP_REST_Request $request){
$result['code'] = 200;
$result['message'] = "Horrayyy!!!!!!!";
$result['data'] = $request->get_params();
return $result;
}

How to use add_action from outside a class

I am learning about wordpress code and have read a lot of posts about add_action, but haven't found one yet that fits my situation. I want to make use of a hook that exists in another WP plugin. In rough outline, it looks like this:
// This part is not my code
class CDH {
function do_stuff() {
// important stuff
do_action( 'hook_i_want_to_use', $parameter );
// more stuff
}
global $cdh;
$cdh = new CDH();
}
// My first attempt code:
add_action( 'hook_i_want_to_use', 'my_function', 10, 1 );
function my_function( $parameter ) {
echo $parameter;
}
// My second attempt code:
class my_CDH extends CDH {
public function __construct() {
add_action( 'hook_i_want_to_use', array( $this, 'my_function' ), 10, 1 );
}
function my_function( $parameter ) {
echo $parameter;
}
}
In both attempts, my_function() is never called. How do I hook on to the do_action in the instance $cdh?

Wordpress hook that will instantiate a class to be able to use get_post_types before publish_CPT hook

I have an issue where I need to instantiate a class in wordpress so that in the constructor I can use the function get_post_types and have that hook happen before the publish_post hook (which I assume is around the publish_CPT hooks).
Here is the code I have so far
class Transient_Delete {
/**
* #var array of all the different post types on the site
*/
private $postTypes;
/**
* #var array of wordpress hooks we will have to assemble to delete all possible transients
*/
private $wpHooks;
public static function init() {
$class = __CLASS__;
new $class;
}
public function __construct()
{
$this->postTypes = array_values( get_post_types(array(), 'names', 'and') );
$this->wpHooks = $this->setWpHooks($this->postTypes);
add_action('publish_alert', array($this, 'deleteAlertTest'));
}
private function setWpHooks($postTypes)
{
$hooks = array_map(function($postType) {
return 'publish_' . $postType;
}, $postTypes);
return $hooks;
}
private function deleteAlertTest($post)
{
$postId = $post->ID;
echo 'test';
}
}
add_action( 'wp_loaded', array( 'Transient_Delete', 'init' ));
Another note here is that this is in the mu-plugins directory.
note: "alert" of publish_alert is a custom post type.
Ok this was my fault, it looks like the hook publish_alert works fine if I change the deleteAlertTest function to public. Any idea on why having it be a private function has that effect? Its within the same class.

Can't Change property value through a method in a WordPress plugin

I'm trying to check if WooCommerce is active or not, I created a property with with a default value of false, then I created a method to check if WooCommerce is active using is_plugin_active() and admin_init hook, if active the value of the property must be updated to true: here is the code:
class MyClass{
public $woo_active = false;
public function __construct(){
add_action( 'admin_init', array( $this, 'check_if_woo_active' ) );
}
// check if WooCommerce is active
public function check_if_woo_active(){
if( is_plugin_active( 'woocommerce/woocommerce.php' ) ){
$this->woo_active = true;
}
}
// is_woo_active()
public function is_woo_active(){
return $this->woo_active;
}
}
$var = new MyClass();
var_dump( $var->is_woo_active() );
the issue is that var_dump returns false even if WooCommerce is active, BUT, if I use var_dump inside the function check_if_woo_active(), it returns true.
Why the property value is not updated? thanks
Updated:
The Second Solution as #helgatheviking sugested works fine, also this works very well and short
class MyClass{
// check if WooCommerce is active
public function is_woo_active(){
include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
if( is_plugin_active( 'woocommerce/woocommerce.php' ) ){
return true;
}else{
return false;
}
}
}
$var = new MyClass();
var_dump( $var->is_woo_active() );
If I had to guess, then $var = new MyClass(); is run before admin_init so the check_if_woo_active() isn't run.
Couple things you could do. First, I will usually launch my plugin on the woocommerce_loaded hook. That way I am 100% sure WooCommerce is running.
class MyClass{
protected static $instance = null;
/**
* Main MyClass Instance
*
* Ensures only one instance of MyClass is loaded or can be loaded.
*
* #static
* #see MyClass()
* #return MyClass - Main instance
* #since 0.1.0
*/
public static function instance() {
if ( ! isset( self::$instance ) && ! ( self::$instance instanceof MyClass ) ) {
self::$instance = new MyClass();
}
return self::$instance;
}
public function __construct(){
// Do what you want, WC is definitely active
}
}
/**
* Returns the main instance of class.
*
* #return MyClass
*/
function MyClass() {
return MyClass::instance();
}
// Launch the class if WooCommerce is loaded:
add_action( 'woocommerce_loaded', 'MyClass' );
You could also mimic what WooCommerce does with their premium plugins and check the option that stores the active plugins:
class MyClass{
private static $active_plugins;
public static function get_active_plugins() {
self::$active_plugins = (array) get_option( 'active_plugins', array() );
if ( is_multisite() ){
self::$active_plugins = array_merge( self::$active_plugins, get_site_option( 'active_sitewide_plugins', array() ) );
}
}
// check if WooCommerce is active
public static function check_if_woo_active() {
if ( ! self::$active_plugins ) self::get_active_plugins();
return in_array( 'woocommerce/woocommerce.php', self::$active_plugins ) || array_key_exists( 'woocommerce/woocommerce.php', self::$active_plugins );
}
}
var_dump( MyClass::check_if_woo_active() );

Symfony 2 + Doctrine 2 + PHPUnit 3.5: Serialization of closure exception

I tried to find something about this on Google but nothing came out. I have a TestCase class that inherits from WebTestCase, with some methods that I want to use in all my unit/functional tests:
<?php
namespace Application\FaxServerBundle\Test;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Doctrine\Common\DataFixtures\Loader;
use Doctrine\Common\DataFixtures\Executor\ORMExecutor;
use Doctrine\Common\DataFixtures\Purger\ORMPurger;
use Application\FaxServerBundle\DataFixtures\ORM\NetworkConfigurationData;
class TestCase extends WebTestCase
{
protected $kernel;
public function setUp()
{
parent::setUp();
}
public function getEm()
{
return $this->getService( 'doctrine.orm.entity_manager' );
}
public function getNetworkConfigurationRepository()
{
return $this->getEm()->getRepository( 'Application\FaxServerBundle\Entity\NetworkConfiguration' );
}
public function loadNetworkConfigurationFixtures()
{
$loader = new Loader();
$loader->addFixture( new NetworkConfigurationData() );
$this->loadFixtures( $loader );
}
public function loadFixtures( $loader )
{
$purger = new ORMPurger();
$executor = new ORMExecutor( $this->getEm(), $purger );
$executor->execute( $loader->getFixtures() );
}
protected function getService( $name, $kernel = null )
{
return $this->getBootedKernel()->getContainer()->get( $name );
}
protected function hasService( $name, $kernel = null )
{
return $this->getBootedKernel()->getContainer()->has( $name );
}
protected function getBootedKernel()
{
$this->kernel = $this->createKernel();
if ( !$this->kernel->isBooted() )
{
$this->kernel->boot();
}
return $this->kernel;
}
public function generateUrl( $client, $route, $parameters = array() )
{
return $client->getContainer()->get( 'router' )->generate( $route, $parameters );
}
}
Then, my unit test:
<?php
namespace Application\FaxServerBundle\Tests\Entity;
use Doctrine\ORM\AbstractQuery;
use Application\FaxServerBundle\Entity;
use Application\FaxServerBundle\Test\TestCase;
class NetworkConfigurationRepositoryTest extends TestCase
{
public function setUp()
{
parent::setUp();
$this->loadNetworkConfigurationFixtures();
}
public function testGetConfiguration()
{
$config = $this->getNetworkConfigurationRepository()->getConfigurationArray();
$this->assertInternalType( 'array', $config );
$this->assertEquals( 6, count( $config ) );
$this->assertArrayHasKey( 'id', $config );
$this->assertArrayHasKey( 'ip', $config );
$this->assertArrayHasKey( 'gateway', $config );
$this->assertArrayHasKey( 'subnetMask', $config );
$this->assertArrayHasKey( 'primaryDns', $config );
$this->assertArrayHasKey( 'secondaryDns', $config );
}
public function testGetConfigurationObject()
{
$config = $this->getNetworkConfigurationRepository()->getConfigurationObject();
$this->assertInternalType( 'object', $config );
}
public function testGetConfigurationArray()
{
$config = $this->getNetworkConfigurationRepository()->getConfigurationArray();
$this->assertInternalType( 'array', $config );
}
}
It was working before, but, suddenly, after I updated my vendors (doctrine included), it began to throw this exception:
3) Application\FaxServerBundle\Tests\Entity\NetworkConfigurationRepositoryTest::testGetConfigurationArray
RuntimeException: PHP Fatal error: Uncaught exception 'PDOException' with message 'You cannot serialize or unserialize PDO instances' in -:32
Stack trace:
#0 [internal function]: PDO->__sleep()
#1 -(32): serialize(Array)
#2 -(113): __phpunit_run_isolated_test()
#3 {main}
Next exception 'Exception' with message 'Serialization of 'Closure' is not allowed' in -:0
Stack trace:
#0 -(0): serialize()
#1 -(113): __phpunit_run_isolated_test()
#2 {main}
thrown in - on line 0
I've found that the problem comes from the fixture loading. If I remove the code that loads fixtures, it works.
Does anyone know what could be wrong in my code? Is this the best way of loading fixtures?
Thanks!
Not technically related to your issue. However, I had a really hard time trying to solve the "Serialization of 'Closure' is not allowed" issue while using PHPUnit, and this question is the top Google result.
The problem comes from the fact that PHPUnit serializes all the $GLOBALS in the system to essential back them up while the test is running. It then restores them after the test is done.
However, if you have any closures in your GLOBAL space, it's going to cause problems. There's two ways to solve it.
You can disable the global backup procedure totally by using an annotation.
/**
* #backupGlobals disabled
*/
class MyTest extends PHPUnit_Framework_TestCase
{
// ...
}
Or, if you know which variable is causing the problem (look for a lambda in var_dump($GLOBALS)), you can just blacklist the problem variable(s).
class MyTest extends PHPUnit_Framework_TestCase
{
protected $backupGlobalsBlacklist = array('application');
// ...
}
You can also try.
<phpunit backupGlobals="false">
<testsuites>
<testsuite name="Test">
<directory>.</directory>
</testsuite>
</testsuites>
</phpunit>

Resources