Problem/Motivation
Note:
If you only want to determine if a particular needle occurs within haystack, use the faster and less memory intensive function strpos() instead.
-- @see https://www.php.net/manual/en/function.strstr.php
Proposed resolution
For example:
- $this->assertTrue(strstr($style, 'width: 555px;') !== FALSE, 'Dialog width respected.');
+ $this->assertTrue(strpos($style, 'width: 555px;') !== FALSE, 'Dialog width respected.');
Current occurrences in core
grep -nri 'str[i]*str\(.*\) [!=]== FALSE' core
core/modules/layout_builder/tests/src/FunctionalJavascript/BlockFormMessagesTest.php:108: $this->assertTrue(stristr($top_form_elements[0]->getText(), 'Title field is required.') !== FALSE);
core/modules/layout_builder/tests/src/FunctionalJavascript/BlockFormMessagesTest.php:132: if (strstr($exception->getMessage(), 'not clickable') === FALSE) {
core/modules/layout_builder/tests/src/FunctionalJavascript/InlineBlockTestBase.php:88: if (stristr($this->getUrl(), 'admin/structure') === FALSE) {
core/modules/image/tests/src/Functional/ImageFieldDisplayTest.php:153: $this->assertTrue(strstr($this->drupalGetHeader('Cache-Control'), 'private') !== FALSE, 'Cache-Control header was sent.');
core/modules/system/tests/src/FunctionalJavascript/OffCanvasTest.php:61: $this->assertTrue(strstr($style, 'width: 555px;') !== FALSE, 'Dialog width respected.');
core/modules/system/tests/src/FunctionalJavascript/OffCanvasTest.php:65: $this->assertTrue(strstr($style, 'width: 555px;') === FALSE, 'Dialog width reset to default.');
core/modules/settings_tray/tests/src/FunctionalJavascript/QuickEditIntegrationTest.php:154: $this->assertTrue(strstr($href, "/admin/structure/block/manage/custom/settings-tray?destination=$destination") !== FALSE);
core/modules/filter/src/Plugin/Filter/FilterAlign.php:27: if (stristr($text, 'data-align') !== FALSE) {
core/modules/filter/src/Plugin/Filter/FilterCaption.php:31: if (stristr($text, 'data-caption') !== FALSE) {
core/modules/responsive_image/tests/src/Functional/ResponsiveImageFieldDisplayTest.php:265: $this->assertTrue(strstr($this->drupalGetHeader('Cache-Control'), 'private') !== FALSE, 'Cache-Control header was sent.');
core/modules/editor/src/Plugin/Filter/EditorFileReference.php:69: if (stristr($text, 'data-entity-type="file"') !== FALSE) {
core/modules/editor/src/EditorXssFilter/Standard.php:102: if (stristr($html, 'data-') !== FALSE) {
core/modules/media/src/Plugin/Filter/MediaEmbed.php:280: if (stristr($text, '<drupal-media') === FALSE) {
Remaining tasks
Replace usages
User interface changes
API changes
Data model changes
Release notes snippet
Comments
Comment #2
jungleComment #3
jungleComment #4
jungleComment #5
jungleComment #6
kim.pepperDid a search for occurrences in core, and added to IS.
Comment #7
kim.pepperSeems like a quick fix.
Comment #8
jungleThanks @kim.pepper, But I guess you missed checking stristr()/ replacing stristr() with stripos()
Comment #9
kim.pepperOh nice catch.
Comment #10
kim.pepperComment #11
kim.pepperAlso need to check for === FALSE.
Comment #12
jungleAlmost perfect, 4 more lines to do.
Comment #13
kim.pepperFixes #12
Comment #14
jungle@kim.pepper, I am really sorry, actually, looking into it again, searching by strstr/stristr is more appropriate. So there are more to replace.
Comment #15
kim.pepperAs discussed in slack, there are a number of occurrences in test assertions that should probably be cleaned up first.
Postponing on #3132778: Replace usages of assertions with strstr with assertStringContainsString()/assertStringNotContainsString()
Comment #16
kim.pepperComment #17
mondrakethese can be replaced by
$this->assertSession()->responseHeaderContains()and in fact are included in #3131186-8: Replace assertions involving calls to drupalGetHeader() with session-based assertions, where possibleComment #18
kim.pepperComment #21
spokjeUnpostponing since #3132778: Replace usages of assertions with strstr with assertStringContainsString()/assertStringNotContainsString() and #3131186: Replace assertions involving calls to drupalGetHeader() with session-based assertions, where possible are (long) in.
Comment #22
spokjePatched against
9.4.x-devComment #23
spokjeComment #26
smustgrave commentedDoing a search of core I found 9 instances of this including comments but the patch has 6 replacements. Could you recheck?
Comment #27
sahil rohilla01 commentedI have made 2 replacement as mentioned in comment #26. Didn't find third one @smustgrave.
Comment #28
sahil rohilla01 commentedPlease Forgot above comment #27
I have made 2 replacement as mentioned in comment #26. but didn't find third one @smustgrave.
Comment #29
smustgrave commentedFrom what I can tell the patches in #27 and #28 don't contains the changes from #22
Comment #30
spokjeComment #31
spokjeComment #32
smustgrave commentedThanks @spokje!
Comment #33
znerol commentedPHP 8 introduced str_contains(). That should be used instead of
strpos(). However, there is no replacement forstripos()afaik.Comment #34
xjmYeah, let's hold off on this in favor of #3324560: Replace strpos/substr with str_starts_with() / str_contains() / str_ends_with(). Thanks!