5 use Webmozart\PathUtil\Path;
11 class UserCase extends CommandUnishTestCase
14 const NAME = 'example';
16 public function setUp()
18 if (!$this->getSites()) {
19 $this->setUpDrupal(1, true);
24 public function testBlockUnblock()
26 $this->drush('user-block', [self::NAME]);
27 $this->drush('user-information', [self::NAME], ['format' => 'json']);
29 $output = $this->getOutputFromJSON($uid);
30 $this->assertEquals(0, $output->user_status, 'User is blocked.');
33 $this->drush('user-unblock', [self::NAME]);
34 $this->drush('user-information', [self::NAME], ['format' => 'json']);
35 $output = $this->getOutputFromJSON($uid);
36 $this->assertEquals(1, $output->user_status, 'User is unblocked.');
39 public function testUserRole()
41 // First, create the role since we use testing install profile.
42 $this->drush('role-create', ['test role']);
43 $this->drush('user-add-role', ['test role', self::NAME]);
44 $this->drush('user-information', [self::NAME], ['format' => 'json']);
46 $output = $this->getOutputFromJSON($uid);
47 $expected = ['authenticated', 'test role'];
48 $this->assertEquals($expected, array_values((array)$output->roles), 'User has test role.');
51 $this->drush('user-remove-role', ['test role', self::NAME]);
52 $this->drush('user-information', [self::NAME], ['format' => 'json']);
53 $output = $this->getOutputFromJSON($uid);
54 $expected = ['authenticated'];
55 $this->assertEquals($expected, array_values((array)$output->roles), 'User removed test role.');
58 public function testUserPassword()
62 $this->drush('user-password', [self::NAME, $newpass]);
63 $eval = "return \\Drupal::service('user.auth')->authenticate('$name', '$newpass');";
64 $this->drush('php-eval', [$eval]);
65 $output = $this->getOutput();
66 $this->assertEquals("2", $output, 'User can login with new password.');
69 public function testUserLogin()
71 // Check if user-login on a non-bootstrapped environment returns error.
72 $this->drush('user-login', [], ['uri' => 'OMIT'], null, null, self::EXIT_ERROR);
75 $user_login_options = ['simulate' => null, 'browser' => 'unish'];
76 // Collect full logs so we can check browser.
77 $this->drush('user-login', [], $user_login_options + ['debug' => null]);
78 $logOutput = $this->getErrorOutput();
79 $url = parse_url($this->getOutput());
80 $this->assertContains('/user/reset/1', $url['path'], 'Login returned a reset URL for uid 1 by default');
81 $this->assertContains('Opening browser unish at http://', $logOutput);
82 // Check specific user with a path argument.
84 $this->drush('user-login', ['node/add'], $user_login_options + ['name' => self::NAME]);
85 $output = $this->getOutput();
86 $url = parse_url($output);
87 $query = $url['query'];
88 $this->assertContains('/user/reset/' . $uid, $url['path'], 'Login with user argument returned a valid reset URL');
89 $this->assertEquals('destination=node/add', $query, 'Login included destination path in URL');
90 // Check path used as only argument when using uid option.
91 $this->drush('user-login', ['node/add'], $user_login_options + ['name' => self::NAME]);
92 $output = $this->getOutput();
93 $url = parse_url($output);
94 $this->assertContains('/user/reset/' . $uid, $url['path'], 'Login with uid option returned a valid reset URL');
95 $query = $url['query'];
96 $this->assertEquals('destination=node/add', $query, 'Login included destination path in URL');
99 public function testUserCancel()
101 // Create a content entity type and enable its module.
103 'name' => 'UnishArticle',
104 'machine_name' => 'unish_article',
105 'package' => 'custom',
106 'version' => '8.x-1.0-dev',
107 'dependencies' => 'text',
108 'entity_type_label' => 'UnishArticle',
109 'entity_type_id' => 'unish_article',
110 'entity_base_path' => 'admin/content/unish_article',
112 'revisionable' => 'no',
114 'access_controller' => 'no',
115 'title_base_field' => 'yes',
116 'status_base_field' => 'yes',
117 'created_base_field' => 'yes',
118 'changed_base_field' => 'yes',
119 'author_base_field' => 'yes',
120 'description_base_field' => 'no',
121 'rest_configuration' => 'no',
123 $answers = json_encode($answers);
124 $original = getenv('SHELL_INTERACTIVE');
125 $this->setEnv(['SHELL_INTERACTIVE' => 1]);
126 $this->drush('generate', ['content-entity'], ['answers' => $answers, 'directory' => Path::join(self::webroot(), 'modules/contrib')]);
127 $this->setEnv(['SHELL_INTERACTIVE' => $original]);
128 $this->drush('pm-enable', ['text,unish_article']);
129 // Create one unish_article owned by our example user.
130 $this->drush('php-script', ['create_unish_articles'], ['script-path' => '../vendor/drush/drush/tests/resources']);
131 // Verify that content entity exists.
132 $code = "echo entity_load('unish_article', 1)->id()";
133 $this->drush('php-eval', [$code]);
134 $this->assertEquals(1, $this->getOutput());
136 // Cancel user and verify that the account is deleted.
137 $this->drush('user-cancel', [self::NAME], ['delete-content' => null]);
138 $this->drush('user-information', [self::NAME], ['fields' => 'user_status', 'format' => 'string'], null, null, self::EXIT_ERROR);
140 // Verify that the content is deleted.
141 // Sigh - only nodes actually honor the cancellation methods. @see node_user_cancel().
142 // $this->drush('php-eval', [$code], [], NULL, NULL, self::EXIT_ERROR);
143 // $output = $this->getOutput();
144 // $this->assertEquals('', $this->getOutput());
147 public function userCreate()
149 $this->drush('user-create', [self::NAME], ['password' => 'password', 'mail' => "example@example.com"]);
150 $this->drush('user-information', [self::NAME], ['format' => 'json']);
152 $output = $this->getOutputFromJSON($uid);
153 $this->assertEquals('example@example.com', $output->mail);
154 $this->assertEquals(self::NAME, $output->name);
155 $this->assertEquals(1, $output->user_status, 'Newly created user is Active.');
156 $expected = ['authenticated'];
157 $this->assertEquals($expected, array_values((array)$output->roles), 'Newly created user has one role.');