Problem/Motivation

In modules/node/src/Tests/NodeTitleTest.php, one of the tests checks if the title is equal to '0'.

$this->assertEqual(current($this->xpath($xpath)), 0, 'Node title is displayed as 0.', 'Node');

This test passes because a title not exist (FALSE), 0 is falsey, and assertEqual considers those to be the same.

$this->assertEqual(FALSE, 0, 'Node title is displayed as 0.', 'Node');

Proposed resolution

Place block title.
$this->drupalPlaceBlock('page_title_block');

Use assertSame() - asserts that two variables have the same type and value.

// Test that 0 appears in the template <h1>.
$this->assertSame($this->xpath('//h1')[0]->getText(), '0');

Remaining tasks

Delete the deprecated code.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

wheatpenny created an issue. See original summary.

wheatpenny’s picture

Attached is an updated test for "0 | Drupal." It currently fails as the H1 does not appear to be created.

wheatpenny’s picture

Issue summary: View changes
wheatpenny’s picture

Status: Active » Needs work
pwolanin’s picture

So, I think the real problem here is that the H1 is not rendered at all as best I can tell.

I a possible change to the test would be:

--- a/core/modules/node/src/Tests/NodeTitleTest.php
+++ b/core/modules/node/src/Tests/NodeTitleTest.php
@@ -85,7 +85,7 @@ function testNodeTitle() {
     $this->assertTitle(0 . ' | Drupal', 'Page title is equal to 0.', 'Node');
     // Test that 0 appears in the template <h2>.
     $xpath = '//h1';
-    $this->assertEqual(current($this->xpath($xpath)), 0, 'Node title is displayed as 0.', 'Node');
+    $this->assertIdentical((string) current($this->xpath($xpath)), '0', 'Node title is displayed as 0.', 'Node');
 
     // Test edge case where node title contains special characters.
     $edge_case_title = 'article\'s "title".';

And this is showing a bug perhaps in the rendering system to node template?

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Krzysztof Domański’s picture

Version: 8.6.x-dev » 8.7.x-dev
Issue summary: View changes
Status: Needs work » Needs review
FileSize
816 bytes
1.4 KB
// Test that 0 appears in the template <h1>.
$xpath = '//h1';
$this->assertEqual(current($this->xpath($xpath)), 0, 'Node title is displayed as 0.', 'Node');

Page title is not rendered now, so test (current($this->xpath($xpath))) treated 0 as FALSE and following assert pass! See attached test-only.

$this->assertEqual(FALSE, 0, 'Node title is displayed as 0.', 'Node');
Krzysztof Domański’s picture

Title: NodeTitleTest passes when it should currently fail » Wrong assert in NodeTitleTest
Issue summary: View changes
FileSize
3.81 KB
4.06 KB

Updated issue summary and fixed coding standards and deprecated code.

Status: Needs review » Needs work

The last submitted patch, 13: core_2668416-13.patch, failed testing. View results

Krzysztof Domański’s picture

Issue summary: View changes
Status: Needs work » Needs review
FileSize
783 bytes
4.06 KB

Changed assertEqual() to assertSame(). Asserts that two variables have the same type and value.

Krzysztof Domański’s picture

Added test-only.

The last submitted patch, 16: core_2668416-16-test-only.patch, failed testing. View results

Anonymous’s picture

Status: Needs review » Reviewed & tested by the community
alexpott’s picture

Status: Reviewed & tested by the community » Needs work

Nice fix. Testing for things that PHP duck types to false can be tricky.

+++ b/core/modules/node/tests/src/Functional/NodeTitleTest.php
@@ -34,8 +34,14 @@ class NodeTitleTest extends NodeTestBase {
-    $this->adminUser = $this->drupalCreateUser(['administer nodes', 'create article content', 'create page content', 'post comments']);
...
+    $this->adminUser = $this->drupalCreateUser([
+      'administer nodes',
+      'create article content',
+      'create page content',
+      'post comments',
+    ]);

@@ -56,15 +62,16 @@ public function testNodeTitle() {
-    $this->assertEqual($this->xpath($xpath)[0]->getText(), $node->label() . ' | Drupal', 'Page title is equal to node title.', 'Node');
+    $this->assertEquals($node->label() . ' | Drupal', $this->xpath($xpath)[0]->getText(), 'Page title is equal to node title.');
...
-    $this->assertEqual($this->xpath($xpath)[0]->getText(), $node->label(), 'Node breadcrumb is equal to node title.', 'Node');
+    $this->assertEquals($node->label(), $this->xpath($xpath)[0]->getText(), 'Node breadcrumb is equal to node title.');
...
-    $this->assertEqual($this->xpath('//article[contains(concat(" ", normalize-space(@class), " "), :node-class)]/h2/a/span', [':node-class' => ' node--type-' . $node->bundle() . ' '])[0]->getText(), $node->label(), 'Node preview title is equal to node title.', 'Node');
+    $xpath = '//article[contains(concat(" ", normalize-space(@class), " "), :node-class)]/h2/a/span';
+    $this->assertEquals($node->label(), $this->xpath($xpath, [':node-class' => ' node--type-' . $node->bundle() . ' '])[0]->getText(), 'Node preview title is equal to node title.');

@@ -92,12 +98,11 @@ public function testNodeTitle() {
-    $this->assertRaw('<title>' . $edge_case_title_escaped . ' | Drupal</title>', 'Page title is equal to article\'s "title".', 'Node');
+    $this->assertSession()->responseContains('<title>' . $edge_case_title_escaped . ' | Drupal</title>', 'Page title is equal to article\'s "title".');
...
-    $this->assertRaw('<title>' . $edge_case_title_escaped . ' | Drupal</title>', 'Page title is equal to article\'s "title".', 'Node');
-
+    $this->assertSession()->responseContains('<title>' . $edge_case_title_escaped . ' | Drupal</title>', 'Page title is equal to article\'s "title".');

These changes are out-of-scope. Yes at some point we need to make this change or similar changes. But scope creep makes everyone have to think more to review the issue.

Krzysztof Domański’s picture

Status: Needs work » Needs review
Issue tags: +Quick fix
FileSize
1.37 KB
Lendude’s picture

Version: 8.7.x-dev » 8.6.x-dev
+++ b/core/modules/node/tests/src/Functional/NodeTitleTest.php
@@ -77,10 +78,10 @@ public function testNodeTitle() {
-    $this->assertTitle(0 . ' | Drupal', 'Page title is equal to 0.', 'Node');
+    $this->assertSession()->titleEquals('0 | Drupal');

This is also out of scope, it's not needed to fix the problem right?

Test only fix so should be eligible for 8.6.x

Krzysztof Domański’s picture

This is also out of scope, it's not needed to fix the problem right?

Right, it's not important in this issue.

 * @deprecated Scheduled for removal in Drupal 9.0.0.
 *   Use $this->assertSession()->titleEquals() instead.
 */
protected function assertTitle($expected_title) {
Krzysztof Domański’s picture

Lendude’s picture

Status: Needs review » Reviewed & tested by the community

This looks good now, all feedback has been addressed, thanks @Krzysztof Domański

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Crediting @wheatpenny for creating the issue and @Lendude and myself for scope management.

Committed and pushed 751b38424c to 8.7.x and 11f66339c1 to 8.6.x. Thanks!

  • alexpott committed 751b384 on 8.7.x
    Issue #2668416 by Krzysztof Domański, wheatpenny, Lendude, alexpott:...

  • alexpott committed 11f6633 on 8.6.x
    Issue #2668416 by Krzysztof Domański, wheatpenny, Lendude, alexpott:...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.