PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/core/modules/user/src/Tests/UserEditTest.php

https://gitlab.com/reasonat/test8
PHP | 140 lines | 85 code | 22 blank | 33 comment | 0 complexity | b161b46367b3a571b4237ce05cf7376b MD5 | raw file
  1. <?php
  2. namespace Drupal\user\Tests;
  3. use Drupal\simpletest\WebTestBase;
  4. /**
  5. * Tests user edit page.
  6. *
  7. * @group user
  8. */
  9. class UserEditTest extends WebTestBase {
  10. /**
  11. * Test user edit page.
  12. */
  13. function testUserEdit() {
  14. // Test user edit functionality.
  15. $user1 = $this->drupalCreateUser(array('change own username'));
  16. $user2 = $this->drupalCreateUser(array());
  17. $this->drupalLogin($user1);
  18. // Test that error message appears when attempting to use a non-unique user name.
  19. $edit['name'] = $user2->getUsername();
  20. $this->drupalPostForm("user/" . $user1->id() . "/edit", $edit, t('Save'));
  21. $this->assertRaw(t('The username %name is already taken.', array('%name' => $edit['name'])));
  22. // Check that filling out a single password field does not validate.
  23. $edit = array();
  24. $edit['pass[pass1]'] = '';
  25. $edit['pass[pass2]'] = $this->randomMachineName();
  26. $this->drupalPostForm("user/" . $user1->id() . "/edit", $edit, t('Save'));
  27. $this->assertText(t("The specified passwords do not match."), 'Typing mismatched passwords displays an error message.');
  28. $edit['pass[pass1]'] = $this->randomMachineName();
  29. $edit['pass[pass2]'] = '';
  30. $this->drupalPostForm("user/" . $user1->id() . "/edit", $edit, t('Save'));
  31. $this->assertText(t("The specified passwords do not match."), 'Typing mismatched passwords displays an error message.');
  32. // Test that the error message appears when attempting to change the mail or
  33. // pass without the current password.
  34. $edit = array();
  35. $edit['mail'] = $this->randomMachineName() . '@new.example.com';
  36. $this->drupalPostForm("user/" . $user1->id() . "/edit", $edit, t('Save'));
  37. $this->assertRaw(t("Your current password is missing or incorrect; it's required to change the %name.", array('%name' => t('Email'))));
  38. $edit['current_pass'] = $user1->pass_raw;
  39. $this->drupalPostForm("user/" . $user1->id() . "/edit", $edit, t('Save'));
  40. $this->assertRaw(t("The changes have been saved."));
  41. // Test that the user must enter current password before changing passwords.
  42. $edit = array();
  43. $edit['pass[pass1]'] = $new_pass = $this->randomMachineName();
  44. $edit['pass[pass2]'] = $new_pass;
  45. $this->drupalPostForm("user/" . $user1->id() . "/edit", $edit, t('Save'));
  46. $this->assertRaw(t("Your current password is missing or incorrect; it's required to change the %name.", array('%name' => t('Password'))));
  47. // Try again with the current password.
  48. $edit['current_pass'] = $user1->pass_raw;
  49. $this->drupalPostForm("user/" . $user1->id() . "/edit", $edit, t('Save'));
  50. $this->assertRaw(t("The changes have been saved."));
  51. // Make sure the changed timestamp is updated.
  52. $this->assertEqual($user1->getChangedTime(), REQUEST_TIME, 'Changing a user sets "changed" timestamp.');
  53. // Make sure the user can log in with their new password.
  54. $this->drupalLogout();
  55. $user1->pass_raw = $new_pass;
  56. $this->drupalLogin($user1);
  57. $this->drupalLogout();
  58. // Test that the password strength indicator displays.
  59. $config = $this->config('user.settings');
  60. $this->drupalLogin($user1);
  61. $config->set('password_strength', TRUE)->save();
  62. $this->drupalPostForm("user/" . $user1->id() . "/edit", $edit, t('Save'));
  63. $this->assertRaw(t('Password strength:'), 'The password strength indicator is displayed.');
  64. $config->set('password_strength', FALSE)->save();
  65. $this->drupalPostForm("user/" . $user1->id() . "/edit", $edit, t('Save'));
  66. $this->assertNoRaw(t('Password strength:'), 'The password strength indicator is not displayed.');
  67. // Check that the user status field has the correct value and that it is
  68. // properly displayed.
  69. $admin_user = $this->drupalCreateUser(array('administer users'));
  70. $this->drupalLogin($admin_user);
  71. $this->drupalGet('user/' . $user1->id() . '/edit');
  72. $this->assertNoFieldChecked('edit-status-0');
  73. $this->assertFieldChecked('edit-status-1');
  74. $edit = array('status' => 0);
  75. $this->drupalPostForm('user/' . $user1->id() . '/edit', $edit, t('Save'));
  76. $this->assertText(t('The changes have been saved.'));
  77. $this->assertFieldChecked('edit-status-0');
  78. $this->assertNoFieldChecked('edit-status-1');
  79. $edit = array('status' => 1);
  80. $this->drupalPostForm('user/' . $user1->id() . '/edit', $edit, t('Save'));
  81. $this->assertText(t('The changes have been saved.'));
  82. $this->assertNoFieldChecked('edit-status-0');
  83. $this->assertFieldChecked('edit-status-1');
  84. }
  85. /**
  86. * Tests setting the password to "0".
  87. *
  88. * We discovered in https://www.drupal.org/node/2563751 that logging in with a
  89. * password that is literally "0" was not possible. This test ensures that
  90. * this regression can't happen again.
  91. */
  92. public function testUserWith0Password() {
  93. $admin = $this->drupalCreateUser(['administer users']);
  94. $this->drupalLogin($admin);
  95. // Create a regular user.
  96. $user1 = $this->drupalCreateUser([]);
  97. $edit = ['pass[pass1]' => '0', 'pass[pass2]' => '0'];
  98. $this->drupalPostForm("user/" . $user1->id() . "/edit", $edit, t('Save'));
  99. $this->assertRaw(t("The changes have been saved."));
  100. }
  101. /**
  102. * Tests editing of a user account without an email address.
  103. */
  104. function testUserWithoutEmailEdit() {
  105. // Test that an admin can edit users without an email address.
  106. $admin = $this->drupalCreateUser(array('administer users'));
  107. $this->drupalLogin($admin);
  108. // Create a regular user.
  109. $user1 = $this->drupalCreateUser(array());
  110. // This user has no email address.
  111. $user1->mail = '';
  112. $user1->save();
  113. $this->drupalPostForm("user/" . $user1->id() . "/edit", array('mail' => ''), t('Save'));
  114. $this->assertRaw(t("The changes have been saved."));
  115. }
  116. }