Make WordPress Core

Ticket #14348: 14348-unit-test.diff

File 14348-unit-test.diff, 2.2 KB (added by kurtpayne, 13 years ago)

Unit test

  • tests/headRequest.php

     
     1<?php
     2
     3/**
     4 * Detect when wp_die was called
     5 */
     6class WPHeadDieException extends Exception {};
     7
     8/**
     9 * Some simple test cases for KSES post content filtering
     10 *
     11 * @group templates
     12 * @ticket 14348
     13 */
     14class Tests_HeadRequest extends WP_UnitTestCase {
     15
     16        /**
     17         * Set up the test fixture
     18         * Hook up the die handler override
     19         */
     20        public function setup() {
     21                $_SERVER['REQUEST_METHOD'] = 'HEAD';
     22                add_filter( 'wp_die_handler', array( $this, 'override_die' ), 99 );
     23        }
     24       
     25        /**
     26         * Tear down the test fixture
     27         * Unhook the die handler override
     28         */
     29        public function teardown() {
     30                remove_filter( 'wp_die_handler', array( $this, 'override_die'), 99 );
     31        }
     32       
     33        /**
     34         * Test that the HEAD requests die early
     35         */
     36        public function test_head_quits_early() {
     37                $this->setExpectedException( 'WPHeadDieException', '' );
     38                do_action( 'head_request' );
     39        }
     40       
     41        /**
     42         * Test that the page doesn't create any output
     43         */
     44        public function test_page_is_empty() {
     45                ob_start();
     46                try {
     47                        do_action( 'head_request' );
     48                        $this->fail( 'Did not die early' );
     49                } catch ( WPHeadDieException $e ) {
     50                }
     51                $contents = ob_get_clean();
     52                $this->assertEmpty( $contents );
     53        }
     54       
     55        /**
     56         * Test the exit_on_http_head filter
     57         */
     58        public function test_exit_on_http_head_filter() {
     59                ob_start();
     60                add_filter( 'exit_on_http_head', '__return_false' );
     61                try {
     62                        do_action( 'head_request' );
     63                        remove_filter( 'exit_on_http_head', '__return_false' );
     64                        $this->assertTrue( true );
     65                } catch ( WPHeadDieException $e ) {
     66                        remove_filter( 'exit_on_http_head', '__return_false' );
     67                        $this->fail( 'Did not die early' );
     68                }
     69                ob_end_clean();
     70        }
     71
     72        /**
     73         * Shim in our die handler
     74         * @return callable
     75         */
     76        public function override_die() {
     77                return array( $this, 'fake_wp_die' );
     78        }
     79       
     80        /**
     81         * This stops WP execution, but not test execution
     82         * @param string $message
     83         * @throws WPHeadDieException
     84         */
     85        public function fake_wp_die( $message ) {
     86                throw new WPHeadDieException( $message );
     87        }
     88}