Skip to content

Commit

Permalink
Add constants in tests (#5901)
Browse files Browse the repository at this point in the history
* Add constants in tests

* one more

* rsync assertions

* one to many
  • Loading branch information
weitzman authored Mar 12, 2024
1 parent e0478fa commit 1c01274
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 51 deletions.
10 changes: 6 additions & 4 deletions tests/functional/RsyncTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Unish;

use Drush\Commands\core\RsyncCommands;

/**
* @file
* Tests for rsync command
Expand Down Expand Up @@ -40,8 +42,8 @@ public function testRsyncSimulated()
// targets. The aliases are not interpreted at all until they recache
// the remote side, at which point they will be evaluated & any needed
// injection will be done.
$this->drush('rsync', ['@example.dev', '@example.stage'], $options, 'user@server/path/to/drupal#sitename');
$expected = "[notice] Simulating: ssh -o PasswordAuthentication=no user@server '/path/to/vendor/bin/drush --no-interaction rsync @example.dev @example.stage --uri=sitename";
$this->drush(RsyncCommands::RSYNC, ['@example.dev', '@example.stage'], $options, 'user@server/path/to/drupal#sitename');
$expected = "[notice] Simulating: ssh -o PasswordAuthentication=no user@server '/path/to/vendor/bin/drush --no-interaction core:rsync @example.dev @example.stage --uri=sitename";
$this->assertStringContainsString($expected, $this->getSimplifiedErrorOutput());
}

Expand Down Expand Up @@ -80,7 +82,7 @@ public function testRsyncPathAliases()

// Test an actual rsync between our two fixture sites. Note that
// these sites share the same web root.
$this->drush('rsync', ["$source_alias:%files/a/", "$target_alias:%files/b"], $options, null, null, self::EXIT_SUCCESS, '2>&1');
$this->drush(RsyncCommands::RSYNC, ["$source_alias:%files/a/", "$target_alias:%files/b"], $options, null, null, self::EXIT_SUCCESS, '2>&1');
$this->assertStringContainsString('Copy new and override existing files at ', $this->getOutput());

// Test to see if our fixture file now exists at $target
Expand All @@ -97,7 +99,7 @@ public function testRsyncAndPercentFiles()
{
$site = current($this->getAliases());
$options['simulate'] = null;
$this->drush('core:rsync', ["$site:%files", "/tmp"], $options, null, null, self::EXIT_SUCCESS, '2>&1;');
$this->drush(RsyncCommands::RSYNC, ["$site:%files", "/tmp"], $options, null, null, self::EXIT_SUCCESS, '2>&1;');
$this->assertStringContainsString('[notice] Simulating: rsync -e \'ssh \' -akz __DIR__/sut/sites/dev/files/ /tmp', $this->getSimplifiedOutput());
}
}
7 changes: 4 additions & 3 deletions tests/functional/SqlConnectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Unish;

use Drush\Commands\core\StatusCommands;
use Drush\Commands\sql\SqlCommands;

/**
Expand Down Expand Up @@ -47,21 +48,21 @@ public function testSqlConnect()
$this->assertStringContainsString('1', $output);

// Run 'core-status' and insure that we can bootstrap Drupal.
$this->drush('core-status', [], ['fields' => 'bootstrap']);
$this->drush(StatusCommands::STATUS, [], ['fields' => 'bootstrap']);
$output = $this->getOutput();
$this->assertStringContainsString('Successful', $output);

// Test to see if 'sql-create' can erase the database.
// The only output is a confirmation string, so we'll run
// other commands to confirm that this worked.
$this->drush('sql-create');
$this->drush(SqlCommands::CREATE);

// Try to execute a query. This should give a "table not found" error.
$this->execute($connectionString . ' ' . $shell_options . ' "SELECT uid FROM users where uid = 1;"', self::EXIT_ERROR, $this->webroot());

// We should still be able to run 'core-status' without getting an
// error, although Drupal should not bootstrap any longer.
$this->drush('core-status', [], ['fields' => 'bootstrap']);
$this->drush(StatusCommands::STATUS, [], ['fields' => 'bootstrap']);
$output = $this->getOutput();
$this->assertStringNotContainsString('Successful', $output);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/UpdateDBTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function testUpdateDBStatus()
// Assure that a pending post-update is reported.
$this->pathPostUpdate = Path::join($this->webroot(), 'modules/unish/drush_empty_module/drush_empty_module.post_update.php');
copy(__DIR__ . '/resources/drush_empty_module.post_update.php', $this->pathPostUpdate);
$this->drush('updatedb:status', [], ['format' => 'json']);
$this->drush(UpdateDBCommands::STATUS, [], ['format' => 'json']);
$out = $this->getOutputFromJSON('drush_empty_module-post-null_op');
$this->assertStringContainsString('This is a test of the emergency broadcast system.', trim($out['description']));
}
Expand Down
87 changes: 46 additions & 41 deletions tests/functional/UserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

namespace Unish;

use Drush\Commands\core\LoginCommands;
use Drush\Commands\core\PhpCommands;
use Drush\Commands\core\RoleCommands;
use Drush\Commands\core\UserCommands;
use Drush\Commands\pm\PmCommands;
use Symfony\Component\Filesystem\Path;

/**
Expand All @@ -27,37 +32,37 @@ public function testBlockUnblock()
{
$uid = 2;

$this->drush('user-block', [self::NAME]);
$this->drush('user-information', [self::NAME], ['format' => 'json']);
$this->drush(UserCommands::BLOCK, [self::NAME]);
$this->drush(UserCommands::INFORMATION, [self::NAME], ['format' => 'json']);
$output = $this->getOutputFromJSON($uid);
$this->assertEquals(0, $output['user_status'], 'User is blocked.');

// user-unblock
$this->drush('user-unblock', [self::NAME]);
$this->drush('user-information', [self::NAME], ['format' => 'json']);
$this->drush(UserCommands::UNBLOCK, [self::NAME]);
$this->drush(UserCommands::INFORMATION, [self::NAME], ['format' => 'json']);
$output = $this->getOutputFromJSON($uid);
$this->assertEquals(1, $output['user_status'], 'User is unblocked.');

// user-block user by uid.
$this->drush('user-block', [], ['uid' => $uid]);
$this->drush('user-information', [self::NAME], ['format' => 'json']);
$this->drush(UserCommands::BLOCK, [], ['uid' => $uid]);
$this->drush(UserCommands::INFORMATION, [self::NAME], ['format' => 'json']);
$output = $this->getOutputFromJSON($uid);
$this->assertEquals(0, $output['user_status'], 'User (id) is blocked.');

$this->drush('user-unblock', [], ['uid' => $uid]);
$this->drush('user-information', [self::NAME], ['format' => 'json']);
$this->drush(UserCommands::UNBLOCK, [], ['uid' => $uid]);
$this->drush(UserCommands::INFORMATION, [self::NAME], ['format' => 'json']);
$output = $this->getOutputFromJSON($uid);
$this->assertEquals(1, $output['user_status'], 'User (id) is unblocked.');


// user-block user by mail.
$this->drush('user-block', [], ['mail' => self::MAIL]);
$this->drush('user-information', [self::NAME], ['format' => 'json']);
$this->drush(UserCommands::BLOCK, [], ['mail' => self::MAIL]);
$this->drush(UserCommands::INFORMATION, [self::NAME], ['format' => 'json']);
$output = $this->getOutputFromJSON($uid);
$this->assertEquals(0, $output['user_status'], 'User (mail) is blocked.');

$this->drush('user-unblock', [], ['uid' => $uid]);
$this->drush('user-information', [self::NAME], ['format' => 'json']);
$this->drush(UserCommands::UNBLOCK, [], ['uid' => $uid]);
$this->drush(UserCommands::INFORMATION, [self::NAME], ['format' => 'json']);
$output = $this->getOutputFromJSON($uid);
$this->assertEquals(1, $output['user_status'], 'User (mail) is unblocked.');
}
Expand All @@ -66,44 +71,44 @@ public function testUserRole()
{
$uid = 2;
// First, create the role since we use testing install profile.
$this->drush('role-create', ['test role']);
$this->drush('user-add-role', ['test role', self::NAME]);
$this->drush('user-information', [self::NAME], ['format' => 'json']);
$this->drush(RoleCommands::CREATE, ['test role']);
$this->drush(UserCommands::ROLE_ADD, ['test role', self::NAME]);
$this->drush(UserCommands::INFORMATION, [self::NAME], ['format' => 'json']);
$output = $this->getOutputFromJSON($uid);
$expected = ['authenticated', 'test role'];
$this->assertEquals($expected, array_values($output['roles']), 'User has test role.');

// user-remove-role
$this->drush('user-remove-role', ['test role', self::NAME]);
$this->drush('user-information', [self::NAME], ['format' => 'json']);
$this->drush(UserCommands::ROLE_REMOVE, ['test role', self::NAME]);
$this->drush(UserCommands::INFORMATION, [self::NAME], ['format' => 'json']);
$output = $this->getOutputFromJSON($uid);
$expected = ['authenticated'];
$this->assertEquals($expected, array_values($output['roles']), 'User removed test role.');

// user-add-role by uid.
$this->drush('user-add-role', ['test role'], ['uid' => $uid]);
$this->drush('user-information', [self::NAME], ['format' => 'json']);
$this->drush(UserCommands::ROLE_ADD, ['test role'], ['uid' => $uid]);
$this->drush(UserCommands::INFORMATION, [self::NAME], ['format' => 'json']);
$output = $this->getOutputFromJSON($uid);
$expected = ['authenticated', 'test role'];
$this->assertEquals($expected, array_values($output['roles']), 'User (id) has test role.');

// user-remove-role by uid
$this->drush('user-remove-role', ['test role'], ['uid' => $uid]);
$this->drush('user-information', [self::NAME], ['format' => 'json']);
$this->drush(UserCommands::ROLE_REMOVE, ['test role'], ['uid' => $uid]);
$this->drush(UserCommands::INFORMATION, [self::NAME], ['format' => 'json']);
$output = $this->getOutputFromJSON($uid);
$expected = ['authenticated'];
$this->assertEquals($expected, array_values($output['roles']), 'User (id) removed test role.');

// user-add-role by mail.
$this->drush('user-add-role', ['test role'], ['mail' => self::MAIL]);
$this->drush('user-information', [self::NAME], ['format' => 'json']);
$this->drush(UserCommands::ROLE_ADD, ['test role'], ['mail' => self::MAIL]);
$this->drush(UserCommands::INFORMATION, [self::NAME], ['format' => 'json']);
$output = $this->getOutputFromJSON($uid);
$expected = ['authenticated', 'test role'];
$this->assertEquals($expected, array_values($output['roles']), 'User (mail) has test role.');

// user-remove-role by mail.
$this->drush('user-remove-role', ['test role'], ['mail' => self::MAIL]);
$this->drush('user-information', [self::NAME], ['format' => 'json']);
$this->drush(UserCommands::ROLE_REMOVE, ['test role'], ['mail' => self::MAIL]);
$this->drush(UserCommands::INFORMATION, [self::NAME], ['format' => 'json']);
$output = $this->getOutputFromJSON($uid);
$expected = ['authenticated'];
$this->assertEquals($expected, array_values($output['roles']), 'User (mail) removed test role.');
Expand All @@ -113,9 +118,9 @@ public function testUserPassword()
{
$newpass = 'newpass';
$name = self::NAME;
$this->drush('user:password', [self::NAME, $newpass]);
$this->drush(UserCommands::PASSWORD, [self::NAME, $newpass]);
$eval = "return Drupal::service(\"user.auth\")->authenticate(\"$name\", \"$newpass\");";
$this->drush('php:eval', [$eval]);
$this->drush(PhpCommands::EVAL, [$eval]);
$output = $this->getOutput();
$this->assertEquals("2", $output, 'User can login with new password.');
}
Expand All @@ -124,44 +129,44 @@ public function testUserLoginNoBootstrappedSite(): never
{
$this->markTestSkipped('TODO: @none should prevent selection of site at cwd');
// Check if user-login on a non-bootstrapped environment returns error.
$this->drush('user-login', [], [], '@none', null, self::EXIT_ERROR);
$this->drush(LoginCommands::LOGIN, [], [], '@none', null, self::EXIT_ERROR);
}

public function testUserLogin()
{
// Check user-login
$user_login_options = ['simulate' => null, 'browser' => 'unish'];
// Collect full logs so we can check browser.
$this->drush('user-login', [], $user_login_options + ['debug' => null]);
$this->drush(LoginCommands::LOGIN, [], $user_login_options + ['debug' => null]);
$logOutput = $this->getErrorOutput();
$url = parse_url($this->getOutput());
$this->assertStringContainsString('/user/reset/1', $url['path'], 'Login returned a reset URL for uid 1 by default');
$this->assertStringContainsString('Opening browser unish at http://', $logOutput);
// Check specific user with a path argument.
$uid = 2;
$this->drush('user-login', ['node/add'], $user_login_options + ['name' => self::NAME]);
$this->drush(LoginCommands::LOGIN, ['node/add'], $user_login_options + ['name' => self::NAME]);
$output = $this->getOutput();
$url = parse_url($output);
$query = $url['query'];
$this->assertStringContainsString('/user/reset/' . $uid, $url['path'], 'Login with user argument returned a valid reset URL');
$this->assertEquals('destination=node/add', $query, 'Login included destination path in URL');
// Check path used as only argument when using uid option.
$this->drush('user-login', ['node/add'], $user_login_options + ['name' => self::NAME]);
$this->drush(LoginCommands::LOGIN, ['node/add'], $user_login_options + ['name' => self::NAME]);
$output = $this->getOutput();
$url = parse_url($output);
$this->assertStringContainsString('/user/reset/' . $uid, $url['path'], 'Login with uid option returned a valid reset URL');
$query = $url['query'];
$this->assertEquals('destination=node/add', $query, 'Login included destination path in URL');
// Test specific user by uid.
$uid = 2;
$this->drush('user-login', [], $user_login_options + ['uid' => $uid]);
$this->drush(LoginCommands::LOGIN, [], $user_login_options + ['uid' => $uid]);
$output = $this->getOutput();
$url = parse_url($output);
$this->assertStringContainsString('/user/reset/' . $uid, $url['path'], 'Login with uid option returned a valid reset URL');
// Test specific user by mail.
$uid = 2;
$mail = self::MAIL;
$this->drush('user-login', [], $user_login_options + ['mail' => $mail]);
$this->drush(LoginCommands::LOGIN, [], $user_login_options + ['mail' => $mail]);
$output = $this->getOutput();
$url = parse_url($output);
$this->assertStringContainsString('/user/reset/' . $uid, $url['path'], 'Login with mail option returned a valid reset URL');
Expand All @@ -170,18 +175,18 @@ public function testUserLogin()
public function testUserCancel()
{
CreateEntityType::createContentEntity($this);
$this->drush('pm-install', ['text,unish_article']);
$this->drush('php:script', ['create_unish_article_bundles'], ['script-path' => Path::join(__DIR__, 'resources')]);
$this->drush(PmCommands::INSTALL, ['text,unish_article']);
$this->drush(PhpCommands::SCRIPT, ['create_unish_article_bundles'], ['script-path' => Path::join(__DIR__, 'resources')]);
// Create one unish_article owned by our example user.
$this->drush('php-script', ['create_unish_articles'], ['script-path' => Path::join(__DIR__, 'resources')]);
$this->drush(PhpCommands::SCRIPT, ['create_unish_articles'], ['script-path' => Path::join(__DIR__, 'resources')]);
// Verify that content entity exists.
$code = "echo Drupal::entityTypeManager()->getStorage('unish_article')->load(1)->id()";
$this->drush('php-eval', [$code]);
$this->drush(PhpCommands::EVAL, [$code]);
$this->assertEquals(1, $this->getOutput());

// Cancel user and verify that the account is deleted.
$this->drush('user-cancel', [self::NAME], ['delete-content' => null]);
$this->drush('user-information', [self::NAME], ['fields' => 'user_status', 'format' => 'string'], null, null, self::EXIT_ERROR);
$this->drush(UserCommands::CANCEL, [self::NAME], ['delete-content' => null]);
$this->drush(UserCommands::INFORMATION, [self::NAME], ['fields' => 'user_status', 'format' => 'string'], null, null, self::EXIT_ERROR);

// Verify that the content is deleted.
// Sigh - only nodes actually honor the cancellation methods. @see node_user_cancel().
Expand All @@ -192,8 +197,8 @@ public function testUserCancel()

public function userCreate()
{
$this->drush('user-create', [self::NAME], ['password' => 'password', 'mail' => self::MAIL]);
$this->drush('user-information', [self::NAME], ['format' => 'json']);
$this->drush(UserCommands::CREATE, [self::NAME], ['password' => 'password', 'mail' => self::MAIL]);
$this->drush(UserCommands::INFORMATION, [self::NAME], ['format' => 'json']);
$uid = 2;
$output = $this->getOutputFromJSON($uid);
$this->assertEquals(self::MAIL, $output['mail']);
Expand Down
7 changes: 5 additions & 2 deletions tests/functional/WatchdogTailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace Unish;

use Drush\Commands\core\PhpCommands;
use Drush\Commands\pm\PmCommands;

/**
* Tests for watchdog tail command.
*
Expand All @@ -19,15 +22,15 @@ class WatchdogTailTest extends CommandUnishTestCase
public function testWatchdogTail()
{
$this->setUpDrupal(1, true);
$ret = $this->drush('pm:install', ['dblog']);
$ret = $this->drush(PmCommands::INSTALL, ['dblog']);
$options = [];
$childDrushProcess = $this->drushBackground('watchdog:tail', [], $options + ['simulate' => null]);
$iteration = 0;
$expected_output = [];
do {
$iteration++;
$expected_output[$iteration] = "watchdog tail iteration $iteration.";
$this->drush('php-eval', ["Drupal::logger('drush')->notice('{$expected_output[$iteration]}');"]);
$this->drush(PhpCommands::EVAL, ["Drupal::logger('drush')->notice('{$expected_output[$iteration]}');"]);
sleep(3);
$output = $childDrushProcess->getIncrementalOutput();
$this->assertStringContainsString($expected_output[$iteration], $output);
Expand Down

0 comments on commit 1c01274

Please sign in to comment.