WordPress.org

Make WordPress Core


Ignore:
Timestamp:
07/19/2021 02:00:11 PM (6 months ago)
Author:
SergeyBiryukov
Message:

Tests: Replace assertContains() with assertStringContainsString() when used with strings.

Using the assertContains() and assertNotContains() methods with string haystacks was deprecated in PHPUnit 8 and removed in PHPUnit 9.

While WordPress test suite currently only supports PHPUnit up to 7.5.x, this allows us to switch to newer assertions ahead of adding full support for PHPUnit 8+.

These methods introduced in PHPUnit 7.5 should be used as an alternative:

  • assertStringContainsString()
  • assertStringContainsStringIgnoringCase
  • assertStringNotContainsString()
  • assertStringNotContainsStringIgnoringCase

As WordPress currently uses PHPUnit 5.7.x to run tests on PHP 5.6, polyfills for these methods were added to the WP_UnitTestCase class for PHPUnit < 7.5.

Follow-up to [51331], [51451], [51461].

Props jrf, dd32, SergeyBiryukov.
See #53363, #46149.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/privacy/wpPrivacyGeneratePersonalDataExportGroupHtml.php

    r50969 r51462  
    4343        $expected_table_markup = '<table><tbody><tr><th>Field 1 Name</th><td>Field 1 Value</td></tr><tr><th>Field 2 Name</th><td>Field 2 Value</td></tr></tbody></table>';
    4444
    45         $this->assertContains( '<h2 id="test-data-group-test-data-group">Test Data Group</h2>', $actual );
    46         $this->assertContains( $expected_table_markup, $actual );
     45        $this->assertStringContainsString( '<h2 id="test-data-group-test-data-group">Test Data Group</h2>', $actual );
     46        $this->assertStringContainsString( $expected_table_markup, $actual );
    4747    }
    4848
     
    8282        $actual = wp_privacy_generate_personal_data_export_group_html( $data, 'test-data-group', 2 );
    8383
    84         $this->assertContains( '<h2 id="test-data-group-test-data-group">Test Data Group', $actual );
    85         $this->assertContains( '<td>Field 1 Value', $actual );
    86         $this->assertContains( '<td>Another Field 1 Value', $actual );
    87         $this->assertContains( '<td>Field 2 Value', $actual );
    88         $this->assertContains( '<td>Another Field 2 Value', $actual );
     84        $this->assertStringContainsString( '<h2 id="test-data-group-test-data-group">Test Data Group', $actual );
     85        $this->assertStringContainsString( '<td>Field 1 Value', $actual );
     86        $this->assertStringContainsString( '<td>Another Field 1 Value', $actual );
     87        $this->assertStringContainsString( '<td>Field 2 Value', $actual );
     88        $this->assertStringContainsString( '<td>Another Field 2 Value', $actual );
    8989        $this->assertSame( 2, substr_count( $actual, '<th>Field 1 Name' ) );
    9090        $this->assertSame( 2, substr_count( $actual, '<th>Field 2 Name' ) );
     
    120120        $actual = wp_privacy_generate_personal_data_export_group_html( $data, 'test-data-group', 2 );
    121121
    122         $this->assertContains( '<a href="http://wordpress.org">http://wordpress.org</a>', $actual );
    123         $this->assertContains( '<a href="https://wordpress.org">https://wordpress.org</a>', $actual );
    124         $this->assertContains( 'https://wordpress.org not a link.', $actual );
     122        $this->assertStringContainsString( '<a href="http://wordpress.org">http://wordpress.org</a>', $actual );
     123        $this->assertStringContainsString( '<a href="https://wordpress.org">https://wordpress.org</a>', $actual );
     124        $this->assertStringContainsString( 'https://wordpress.org not a link.', $actual );
    125125    }
    126126
     
    138138        $actual = wp_privacy_generate_personal_data_export_group_html( $data, 'escape-html-in-group-labels', 2 );
    139139
    140         $this->assertContains( '<h2 id="escape-html-in-group-labels-escape-html-in-group-labels">&lt;div&gt;Escape HTML in group labels&lt;/div&gt;</h2>', $actual );
     140        $this->assertStringContainsString( '<h2 id="escape-html-in-group-labels-escape-html-in-group-labels">&lt;div&gt;Escape HTML in group labels&lt;/div&gt;</h2>', $actual );
    141141    }
    142142
     
    164164
    165165        $actual = wp_privacy_generate_personal_data_export_group_html( $data, 'test-data-group', 2 );
    166         $this->assertContains( $data['items'][0]['links']['value'], $actual );
    167         $this->assertContains( $data['items'][0]['formatting']['value'], $actual );
     166        $this->assertStringContainsString( $data['items'][0]['links']['value'], $actual );
     167        $this->assertStringContainsString( $data['items'][0]['formatting']['value'], $actual );
    168168    }
    169169
     
    192192        $actual = wp_privacy_generate_personal_data_export_group_html( $data, 'test-data-group', 2 );
    193193
    194         $this->assertNotContains( $data['items'][0]['scripts']['value'], $actual );
    195         $this->assertContains( '<td>Testing that script tags are stripped.</td>', $actual );
    196 
    197         $this->assertNotContains( $data['items'][0]['images']['value'], $actual );
    198         $this->assertContains( '<th>Images are not allowed</th><td></td>', $actual );
     194        $this->assertStringNotContainsString( $data['items'][0]['scripts']['value'], $actual );
     195        $this->assertStringContainsString( '<td>Testing that script tags are stripped.</td>', $actual );
     196
     197        $this->assertStringNotContainsString( $data['items'][0]['images']['value'], $actual );
     198        $this->assertStringContainsString( '<th>Images are not allowed</th><td></td>', $actual );
    199199    }
    200200
     
    225225        $actual = wp_privacy_generate_personal_data_export_group_html( $data, 'test-data-group', 2 );
    226226
    227         $this->assertContains( '<h2 id="test-data-group-test-data-group">Test Data Group', $actual );
    228         $this->assertContains( '<span class="count">(2)</span></h2>', $actual );
     227        $this->assertStringContainsString( '<h2 id="test-data-group-test-data-group">Test Data Group', $actual );
     228        $this->assertStringContainsString( '<span class="count">(2)</span></h2>', $actual );
    229229        $this->assertSame( 2, substr_count( $actual, '<table>' ) );
    230230    }
     
    250250        $actual = wp_privacy_generate_personal_data_export_group_html( $data, 'test-data-group', 2 );
    251251
    252         $this->assertContains( '<h2 id="test-data-group-test-data-group">Test Data Group</h2>', $actual );
    253         $this->assertNotContains( '<span class="count">', $actual );
     252        $this->assertStringContainsString( '<h2 id="test-data-group-test-data-group">Test Data Group</h2>', $actual );
     253        $this->assertStringNotContainsString( '<span class="count">', $actual );
    254254        $this->assertSame( 1, substr_count( $actual, '<table>' ) );
    255255    }
Note: See TracChangeset for help on using the changeset viewer.