public function RedirectAPITest::testRedirectEntity in Redirect 8
Test redirect entity logic.
File
- tests/
src/ Kernel/ RedirectAPITest.php, line 51
Class
- RedirectAPITest
- Redirect entity and redirect API test coverage.
Namespace
Drupal\Tests\redirect\KernelCode
public function testRedirectEntity() {
// Create a redirect and test if hash has been generated correctly.
/** @var \Drupal\redirect\Entity\Redirect $redirect */
$redirect = $this->storage
->create();
$redirect
->setSource('some-url', [
'key' => 'val',
]);
$redirect
->setRedirect('node');
$redirect
->save();
$this
->assertEquals(Redirect::generateHash('some-url', [
'key' => 'val',
], Language::LANGCODE_NOT_SPECIFIED), $redirect
->getHash());
// Update the redirect source query and check if hash has been updated as
// expected.
$redirect
->setSource('some-url', [
'key1' => 'val1',
]);
$redirect
->save();
$this
->assertEquals(Redirect::generateHash('some-url', [
'key1' => 'val1',
], Language::LANGCODE_NOT_SPECIFIED), $redirect
->getHash());
// Update the redirect source path and check if hash has been updated as
// expected.
$redirect
->setSource('another-url', [
'key1' => 'val1',
]);
$redirect
->save();
$this
->assertEquals(Redirect::generateHash('another-url', [
'key1' => 'val1',
], Language::LANGCODE_NOT_SPECIFIED), $redirect
->getHash());
// Update the redirect language and check if hash has been updated as
// expected.
$redirect
->setLanguage('de');
$redirect
->save();
$this
->assertEquals(Redirect::generateHash('another-url', [
'key1' => 'val1',
], 'de'), $redirect
->getHash());
// Create a few more redirects to test the select.
for ($i = 0; $i < 5; $i++) {
$redirect = $this->storage
->create();
$redirect
->setSource($this
->randomMachineName());
$redirect
->save();
}
/** @var \Drupal\redirect\RedirectRepository $repository */
$repository = \Drupal::service('redirect.repository');
$redirect = $repository
->findMatchingRedirect('another-url', [
'key1' => 'val1',
], 'de');
if (!empty($redirect)) {
$this
->assertEquals($redirect
->getSourceUrl(), '/another-url?key1=val1');
}
else {
$this
->fail(t('Failed to find matching redirect.'));
}
// Load the redirect based on url.
$redirects = $repository
->findBySourcePath('another-url');
$redirect = array_shift($redirects);
if (!empty($redirect)) {
$this
->assertEquals($redirect
->getSourceUrl(), '/another-url?key1=val1');
}
else {
$this
->fail(t('Failed to find redirect by source path.'));
}
// Test passthrough_querystring.
$redirect = $this->storage
->create();
$redirect
->setSource('a-different-url');
$redirect
->setRedirect('node');
$redirect
->save();
$redirect = $repository
->findMatchingRedirect('a-different-url', [
'key1' => 'val1',
], 'de');
if (!empty($redirect)) {
$this
->assertEquals($redirect
->getSourceUrl(), '/a-different-url');
}
else {
$this
->fail('Failed to find redirect by source path with query string.');
}
// Add another redirect to the same path, with a query. This should always
// be found before the source without a query set.
/** @var \Drupal\redirect\Entity\Redirect $new_redirect */
$new_redirect = $this->storage
->create();
$new_redirect
->setSource('a-different-url', [
'foo' => 'bar',
]);
$new_redirect
->setRedirect('node');
$new_redirect
->save();
$found = $repository
->findMatchingRedirect('a-different-url', [
'foo' => 'bar',
], 'de');
if (!empty($found)) {
$this
->assertEquals($found
->getSourceUrl(), '/a-different-url?foo=bar');
}
else {
$this
->fail('Failed to find a redirect by source path with query string.');
}
// Add a redirect to an external URL.
$external_redirect = $this->storage
->create();
$external_redirect
->setSource('google');
$external_redirect
->setRedirect('https://google.com');
$external_redirect
->save();
$found = $repository
->findMatchingRedirect('google');
if (!empty($found)) {
$this
->assertEquals($found
->getRedirectUrl()
->toString(), 'https://google.com');
}
else {
$this
->fail('Failed to find a redirect for google.');
}
// Hashes should be case-insensitive since the source paths are.
/** @var \Drupal\redirect\Entity\Redirect $redirect */
$redirect = $this->storage
->create();
$redirect
->setSource('Case-Sensitive-Path');
$redirect
->setRedirect('node');
$redirect
->save();
$found = $repository
->findBySourcePath('case-sensitive-path');
if (!empty($found)) {
$found = reset($found);
$this
->assertEquals($found
->getSourceUrl(), '/Case-Sensitive-Path');
}
else {
$this
->fail('findBySourcePath is case sensitive');
}
$found = $repository
->findMatchingRedirect('case-sensitive-path');
if (!empty($found)) {
$this
->assertEquals($found
->getSourceUrl(), '/Case-Sensitive-Path');
}
else {
$this
->fail('findMatchingRedirect is case sensitive.');
}
}