Changeset 60936 for trunk/tests/phpunit/tests/template.php
- Timestamp:
- 10/15/2025 05:12:32 PM (2 months ago)
- File:
-
- 1 edited
-
trunk/tests/phpunit/tests/template.php (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/template.php
r58136 r60936 64 64 } 65 65 66 /** 67 * @var string 68 */ 69 protected $original_default_mimetype; 70 66 71 public function set_up() { 67 72 parent::set_up(); 73 $this->original_default_mimetype = ini_get( 'default_mimetype' ); 68 74 register_post_type( 69 75 'cpt', … … 84 90 85 91 public function tear_down() { 92 ini_set( 'default_mimetype', $this->original_default_mimetype ); 86 93 unregister_post_type( 'cpt' ); 87 94 unregister_taxonomy( 'taxo' ); … … 493 500 switch_theme( $new_theme->get_stylesheet() ); 494 501 $this->assertSame( $new_theme->get_stylesheet_directory() . '/index.php', locate_template( $template_names ), 'Incorrect index template found in theme after switch.' ); 502 } 503 504 /** 505 * Tests that wp_start_template_enhancement_output_buffer() does not start a buffer when no filters are present. 506 * 507 * @ticket 43258 508 * @covers ::wp_should_output_buffer_template_for_enhancement 509 * @covers ::wp_start_template_enhancement_output_buffer 510 */ 511 public function test_wp_start_template_enhancement_output_buffer_without_filters_and_no_override(): void { 512 remove_all_filters( 'wp_template_enhancement_output_buffer' ); 513 $level = ob_get_level(); 514 $this->assertFalse( wp_should_output_buffer_template_for_enhancement(), 'Expected wp_should_output_buffer_template_for_enhancement() to return false when there are no wp_template_enhancement_output_buffer filters added.' ); 515 $this->assertFalse( wp_start_template_enhancement_output_buffer(), 'Expected wp_start_template_enhancement_output_buffer() to return false because the output buffer should not be started.' ); 516 $this->assertSame( 0, did_action( 'wp_template_enhancement_output_buffer_started' ), 'Expected the wp_template_enhancement_output_buffer_started action to not have fired.' ); 517 $this->assertSame( $level, ob_get_level(), 'Expected the initial output buffer level to be unchanged.' ); 518 } 519 520 /** 521 * Tests that wp_start_template_enhancement_output_buffer() does start a buffer when no filters are present but there is an override. 522 * 523 * @ticket 43258 524 * @covers ::wp_should_output_buffer_template_for_enhancement 525 * @covers ::wp_start_template_enhancement_output_buffer 526 */ 527 public function test_wp_start_template_enhancement_output_buffer_begins_without_filters_but_overridden(): void { 528 remove_all_filters( 'wp_template_enhancement_output_buffer' ); 529 $level = ob_get_level(); 530 add_filter( 'wp_should_output_buffer_template_for_enhancement', '__return_true' ); 531 $this->assertTrue( wp_should_output_buffer_template_for_enhancement(), 'Expected wp_should_output_buffer_template_for_enhancement() to return true when overridden with the wp_should_output_buffer_template_for_enhancement filter.' ); 532 $this->assertTrue( wp_start_template_enhancement_output_buffer(), 'Expected wp_start_template_enhancement_output_buffer() to return true because the output buffer should be started due to the override.' ); 533 $this->assertSame( 1, did_action( 'wp_template_enhancement_output_buffer_started' ), 'Expected the wp_template_enhancement_output_buffer_started action to have fired.' ); 534 $this->assertSame( $level + 1, ob_get_level(), 'Expected the output buffer level to have been incremented.' ); 535 ob_end_clean(); 536 } 537 538 /** 539 * Tests that wp_start_template_enhancement_output_buffer() does not start a buffer even when there are filters present due to override. 540 * 541 * @ticket 43258 542 * @covers ::wp_should_output_buffer_template_for_enhancement 543 * @covers ::wp_start_template_enhancement_output_buffer 544 */ 545 public function test_wp_start_template_enhancement_output_buffer_begins_with_filters_but_blocked(): void { 546 add_filter( 547 'wp_template_enhancement_output_buffer', 548 static function () { 549 return '<html>Hey!</html>'; 550 } 551 ); 552 $level = ob_get_level(); 553 add_filter( 'wp_should_output_buffer_template_for_enhancement', '__return_false' ); 554 $this->assertFalse( wp_should_output_buffer_template_for_enhancement(), 'Expected wp_should_output_buffer_template_for_enhancement() to return false since wp_should_output_buffer_template_for_enhancement was filtered to be false even though there is a wp_template_enhancement_output_buffer filter added.' ); 555 $this->assertFalse( wp_start_template_enhancement_output_buffer(), 'Expected wp_start_template_enhancement_output_buffer() to return false because the output buffer should not be started.' ); 556 $this->assertSame( 0, did_action( 'wp_template_enhancement_output_buffer_started' ), 'Expected the wp_template_enhancement_output_buffer_started action to not have fired.' ); 557 $this->assertSame( $level, ob_get_level(), 'Expected the initial output buffer level to be unchanged.' ); 558 } 559 560 /** 561 * Tests that wp_start_template_enhancement_output_buffer() starts the expected output buffer and that the expected hooks fire for 562 * an HTML document and that the response is not incrementally flushable. 563 * 564 * @ticket 43258 565 * @covers ::wp_start_template_enhancement_output_buffer 566 * @covers ::wp_finalize_template_enhancement_output_buffer 567 */ 568 public function test_wp_start_template_enhancement_output_buffer_for_html(): void { 569 // Start a wrapper output buffer so that we can flush the inner buffer. 570 ob_start(); 571 572 $filter_args = null; 573 add_filter( 574 'wp_template_enhancement_output_buffer', 575 static function ( string $buffer ) use ( &$filter_args ): string { 576 $filter_args = func_get_args(); 577 578 $p = WP_HTML_Processor::create_full_parser( $buffer ); 579 while ( $p->next_tag() ) { 580 echo $p->get_tag() . PHP_EOL; 581 switch ( $p->get_tag() ) { 582 case 'HTML': 583 $p->set_attribute( 'lang', 'es' ); 584 break; 585 case 'TITLE': 586 $p->set_modifiable_text( 'Saludo' ); 587 break; 588 case 'H1': 589 if ( $p->next_token() && '#text' === $p->get_token_name() ) { 590 $p->set_modifiable_text( '¡Hola, mundo!' ); 591 } 592 break; 593 } 594 } 595 return $p->get_updated_html(); 596 }, 597 10, 598 PHP_INT_MAX 599 ); 600 601 $initial_ob_level = ob_get_level(); 602 $this->assertTrue( wp_start_template_enhancement_output_buffer(), 'Expected wp_start_template_enhancement_output_buffer() to return true indicating the output buffer started.' ); 603 $this->assertSame( 1, did_action( 'wp_template_enhancement_output_buffer_started' ), 'Expected the wp_template_enhancement_output_buffer_started action to have fired.' ); 604 $this->assertSame( $initial_ob_level + 1, ob_get_level(), 'Expected the output buffer level to have been incremented' ); 605 606 ?> 607 <!DOCTYPE html> 608 <html lang="en"> 609 <head> 610 <title>Greeting</title> 611 </head> 612 <?php 613 $this->assertFalse( 614 @ob_flush(), // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged 615 'Expected output buffer to not be incrementally flushable.' 616 ); 617 ?> 618 <body> 619 <h1>Hello World!</h1> 620 </body> 621 </html> 622 <?php 623 624 $ob_status = ob_get_status(); 625 $this->assertSame( 'wp_finalize_template_enhancement_output_buffer', $ob_status['name'], 'Expected name to be WP function.' ); 626 $this->assertSame( 1, $ob_status['type'], 'Expected type to be user supplied handler.' ); 627 $this->assertSame( 0, $ob_status['chunk_size'], 'Expected unlimited chunk size.' ); 628 629 ob_end_flush(); // End the buffer started by wp_start_template_enhancement_output_buffer(). 630 $this->assertSame( $initial_ob_level, ob_get_level(), 'Expected the output buffer to be back at the initial level.' ); 631 632 $this->assertIsArray( $filter_args, 'Expected the wp_template_enhancement_output_buffer filter to have applied.' ); 633 $this->assertCount( 2, $filter_args, 'Expected two args to be supplied to the wp_template_enhancement_output_buffer filter.' ); 634 $this->assertIsString( $filter_args[0], 'Expected the $filtered_output param to the wp_template_enhancement_output_buffer filter to be a string.' ); 635 $this->assertIsString( $filter_args[1], 'Expected the $output param to the wp_template_enhancement_output_buffer filter to be a string.' ); 636 $this->assertSame( $filter_args[1], $filter_args[0], 'Expected the initial $filtered_output to match $output in the wp_template_enhancement_output_buffer filter.' ); 637 $original_output = $filter_args[0]; 638 $this->assertStringContainsString( '<!DOCTYPE html>', $original_output, 'Expected original output to contain string.' ); 639 $this->assertStringContainsString( '<html lang="en">', $original_output, 'Expected original output to contain string.' ); 640 $this->assertStringContainsString( '<title>Greeting</title>', $original_output, 'Expected original output to contain string.' ); 641 $this->assertStringContainsString( '<h1>Hello World!</h1>', $original_output, 'Expected original output to contain string.' ); 642 $this->assertStringContainsString( '</html>', $original_output, 'Expected original output to contain string.' ); 643 644 $processed_output = ob_get_clean(); // Obtain the output via the wrapper output buffer. 645 $this->assertIsString( $processed_output ); 646 $this->assertNotEquals( $original_output, $processed_output ); 647 648 $this->assertStringContainsString( '<!DOCTYPE html>', $processed_output, 'Expected processed output to contain string.' ); 649 $this->assertStringContainsString( '<html lang="es">', $processed_output, 'Expected processed output to contain string.' ); 650 $this->assertStringContainsString( '<title>Saludo</title>', $processed_output, 'Expected processed output to contain string.' ); 651 $this->assertStringContainsString( '<h1>¡Hola, mundo!</h1>', $processed_output, 'Expected processed output to contain string.' ); 652 $this->assertStringContainsString( '</html>', $processed_output, 'Expected processed output to contain string.' ); 653 } 654 655 /** 656 * Tests that wp_start_template_enhancement_output_buffer() starts the expected output buffer but ending with cleaning prevents any processing. 657 * 658 * @ticket 43258 659 * @covers ::wp_start_template_enhancement_output_buffer 660 * @covers ::wp_finalize_template_enhancement_output_buffer 661 */ 662 public function test_wp_start_template_enhancement_output_buffer_ended_cleaned(): void { 663 // Start a wrapper output buffer so that we can flush the inner buffer. 664 ob_start(); 665 666 $applied_filter = false; 667 add_filter( 668 'wp_template_enhancement_output_buffer', 669 static function ( string $buffer ) use ( &$applied_filter ): string { 670 $applied_filter = true; 671 672 $p = WP_HTML_Processor::create_full_parser( $buffer ); 673 if ( $p->next_tag( array( 'tag_name' => 'TITLE' ) ) ) { 674 $p->set_modifiable_text( 'Processed' ); 675 } 676 return $p->get_updated_html(); 677 } 678 ); 679 680 $initial_ob_level = ob_get_level(); 681 $this->assertTrue( wp_start_template_enhancement_output_buffer(), 'Expected wp_start_template_enhancement_output_buffer() to return true indicating the output buffer started.' ); 682 $this->assertSame( 1, did_action( 'wp_template_enhancement_output_buffer_started' ), 'Expected the wp_template_enhancement_output_buffer_started action to have fired.' ); 683 $this->assertSame( $initial_ob_level + 1, ob_get_level(), 'Expected the output buffer level to have been incremented' ); 684 685 ?> 686 <!DOCTYPE html> 687 <html lang="en"> 688 <head> 689 <title>Unprocessed</title> 690 </head> 691 <body> 692 <h1>Hello World!</h1> 693 <!-- ... --> 694 <?php ob_end_clean(); // Clean and end the buffer started by wp_start_template_enhancement_output_buffer(). ?> 695 <!DOCTYPE html> 696 <html lang="en"> 697 <head> 698 <title>Output Buffer Not Processed</title> 699 </head> 700 <body> 701 <h1>Template rendering aborted!!!</h1> 702 </body> 703 </html> 704 <?php 705 706 $this->assertSame( $initial_ob_level, ob_get_level(), 'Expected the output buffer to be back at the initial level.' ); 707 708 $this->assertFalse( $applied_filter, 'Expected the wp_template_enhancement_output_buffer filter to not have applied.' ); 709 $this->assertSame( 0, did_action( 'wp_final_template_output_buffer' ), 'Expected the wp_final_template_output_buffer action to not have fired.' ); 710 711 // Obtain the output via the wrapper output buffer. 712 $output = ob_get_clean(); 713 $this->assertIsString( $output, 'Expected ob_get_clean() to return a string.' ); 714 $this->assertStringNotContainsString( '<title>Unprocessed</title>', $output, 'Expected output buffer to not have string since the template was overridden.' ); 715 $this->assertStringNotContainsString( '<title>Processed</title>', $output, 'Expected output buffer to not have string since the filter did not apply.' ); 716 $this->assertStringContainsString( '<title>Output Buffer Not Processed</title>', $output, 'Expected output buffer to have string since the output buffer was ended with cleaning.' ); 717 } 718 719 /** 720 * Tests that wp_start_template_enhancement_output_buffer() starts the expected output buffer and cleaning allows the template to be replaced. 721 * 722 * @ticket 43258 723 * @covers ::wp_start_template_enhancement_output_buffer 724 * @covers ::wp_finalize_template_enhancement_output_buffer 725 */ 726 public function test_wp_start_template_enhancement_output_buffer_cleaned_and_replaced(): void { 727 // Start a wrapper output buffer so that we can flush the inner buffer. 728 ob_start(); 729 730 $called_filter = false; 731 add_filter( 732 'wp_template_enhancement_output_buffer', 733 static function ( string $buffer ) use ( &$called_filter ): string { 734 $called_filter = true; 735 736 $p = WP_HTML_Processor::create_full_parser( $buffer ); 737 if ( $p->next_tag( array( 'tag_name' => 'TITLE' ) ) ) { 738 $p->set_modifiable_text( 'Processed' ); 739 } 740 return $p->get_updated_html(); 741 } 742 ); 743 744 $initial_ob_level = ob_get_level(); 745 $this->assertTrue( wp_start_template_enhancement_output_buffer(), 'Expected wp_start_template_enhancement_output_buffer() to return true indicating the output buffer started.' ); 746 $this->assertSame( 1, did_action( 'wp_template_enhancement_output_buffer_started' ), 'Expected the wp_template_enhancement_output_buffer_started action to have fired.' ); 747 $this->assertSame( $initial_ob_level + 1, ob_get_level(), 'Expected the output buffer level to have been incremented.' ); 748 749 ?> 750 <!DOCTYPE html> 751 <html lang="en"> 752 <head> 753 <title>Unprocessed</title> 754 </head> 755 <body> 756 <h1>Hello World!</h1> 757 <!-- ... --> 758 <?php ob_clean(); // Clean the buffer started by wp_start_template_enhancement_output_buffer(), allowing the following document to replace the above.. ?> 759 <!DOCTYPE html> 760 <html lang="en"> 761 <head> 762 <title>Template Replaced</title> 763 </head> 764 <body> 765 <h1>Template Replaced</h1> 766 <p>The original template called <code>ob_clean()</code> which allowed this template to take its place.</p> 767 </body> 768 </html> 769 <?php 770 771 ob_end_flush(); // End the buffer started by wp_start_template_enhancement_output_buffer(). 772 $this->assertSame( $initial_ob_level, ob_get_level(), 'Expected the output buffer to be back at the initial level.' ); 773 774 $this->assertTrue( $called_filter, 'Expected the wp_template_enhancement_output_buffer filter to have applied.' ); 775 776 // Obtain the output via the wrapper output buffer. 777 $output = ob_get_clean(); 778 $this->assertIsString( $output, 'Expected ob_get_clean() to return a string.' ); 779 $this->assertStringNotContainsString( '<title>Unprocessed</title>', $output, 'Expected output buffer to not have string due to template override.' ); 780 $this->assertStringContainsString( '<title>Processed</title>', $output, 'Expected output buffer to have string due to filtering.' ); 781 $this->assertStringContainsString( '<h1>Template Replaced</h1>', $output, 'Expected output buffer to have string due to replaced template.' ); 782 } 783 784 /** 785 * Tests that wp_start_template_enhancement_output_buffer() starts the expected output buffer and that the output buffer is not processed. 786 * 787 * @ticket 43258 788 * @covers ::wp_start_template_enhancement_output_buffer 789 * @covers ::wp_finalize_template_enhancement_output_buffer 790 */ 791 public function test_wp_start_template_enhancement_output_buffer_for_json(): void { 792 // Start a wrapper output buffer so that we can flush the inner buffer. 793 ob_start(); 794 795 $mock_filter_callback = new MockAction(); 796 add_filter( 'wp_template_enhancement_output_buffer', array( $mock_filter_callback, 'filter' ) ); 797 798 $initial_ob_level = ob_get_level(); 799 $this->assertTrue( wp_start_template_enhancement_output_buffer(), 'Expected wp_start_template_enhancement_output_buffer() to return true indicating the output buffer started.' ); 800 $this->assertSame( 1, did_action( 'wp_template_enhancement_output_buffer_started' ), 'Expected the wp_template_enhancement_output_buffer_started action to have fired.' ); 801 $this->assertSame( $initial_ob_level + 1, ob_get_level(), 'Expected the output buffer level to have been incremented.' ); 802 803 ini_set( 'default_mimetype', 'application/json' ); // Since sending a header won't work. 804 $json = wp_json_encode( 805 array( 806 'success' => true, 807 'data' => array( 808 'message' => 'Hello, world!', 809 'fish' => '<o><', // Something that looks like HTML. 810 ), 811 ) 812 ); 813 echo $json; 814 815 $ob_status = ob_get_status(); 816 $this->assertSame( 'wp_finalize_template_enhancement_output_buffer', $ob_status['name'], 'Expected name to be WP function.' ); 817 $this->assertSame( 1, $ob_status['type'], 'Expected type to be user supplied handler.' ); 818 $this->assertSame( 0, $ob_status['chunk_size'], 'Expected unlimited chunk size.' ); 819 820 ob_end_flush(); // End the buffer started by wp_start_template_enhancement_output_buffer(). 821 $this->assertSame( $initial_ob_level, ob_get_level(), 'Expected the output buffer to be back at the initial level.' ); 822 823 $this->assertSame( 0, $mock_filter_callback->get_call_count(), 'Expected the wp_template_enhancement_output_buffer filter to not have applied.' ); 824 825 // Obtain the output via the wrapper output buffer. 826 $output = ob_get_clean(); 827 $this->assertIsString( $output, 'Expected ob_get_clean() to return a string.' ); 828 $this->assertSame( $json, $output, 'Expected output to not be processed.' ); 495 829 } 496 830
Note: See TracChangeset
for help on using the changeset viewer.