woocommerce/tests/unit-tests/util/notice-functions.php

186 lines
4.0 KiB
PHP
Raw Normal View History

2014-09-05 06:36:46 +00:00
<?php
2015-03-06 15:32:40 +00:00
2014-09-05 06:36:46 +00:00
/**
2015-11-03 13:31:20 +00:00
* Class Notice_Functions.
2015-03-06 15:32:40 +00:00
* @package WooCommerce\Tests\Util
* @since 2.2
*/
class WC_Tests_Notice_Functions extends WC_Unit_Test_Case {
2014-09-05 06:36:46 +00:00
/**
2015-11-03 13:31:20 +00:00
* Clear out notices after each test.
2014-09-05 06:36:46 +00:00
*
* @since 2.2
*/
public function tearDown() {
WC()->session->set( 'wc_notices', null );
}
/**
2015-11-03 13:31:20 +00:00
* Test wc_notice_count().
2014-09-05 06:36:46 +00:00
*
* @since 2.2
*/
function test_wc_notice_count() {
// no error notices
$this->assertEquals( 0, wc_notice_count( 'error' ) );
2014-09-05 06:36:46 +00:00
// single notice
wc_add_notice( 'Bogus Notice', 'success' );
$this->assertEquals( 1, wc_notice_count() );
// specific notice
wc_add_notice( 'Bogus Error Notice', 'error' );
$this->assertEquals( 1, wc_notice_count( 'error' ) );
// multiple notices of different types
wc_add_notice( 'Bogus Notice 2', 'success' );
wc_add_notice( 'Bogus Error Notice 2', 'error' );
$this->assertEquals( 4, wc_notice_count() );
}
/**
2015-11-03 13:31:20 +00:00
* Test wc_has_notice().
2014-09-05 06:36:46 +00:00
*
* @since 2.2
*/
function test_wc_has_notice() {
// negative
wc_add_notice( 'Bogus Notice', 'success' );
$this->assertFalse( wc_has_notice( 'Legit Notice' ) );
// positive
wc_add_notice( 'One True Notice', 'notice' );
$this->assertTrue( wc_has_notice( 'One True Notice', 'notice' ) );
}
/**
2015-11-03 13:31:20 +00:00
* Test wc_notice_add_notice().
2014-09-05 06:36:46 +00:00
*
* @since 2.2
*/
function test_wc_add_notice() {
// default type
wc_add_notice( 'Test Notice' );
$notices = wc_get_notices();
$this->assertArrayHasKey( 'success', $notices );
$this->assertEquals( 'Test Notice', $notices['success'][0] );
// clear notices
WC()->session->set( 'wc_notices', null );
// specific type
wc_add_notice( 'Test Error Notice', 'error' );
$notices = wc_get_notices();
$this->assertArrayHasKey( 'error', $notices );
$this->assertEquals( 'Test Error Notice', $notices['error'][0] );
}
/**
2015-11-03 13:31:20 +00:00
* Test wc_clear_notices().
2014-09-05 06:36:46 +00:00
*
* @since 2.2
*/
function test_wc_clear_notices() {
wc_add_notice( 'Test Notice' );
wc_clear_notices();
$this->assertEmpty( WC()->session->get( 'wc_notices' ) );
}
/**
2015-11-03 13:31:20 +00:00
* Test wc_print_notices().
2014-09-05 06:36:46 +00:00
*
* @since 2.2
*/
public function test_wc_print_notices() {
wc_add_notice( 'One True Notice', 'notice' );
$this->expectOutputString( '<div class="woocommerce-info">One True Notice</div>' );
wc_print_notices();
$this->assertEmpty( WC()->session->get( 'wc_notices' ) );
}
/**
2015-11-03 13:31:20 +00:00
* Test actions that print the notices.
2014-09-05 06:36:46 +00:00
*
* @since 2.2
*/
public function test_wc_print_notices_actions() {
$this->assertNotFalse( has_action( 'woocommerce_before_shop_loop', 'wc_print_notices' ) );
$this->assertNotFalse( has_action( 'woocommerce_before_single_product', 'wc_print_notices' ) );
}
/**
2015-11-03 13:31:20 +00:00
* Test wc_print_notice() w/ success type.
2014-09-05 06:36:46 +00:00
*
* @since 2.2
*/
public function test_wc_print_success_notice() {
$this->expectOutputString( '<div class="woocommerce-message">Success!</div>' );
wc_print_notice( 'Success!' );
}
/**
2015-11-03 13:31:20 +00:00
* Test wc_print_notice() w/ notice type.
2014-09-05 06:36:46 +00:00
*
* @since 2.2
*/
public function test_wc_print_info_notice() {
$this->expectOutputString( '<div class="woocommerce-info">Info!</div>' );
wc_print_notice( 'Info!', 'notice' );
}
/**
2015-11-03 13:31:20 +00:00
* Test wc_print_notice() w/ error type.
2014-09-05 06:36:46 +00:00
*
* @since 2.2
*/
public function test_wc_print_error_notice() {
// specific type
$this->expectOutputString( '<ul class="woocommerce-error"><li>Error!</li></ul>' );
wc_print_notice( 'Error!', 'error' );
}
/**
2015-11-03 13:31:20 +00:00
* Test wc_get_notices().
2014-09-05 06:36:46 +00:00
*
* @since 2.2
*/
public function test_wc_get_notices() {
// no notices
$notices = wc_get_notices();
$this->assertInternalType( 'array', $notices );
$this->assertEmpty( $notices );
// default type
wc_add_notice( 'Another Notice' );
$this->assertEquals( array( 'success' => array( 'Another Notice' ) ), wc_get_notices() );
// specific type
wc_add_notice( 'Error Notice', 'error' );
$this->assertEquals( array( 'Error Notice' ), wc_get_notices( 'error' ) );
// invalid type
$notices = wc_get_notices( 'bogus_type' );
$this->assertInternalType( 'array', $notices );
$this->assertEmpty( $notices );
}
}