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

235 lines
5.6 KiB
PHP
Raw Normal View History

2014-09-05 06:36:46 +00:00
<?php
/**
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
*/
2019-11-07 23:25:32 +00:00
/**
* WC_Tests_Notice_Functions class.
*/
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
*/
2019-11-07 23:25:32 +00:00
public function test_wc_notice_count() {
2014-09-05 06:36:46 +00:00
2019-11-07 23:25:32 +00:00
// No error notices.
$this->assertEquals( 0, wc_notice_count( 'error' ) );
2014-09-05 06:36:46 +00:00
2019-11-07 23:25:32 +00:00
// Single notice.
2014-09-05 06:36:46 +00:00
wc_add_notice( 'Bogus Notice', 'success' );
$this->assertEquals( 1, wc_notice_count() );
2019-11-07 23:25:32 +00:00
// Specific notice.
2014-09-05 06:36:46 +00:00
wc_add_notice( 'Bogus Error Notice', 'error' );
$this->assertEquals( 1, wc_notice_count( 'error' ) );
2019-11-07 23:25:32 +00:00
// Multiple notices of different types.
2018-01-10 12:43:48 +00:00
wc_clear_notices();
wc_add_notice( 'Bogus 1', 'success' );
wc_add_notice( 'Bogus 2', 'success' );
wc_add_notice( 'Bogus Notice 1', 'notice' );
wc_add_notice( 'Bogus Notice 2', 'notice' );
wc_add_notice( 'Bogus Error Notice 1', 'error' );
2014-09-05 06:36:46 +00:00
wc_add_notice( 'Bogus Error Notice 2', 'error' );
2018-01-10 12:43:48 +00:00
$this->assertEquals( 6, wc_notice_count() );
// repeat with duplicates.
wc_add_notice( 'Bogus 1', 'success' );
wc_add_notice( 'Bogus 2', 'success' );
wc_add_notice( 'Bogus Notice 1', 'notice' );
wc_add_notice( 'Bogus Notice 2', 'notice' );
wc_add_notice( 'Bogus Error Notice 1', 'error' );
wc_add_notice( 'Bogus Error Notice 2', 'error' );
$this->assertEquals( 12, wc_notice_count() );
2014-09-05 06:36:46 +00:00
}
/**
2015-11-03 13:31:20 +00:00
* Test wc_has_notice().
2014-09-05 06:36:46 +00:00
*
* @since 2.2
*/
2019-11-07 23:25:32 +00:00
public function test_wc_has_notice() {
2014-09-05 06:36:46 +00:00
2019-11-07 23:25:32 +00:00
// Negative.
2014-09-05 06:36:46 +00:00
wc_add_notice( 'Bogus Notice', 'success' );
$this->assertFalse( wc_has_notice( 'Legit Notice' ) );
2019-11-07 23:25:32 +00:00
// Positive.
2014-09-05 06:36:46 +00:00
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
*/
2019-11-07 23:25:32 +00:00
public function test_wc_add_notice() {
2014-09-05 06:36:46 +00:00
2019-11-07 23:25:32 +00:00
// Default type.
2014-09-05 06:36:46 +00:00
wc_add_notice( 'Test Notice' );
$notices = wc_get_notices();
$this->assertArrayHasKey( 'success', $notices );
2019-11-07 23:25:32 +00:00
$this->assertEquals( 'Test Notice', $notices['success'][0]['notice'] );
2014-09-05 06:36:46 +00:00
2019-11-07 23:25:32 +00:00
// Clear notices.
2014-09-05 06:36:46 +00:00
WC()->session->set( 'wc_notices', null );
2019-11-07 23:25:32 +00:00
// Specific type.
wc_add_notice( 'Test Error Notice', 'error', array( 'id' => 'billing_postcode' ) );
2014-09-05 06:36:46 +00:00
$notices = wc_get_notices();
$this->assertArrayHasKey( 'error', $notices );
2019-11-07 23:25:32 +00:00
$this->assertEquals( 'Test Error Notice', $notices['error'][0]['notice'] );
$this->assertEquals( array( 'id' => 'billing_postcode' ), $notices['error'][0]['data'] );
2014-09-05 06:36:46 +00:00
}
/**
2015-11-03 13:31:20 +00:00
* Test wc_clear_notices().
2014-09-05 06:36:46 +00:00
*
* @since 2.2
*/
2019-11-07 23:25:32 +00:00
public function test_wc_clear_notices() {
2014-09-05 06:36:46 +00:00
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' );
wc_add_notice( 'Second True Notice', 'notice', array( 'id' => 'second_notice' ) );
2014-09-05 06:36:46 +00:00
$this->expectOutputString( '<div class="woocommerce-info">One True Notice</div><div class="woocommerce-info" data-id="second_notice">Second True Notice</div>' );
2014-09-05 06:36:46 +00:00
wc_print_notices();
$this->assertEmpty( WC()->session->get( 'wc_notices' ) );
}
/**
* Test wc_print_notices() should return notices
* when first parameter is set to true.
*/
public function test_wc_print_notices_should_return_notices() {
$expected_return = "\n <div class=\"woocommerce-info\">\n One True Notice </div>\n";
wc_add_notice( 'One True Notice', 'notice' );
$this->assertEquals( $expected_return, wc_print_notices( true ) );
}
2014-09-05 06:36:46 +00:00
/**
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() {
2017-12-12 18:29:20 +00:00
$this->expectOutputString( '<div class="woocommerce-message" role="alert">Success!</div>' );
2014-09-05 06:36:46 +00:00
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() {
2019-11-07 23:25:32 +00:00
// Specific type.
2017-12-12 18:29:20 +00:00
$this->expectOutputString( '<ul class="woocommerce-error" role="alert"><li>Error!</li></ul>' );
2014-09-05 06:36:46 +00:00
wc_print_notice( 'Error!', 'error' );
}
2019-11-07 23:25:32 +00:00
/**
* Test wc_print_notice() w/ data.
*
* @since 2.2
*/
public function test_wc_print_notice_data() {
// Specific type.
2019-11-07 23:34:49 +00:00
$this->expectOutputString( '<ul class="woocommerce-error" role="alert"><li data-id="billing_postcode">Error!</li></ul>' );
2019-11-07 23:25:32 +00:00
wc_print_notice( 'Error!', 'error', array( 'id' => 'billing_postcode' ) );
}
2014-09-05 06:36:46 +00:00
/**
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() {
2019-11-07 23:25:32 +00:00
// No notices.
2014-09-05 06:36:46 +00:00
$notices = wc_get_notices();
$this->assertInternalType( 'array', $notices );
$this->assertEmpty( $notices );
2019-11-07 23:25:32 +00:00
// Default type.
2014-09-05 06:36:46 +00:00
wc_add_notice( 'Another Notice' );
2019-11-07 23:25:32 +00:00
$this->assertEquals(
array(
'success' => array(
array(
'notice' => 'Another Notice',
'data' => array(),
),
),
),
wc_get_notices()
);
// Specific type.
wc_add_notice( 'Error Notice', 'error', array( 'id' => 'billing_email' ) );
$this->assertEquals(
array(
array(
'notice' => 'Error Notice',
'data' => array( 'id' => 'billing_email' ),
),
),
wc_get_notices( 'error' )
);
// Invalid type.
2014-09-05 06:36:46 +00:00
$notices = wc_get_notices( 'bogus_type' );
$this->assertInternalType( 'array', $notices );
$this->assertEmpty( $notices );
}
}