Changeset 42343 for trunk/tests/phpunit/tests/widgets/text-widget.php
- Timestamp:
- 11/30/2017 11:09:33 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/widgets/text-widget.php
r41361 r42343 37 37 parent::clean_up_global_scope(); 38 38 $wp_scripts = null; 39 $wp_styles = null;39 $wp_styles = null; 40 40 } 41 41 … … 76 76 function test_widget() { 77 77 $widget = new WP_Widget_Text(); 78 $text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.\n Praesent ut turpis consequat lorem volutpat bibendum vitae vitae ante.";78 $text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.\n Praesent ut turpis consequat lorem volutpat bibendum vitae vitae ante."; 79 79 80 80 $args = array( … … 107 107 // Test with filter=true, implicit legacy mode. 108 108 $this->widget_text_content_args = null; 109 $instance = array(109 $instance = array( 110 110 'title' => 'Foo', 111 111 'text' => $text, … … 127 127 // Test with filter=content, the upgraded widget, in 4.8.0 only. 128 128 $this->widget_text_content_args = null; 129 $instance = array(129 $instance = array( 130 130 'title' => 'Foo', 131 131 'text' => $text, 132 132 'filter' => 'content', 133 133 ); 134 $expected_instance = array_merge( $instance, array( 135 'filter' => true, 136 'visual' => true, 137 ) ); 134 $expected_instance = array_merge( 135 $instance, array( 136 'filter' => true, 137 'visual' => true, 138 ) 139 ); 138 140 ob_start(); 139 141 $widget->widget( $args, $instance ); … … 153 155 // Test with filter=true&visual=true, the upgraded widget, in 4.8.1 and above. 154 156 $this->widget_text_content_args = null; 155 $instance = array(157 $instance = array( 156 158 'title' => 'Foo', 157 159 'text' => $text, … … 159 161 'visual' => true, 160 162 ); 161 $expected_instance = $instance;163 $expected_instance = $instance; 162 164 ob_start(); 163 165 $widget->widget( $args, $instance ); … … 177 179 // Test with filter=true&visual=true, the upgraded widget, in 4.8.1 and above. 178 180 $this->widget_text_content_args = null; 179 $instance = array(181 $instance = array( 180 182 'title' => 'Foo', 181 183 'text' => $text, … … 183 185 'visual' => false, 184 186 ); 185 $expected_instance = $instance;187 $expected_instance = $instance; 186 188 ob_start(); 187 189 $widget->widget( $args, $instance ); … … 198 200 // Test with filter=false&visual=false, the upgraded widget, in 4.8.1 and above. 199 201 $this->widget_text_content_args = null; 200 $instance = array(202 $instance = array( 201 203 'title' => 'Foo', 202 204 'text' => $text, … … 204 206 'visual' => false, 205 207 ); 206 $expected_instance = $instance;208 $expected_instance = $instance; 207 209 ob_start(); 208 210 $widget->widget( $args, $instance ); … … 258 260 global $post; 259 261 $post_id = $this->factory()->post->create(); 260 $post = get_post( $post_id );261 262 $args = array(262 $post = get_post( $post_id ); 263 264 $args = array( 263 265 'before_title' => '<h2>', 264 266 'after_title' => "</h2>\n", … … 270 272 271 273 $base_instance = array( 272 'title' => 'Example',273 'text' => "This is an example:\n\n[example]\n\nHello.",274 'title' => 'Example', 275 'text' => "This is an example:\n\n[example]\n\nHello.", 274 276 'filter' => false, 275 277 ); 276 278 277 279 // Legacy Text Widget without wpautop. 278 $instance = array_merge( $base_instance, array( 279 'filter' => false, 280 ) ); 280 $instance = array_merge( 281 $base_instance, array( 282 'filter' => false, 283 ) 284 ); 281 285 $this->shortcode_render_count = 0; 282 286 ob_start(); … … 290 294 291 295 // Legacy Text Widget with wpautop. 292 $instance = array_merge( $base_instance, array( 293 'filter' => true, 294 'visual' => false, 295 ) ); 296 $instance = array_merge( 297 $base_instance, array( 298 'filter' => true, 299 'visual' => false, 300 ) 301 ); 296 302 $this->shortcode_render_count = 0; 297 303 ob_start(); … … 316 322 remove_filter( 'widget_text', 'do_shortcode' ); 317 323 318 $instance = array_merge( $base_instance, array( 319 'filter' => true, 320 'visual' => true, 321 ) ); 324 $instance = array_merge( 325 $base_instance, array( 326 'filter' => true, 327 'visual' => true, 328 ) 329 ); 322 330 323 331 // Visual Text Widget with only core-added widget_text_content filter for do_shortcode. … … 399 407 */ 400 408 function test_is_legacy_instance() { 401 $widget = new WP_Widget_Text();409 $widget = new WP_Widget_Text(); 402 410 $base_instance = array( 403 411 'title' => 'Title', 404 'text' => "Hello\n\nWorld", 405 ); 406 407 $instance = array_merge( $base_instance, array( 408 'visual' => false, 409 ) ); 412 'text' => "Hello\n\nWorld", 413 ); 414 415 $instance = array_merge( 416 $base_instance, array( 417 'visual' => false, 418 ) 419 ); 410 420 $this->assertTrue( $widget->is_legacy_instance( $instance ), 'Legacy when visual=false prop is present.' ); 411 421 412 $instance = array_merge( $base_instance, array( 413 'visual' => true, 414 ) ); 422 $instance = array_merge( 423 $base_instance, array( 424 'visual' => true, 425 ) 426 ); 415 427 $this->assertFalse( $widget->is_legacy_instance( $instance ), 'Not legacy when visual=true prop is present.' ); 416 428 417 $instance = array_merge( $base_instance, array( 418 'filter' => 'content', 419 ) ); 429 $instance = array_merge( 430 $base_instance, array( 431 'filter' => 'content', 432 ) 433 ); 420 434 $this->assertFalse( $widget->is_legacy_instance( $instance ), 'Not legacy when filter is explicitly content (in WP 4.8.0 only).' ); 421 435 422 $instance = array_merge( $base_instance, array( 423 'text' => '', 424 'filter' => true, 425 ) ); 436 $instance = array_merge( 437 $base_instance, array( 438 'text' => '', 439 'filter' => true, 440 ) 441 ); 426 442 $this->assertFalse( $widget->is_legacy_instance( $instance ), 'Not legacy when text is empty.' ); 427 443 428 $instance = array_merge( $base_instance, array( 429 'text' => "\nOne line", 430 'filter' => false, 431 ) ); 444 $instance = array_merge( 445 $base_instance, array( 446 'text' => "\nOne line", 447 'filter' => false, 448 ) 449 ); 432 450 $this->assertFalse( $widget->is_legacy_instance( $instance ), 'Not legacy when there is leading whitespace.' ); 433 451 434 $instance = array_merge( $base_instance, array( 435 'text' => "\nOne line\n\n", 436 'filter' => false, 437 ) ); 452 $instance = array_merge( 453 $base_instance, array( 454 'text' => "\nOne line\n\n", 455 'filter' => false, 456 ) 457 ); 438 458 $this->assertFalse( $widget->is_legacy_instance( $instance ), 'Not legacy when there is trailing whitespace.' ); 439 459 440 $instance = array_merge( $base_instance, array( 441 'text' => "One\nTwo", 442 'filter' => false, 443 ) ); 460 $instance = array_merge( 461 $base_instance, array( 462 'text' => "One\nTwo", 463 'filter' => false, 464 ) 465 ); 444 466 $this->assertTrue( $widget->is_legacy_instance( $instance ), 'Legacy when not-wpautop and there are line breaks.' ); 445 467 446 $instance = array_merge( $base_instance, array( 447 'text' => "One\n\nTwo", 448 'filter' => false, 449 ) ); 468 $instance = array_merge( 469 $base_instance, array( 470 'text' => "One\n\nTwo", 471 'filter' => false, 472 ) 473 ); 450 474 $this->assertTrue( $widget->is_legacy_instance( $instance ), 'Legacy when not-wpautop and there are paragraph breaks.' ); 451 475 452 $instance = array_merge( $base_instance, array( 453 'text' => "One\nTwo", 454 'filter' => true, 455 ) ); 476 $instance = array_merge( 477 $base_instance, array( 478 'text' => "One\nTwo", 479 'filter' => true, 480 ) 481 ); 456 482 $this->assertFalse( $widget->is_legacy_instance( $instance ), 'Not automatically legacy when wpautop and there are line breaks.' ); 457 483 458 $instance = array_merge( $base_instance, array( 459 'text' => "One\n\nTwo", 460 'filter' => true, 461 ) ); 484 $instance = array_merge( 485 $base_instance, array( 486 'text' => "One\n\nTwo", 487 'filter' => true, 488 ) 489 ); 462 490 $this->assertFalse( $widget->is_legacy_instance( $instance ), 'Not automatically legacy when wpautop and there are paragraph breaks.' ); 463 491 464 $instance = array_merge( $base_instance, array( 465 'text' => 'Test<!-- comment -->', 466 'filter' => true, 467 ) ); 492 $instance = array_merge( 493 $base_instance, array( 494 'text' => 'Test<!-- comment -->', 495 'filter' => true, 496 ) 497 ); 468 498 $this->assertTrue( $widget->is_legacy_instance( $instance ), 'Legacy when HTML comment is present.' ); 469 499 … … 482 512 ); 483 513 foreach ( $legacy_text_examples as $legacy_text_example ) { 484 $instance = array_merge( $base_instance, array( 485 'text' => $legacy_text_example, 486 'filter' => true, 487 ) ); 514 $instance = array_merge( 515 $base_instance, array( 516 'text' => $legacy_text_example, 517 'filter' => true, 518 ) 519 ); 488 520 $this->assertTrue( $widget->is_legacy_instance( $instance ), 'Legacy when wpautop and there is HTML that is not liable to be mutated.' ); 489 521 490 $instance = array_merge( $base_instance, array( 491 'text' => $legacy_text_example, 492 'filter' => false, 493 ) ); 522 $instance = array_merge( 523 $base_instance, array( 524 'text' => $legacy_text_example, 525 'filter' => false, 526 ) 527 ); 494 528 $this->assertTrue( $widget->is_legacy_instance( $instance ), 'Legacy when not-wpautop and there is HTML that is not liable to be mutated.' ); 495 529 } … … 507 541 ); 508 542 foreach ( $migratable_text_examples as $migratable_text_example ) { 509 $instance = array_merge( $base_instance, array( 510 'text' => $migratable_text_example, 511 'filter' => true, 512 ) ); 543 $instance = array_merge( 544 $base_instance, array( 545 'text' => $migratable_text_example, 546 'filter' => true, 547 ) 548 ); 513 549 $this->assertFalse( $widget->is_legacy_instance( $instance ), 'Legacy when wpautop and there is HTML that is not liable to be mutated.' ); 514 550 } … … 525 561 $widget->_set( 2 ); 526 562 $instance = array( 527 'title' => 'Title',528 'text' => 'Text',563 'title' => 'Title', 564 'text' => 'Text', 529 565 'filter' => false, 530 566 'visual' => false, … … 538 574 539 575 $instance = array( 540 'title' => 'Title',541 'text' => 'Text',576 'title' => 'Title', 577 'text' => 'Text', 542 578 'filter' => 'content', 543 579 ); … … 550 586 551 587 $instance = array( 552 'title' => 'Title',553 'text' => 'Text',588 'title' => 'Title', 589 'text' => 'Text', 554 590 'filter' => true, 555 591 ); … … 562 598 563 599 $instance = array( 564 'title' => 'Title',565 'text' => 'This is some HTML Code: <code><strong>BOLD!</strong></code>',600 'title' => 'Title', 601 'text' => 'This is some HTML Code: <code><strong>BOLD!</strong></code>', 566 602 'filter' => true, 567 603 'visual' => true, … … 578 614 add_filter( 'user_can_richedit', '__return_false' ); 579 615 $instance = array( 580 'title' => 'Title',581 'text' => 'Evil:</textarea><script>alert("XSS")</script>',616 'title' => 'Title', 617 'text' => 'Evil:</textarea><script>alert("XSS")</script>', 582 618 'filter' => true, 583 619 'visual' => true, … … 597 633 */ 598 634 function test_update() { 599 $widget = new WP_Widget_Text();600 $instance = array( 601 'title' => "The\nTitle",602 'text' => "The\n\nText",635 $widget = new WP_Widget_Text(); 636 $instance = array( 637 'title' => "The\nTitle", 638 'text' => "The\n\nText", 603 639 'filter' => true, 604 640 'visual' => true, 605 641 ); 606 642 607 wp_set_current_user( $this->factory()->user->create( array( 608 'role' => 'administrator', 609 ) ) ); 643 wp_set_current_user( 644 $this->factory()->user->create( 645 array( 646 'role' => 'administrator', 647 ) 648 ) 649 ); 610 650 611 651 $expected = array( … … 615 655 'visual' => true, 616 656 ); 617 $result = $widget->update( $instance, array() );657 $result = $widget->update( $instance, array() ); 618 658 $this->assertEquals( $expected, $result ); 619 659 $this->assertTrue( ! empty( $expected['filter'] ), 'Expected filter prop to be truthy, to handle case where 4.8 is downgraded to 4.7.' ); … … 623 663 $instance['text'] = '<script>alert( "Howdy!" );</script>'; 624 664 $expected['text'] = $instance['text']; 625 $result = $widget->update( $instance, array() );665 $result = $widget->update( $instance, array() ); 626 666 $this->assertEquals( $expected, $result, 'KSES should apply as expected.' ); 627 667 remove_filter( 'map_meta_cap', array( $this, 'grant_unfiltered_html_cap' ) ); … … 631 671 $instance['text'] = '<script>alert( "Howdy!" );</script>'; 632 672 $expected['text'] = wp_kses_post( $instance['text'] ); 633 $result = $widget->update( $instance, array() );673 $result = $widget->update( $instance, array() ); 634 674 $this->assertEquals( $expected, $result, 'KSES should not apply since user can unfiltered_html.' ); 635 675 remove_filter( 'map_meta_cap', array( $this, 'revoke_unfiltered_html_cap' ), 10 ); … … 646 686 // -- 647 687 $instance = array( 688 'title' => 'Legacy', 689 'text' => 'Text', 690 'filter' => false, 691 ); 692 $result = $widget->update( $instance, array() ); 693 $this->assertEquals( $instance, $result, 'Updating a widget without visual prop and explicit filter=false leaves visual prop absent' ); 694 695 // -- 696 $instance = array( 697 'title' => 'Legacy', 698 'text' => 'Text', 699 'filter' => true, 700 ); 701 $result = $widget->update( $instance, array() ); 702 $this->assertEquals( $instance, $result, 'Updating a widget without visual prop and explicit filter=true leaves legacy prop absent.' ); 703 704 // -- 705 $instance = array( 706 'title' => 'Legacy', 707 'text' => 'Text', 708 'visual' => true, 709 ); 710 $old_instance = array_merge( 711 $instance, array( 712 'filter' => false, 713 ) 714 ); 715 $expected = array_merge( 716 $instance, array( 717 'filter' => true, 718 ) 719 ); 720 $result = $widget->update( $instance, $old_instance ); 721 $this->assertEquals( $expected, $result, 'Updating a pre-existing widget with visual mode forces filter to be true.' ); 722 723 // -- 724 $instance = array( 725 'title' => 'Legacy', 726 'text' => 'Text', 727 'filter' => true, 728 ); 729 $old_instance = array_merge( 730 $instance, array( 731 'visual' => true, 732 ) 733 ); 734 $result = $widget->update( $instance, $old_instance ); 735 $expected = array_merge( 736 $instance, array( 737 'visual' => true, 738 ) 739 ); 740 $this->assertEquals( $expected, $result, 'Updating a pre-existing visual widget retains visual mode when updated.' ); 741 742 // -- 743 $instance = array( 648 744 'title' => 'Legacy', 649 'text' => 'Text', 745 'text' => 'Text', 746 ); 747 $old_instance = array_merge( 748 $instance, array( 749 'visual' => true, 750 ) 751 ); 752 $result = $widget->update( $instance, $old_instance ); 753 $expected = array_merge( 754 $instance, array( 755 'visual' => true, 756 'filter' => true, 757 ) 758 ); 759 $this->assertEquals( $expected, $result, 'Updating a pre-existing visual widget retains visual=true and supplies missing filter=true.' ); 760 761 // -- 762 $instance = array( 763 'title' => 'Legacy', 764 'text' => 'Text', 765 'visual' => true, 766 ); 767 $expected = array_merge( 768 $instance, array( 769 'filter' => true, 770 ) 771 ); 772 $result = $widget->update( $instance, array() ); 773 $this->assertEquals( $expected, $result, 'Updating a widget with explicit visual=true and absent filter prop causes filter to be set to true.' ); 774 775 // -- 776 $instance = array( 777 'title' => 'Legacy', 778 'text' => 'Text', 779 'visual' => false, 780 ); 781 $result = $widget->update( $instance, array() ); 782 $expected = array_merge( 783 $instance, array( 784 'filter' => false, 785 ) 786 ); 787 $this->assertEquals( $expected, $result, 'Updating a widget in legacy mode results in filter=false as if checkbox not checked.' ); 788 789 // -- 790 $instance = array( 791 'title' => 'Title', 792 'text' => 'Text', 650 793 'filter' => false, 651 794 ); 652 $result = $widget->update( $instance, array() ); 653 $this->assertEquals( $instance, $result, 'Updating a widget without visual prop and explicit filter=false leaves visual prop absent' ); 654 655 // -- 656 $instance = array( 657 'title' => 'Legacy', 658 'text' => 'Text', 659 'filter' => true, 660 ); 661 $result = $widget->update( $instance, array() ); 662 $this->assertEquals( $instance, $result, 'Updating a widget without visual prop and explicit filter=true leaves legacy prop absent.' ); 663 664 // -- 665 $instance = array( 666 'title' => 'Legacy', 667 'text' => 'Text', 668 'visual' => true, 669 ); 670 $old_instance = array_merge( $instance, array( 671 'filter' => false, 672 ) ); 673 $expected = array_merge( $instance, array( 674 'filter' => true, 675 ) ); 676 $result = $widget->update( $instance, $old_instance ); 677 $this->assertEquals( $expected, $result, 'Updating a pre-existing widget with visual mode forces filter to be true.' ); 678 679 // -- 680 $instance = array( 681 'title' => 'Legacy', 682 'text' => 'Text', 683 'filter' => true, 684 ); 685 $old_instance = array_merge( $instance, array( 686 'visual' => true, 687 ) ); 688 $result = $widget->update( $instance, $old_instance ); 689 $expected = array_merge( $instance, array( 690 'visual' => true, 691 ) ); 692 $this->assertEquals( $expected, $result, 'Updating a pre-existing visual widget retains visual mode when updated.' ); 693 694 // -- 695 $instance = array( 696 'title' => 'Legacy', 697 'text' => 'Text', 698 ); 699 $old_instance = array_merge( $instance, array( 700 'visual' => true, 701 ) ); 702 $result = $widget->update( $instance, $old_instance ); 703 $expected = array_merge( $instance, array( 704 'visual' => true, 705 'filter' => true, 706 ) ); 707 $this->assertEquals( $expected, $result, 'Updating a pre-existing visual widget retains visual=true and supplies missing filter=true.' ); 708 709 // -- 710 $instance = array( 711 'title' => 'Legacy', 712 'text' => 'Text', 713 'visual' => true, 714 ); 715 $expected = array_merge( $instance, array( 716 'filter' => true, 717 ) ); 718 $result = $widget->update( $instance, array() ); 719 $this->assertEquals( $expected, $result, 'Updating a widget with explicit visual=true and absent filter prop causes filter to be set to true.' ); 720 721 // -- 722 $instance = array( 723 'title' => 'Legacy', 724 'text' => 'Text', 725 'visual' => false, 726 ); 727 $result = $widget->update( $instance, array() ); 728 $expected = array_merge( $instance, array( 729 'filter' => false, 730 ) ); 731 $this->assertEquals( $expected, $result, 'Updating a widget in legacy mode results in filter=false as if checkbox not checked.' ); 732 733 // -- 734 $instance = array( 795 $old_instance = array_merge( 796 $instance, array( 797 'visual' => false, 798 'filter' => true, 799 ) 800 ); 801 $result = $widget->update( $instance, $old_instance ); 802 $expected = array_merge( 803 $instance, array( 804 'visual' => false, 805 'filter' => false, 806 ) 807 ); 808 $this->assertEquals( $expected, $result, 'Updating a widget that previously had legacy form results in filter allowed to be false.' ); 809 810 // -- 811 $instance = array( 812 'title' => 'Title', 813 'text' => 'Text', 814 'filter' => 'content', 815 ); 816 $result = $widget->update( $instance, array() ); 817 $expected = array_merge( 818 $instance, array( 819 'filter' => true, 820 'visual' => true, 821 ) 822 ); 823 $this->assertEquals( $expected, $result, 'Updating a widget that had \'content\' as its filter value persists non-legacy mode. This only existed in WP 4.8.0.' ); 824 825 // -- 826 $instance = array( 735 827 'title' => 'Title', 736 'text' => 'Text', 737 'filter' => false, 738 ); 739 $old_instance = array_merge( $instance, array( 740 'visual' => false, 741 'filter' => true, 742 ) ); 743 $result = $widget->update( $instance, $old_instance ); 744 $expected = array_merge( $instance, array( 745 'visual' => false, 746 'filter' => false, 747 ) ); 748 $this->assertEquals( $expected, $result, 'Updating a widget that previously had legacy form results in filter allowed to be false.' ); 749 750 // -- 751 $instance = array( 752 'title' => 'Title', 753 'text' => 'Text', 828 'text' => 'Text', 829 ); 830 $old_instance = array_merge( 831 $instance, array( 832 'filter' => 'content', 833 ) 834 ); 835 $result = $widget->update( $instance, $old_instance ); 836 $expected = array_merge( 837 $instance, array( 838 'visual' => true, 839 'filter' => true, 840 ) 841 ); 842 $this->assertEquals( $expected, $result, 'Updating a pre-existing widget with the filter=content prop in WP 4.8.0 upgrades to filter=true&visual=true.' ); 843 844 // -- 845 $instance = array( 846 'title' => 'Title', 847 'text' => 'Text', 754 848 'filter' => 'content', 755 849 ); 756 $result = $widget->update( $instance, array() ); 757 $expected = array_merge( $instance, array( 758 'filter' => true, 759 'visual' => true, 760 ) ); 761 $this->assertEquals( $expected, $result, 'Updating a widget that had \'content\' as its filter value persists non-legacy mode. This only existed in WP 4.8.0.' ); 762 763 // -- 764 $instance = array( 765 'title' => 'Title', 766 'text' => 'Text', 767 ); 768 $old_instance = array_merge( $instance, array( 769 'filter' => 'content', 770 ) ); 771 $result = $widget->update( $instance, $old_instance ); 772 $expected = array_merge( $instance, array( 773 'visual' => true, 774 'filter' => true, 775 ) ); 776 $this->assertEquals( $expected, $result, 'Updating a pre-existing widget with the filter=content prop in WP 4.8.0 upgrades to filter=true&visual=true.' ); 777 778 // -- 779 $instance = array( 780 'title' => 'Title', 781 'text' => 'Text', 782 'filter' => 'content', 783 ); 784 $result = $widget->update( $instance, array() ); 785 $expected = array_merge( $instance, array( 786 'filter' => true, 787 'visual' => true, 788 ) ); 850 $result = $widget->update( $instance, array() ); 851 $expected = array_merge( 852 $instance, array( 853 'filter' => true, 854 'visual' => true, 855 ) 856 ); 789 857 $this->assertEquals( $expected, $result, 'Updating a widget with filter=content (from WP 4.8.0) upgrades to filter=true&visual=true.' ); 790 858 } … … 799 867 function grant_unfiltered_html_cap( $caps, $cap ) { 800 868 if ( 'unfiltered_html' === $cap ) { 801 $caps = array_diff( $caps, array( 'do_not_allow' ) );869 $caps = array_diff( $caps, array( 'do_not_allow' ) ); 802 870 $caps[] = 'unfiltered_html'; 803 871 } … … 814 882 function revoke_unfiltered_html_cap( $caps, $cap ) { 815 883 if ( 'unfiltered_html' === $cap ) { 816 $caps = array_diff( $caps, array( 'unfiltered_html' ) );884 $caps = array_diff( $caps, array( 'unfiltered_html' ) ); 817 885 $caps[] = 'do_not_allow'; 818 886 }
Note: See TracChangeset
for help on using the changeset viewer.