DrupalMinkClientTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace Drupal\BuildTests\Framework\Tests;
  3. use Drupal\BuildTests\Framework\DrupalMinkClient;
  4. use PHPUnit\Framework\TestCase;
  5. use Symfony\Component\BrowserKit\Response;
  6. /**
  7. * Test \Drupal\BuildTests\Framework\DrupalMinkClient.
  8. *
  9. * This test is adapted from \Symfony\Component\BrowserKit\Tests\ClientTest.
  10. *
  11. * @coversDefaultClass \Drupal\BuildTests\Framework\DrupalMinkClient
  12. *
  13. * @group Build
  14. */
  15. class DrupalMinkClientTest extends TestCase {
  16. /**
  17. * @dataProvider getTestsForMetaRefresh
  18. * @covers ::getMetaRefreshUrl
  19. */
  20. public function testFollowMetaRefresh(string $content, string $expectedEndingUrl, bool $followMetaRefresh = TRUE) {
  21. $client = new TestClient();
  22. $client->followMetaRefresh($followMetaRefresh);
  23. $client->setNextResponse(new Response($content));
  24. $client->request('GET', 'http://www.example.com/foo/foobar');
  25. $this->assertEquals($expectedEndingUrl, $client->getRequest()->getUri());
  26. }
  27. public function getTestsForMetaRefresh() {
  28. return [
  29. ['<html><head><meta http-equiv="Refresh" content="4" /><meta http-equiv="refresh" content="0; URL=http://www.example.com/redirected"/></head></html>', 'http://www.example.com/redirected'],
  30. ['<html><head><meta http-equiv="refresh" content="0;URL=http://www.example.com/redirected"/></head></html>', 'http://www.example.com/redirected'],
  31. ['<html><head><meta http-equiv="refresh" content="0;URL=\'http://www.example.com/redirected\'"/></head></html>', 'http://www.example.com/redirected'],
  32. ['<html><head><meta http-equiv="refresh" content=\'0;URL="http://www.example.com/redirected"\'/></head></html>', 'http://www.example.com/redirected'],
  33. ['<html><head><meta http-equiv="refresh" content="0; URL = http://www.example.com/redirected"/></head></html>', 'http://www.example.com/redirected'],
  34. ['<html><head><meta http-equiv="refresh" content="0;URL= http://www.example.com/redirected "/></head></html>', 'http://www.example.com/redirected'],
  35. ['<html><head><meta http-equiv="refresh" content="0;url=http://www.example.com/redirected "/></head></html>', 'http://www.example.com/redirected'],
  36. ['<html><head><noscript><meta http-equiv="refresh" content="0;URL=http://www.example.com/redirected"/></noscript></head></head></html>', 'http://www.example.com/redirected'],
  37. // Non-zero timeout should not result in a redirect.
  38. ['<html><head><meta http-equiv="refresh" content="4; URL=http://www.example.com/redirected"/></head></html>', 'http://www.example.com/foo/foobar'],
  39. ['<html><body></body></html>', 'http://www.example.com/foo/foobar'],
  40. // HTML 5 allows the meta tag to be placed in head or body.
  41. ['<html><body><meta http-equiv="refresh" content="0;url=http://www.example.com/redirected"/></body></html>', 'http://www.example.com/redirected'],
  42. // Valid meta refresh should not be followed if disabled.
  43. ['<html><head><meta http-equiv="refresh" content="0;URL=http://www.example.com/redirected"/></head></html>', 'http://www.example.com/foo/foobar', FALSE],
  44. 'drupal-1' => ['<html><head><meta http-equiv="Refresh" content="0; URL=/update.php/start?id=2&op=do_nojs" /></body></html>', 'http://www.example.com/update.php/start?id=2&op=do_nojs'],
  45. 'drupal-2' => ['<html><head><noscript><meta http-equiv="Refresh" content="0; URL=/update.php/start?id=2&op=do_nojs" /></noscript></body></html>', 'http://www.example.com/update.php/start?id=2&op=do_nojs'],
  46. ];
  47. }
  48. /**
  49. * @covers ::request
  50. */
  51. public function testBackForwardMetaRefresh() {
  52. $client = new TestClient();
  53. $client->followMetaRefresh();
  54. // First request.
  55. $client->request('GET', 'http://www.example.com/first-page');
  56. $content = '<html><head><meta http-equiv="Refresh" content="0; URL=/refreshed" /></body></html>';
  57. $client->setNextResponse(new Response($content, 200));
  58. $client->request('GET', 'http://www.example.com/refresh-from-here');
  59. $this->assertEquals('http://www.example.com/refreshed', $client->getRequest()->getUri());
  60. $client->back();
  61. $this->assertEquals('http://www.example.com/first-page', $client->getRequest()->getUri());
  62. $client->forward();
  63. $this->assertEquals('http://www.example.com/refreshed', $client->getRequest()->getUri());
  64. }
  65. }
  66. /**
  67. * Special client that can return a given response on the first doRequest().
  68. */
  69. class TestClient extends DrupalMinkClient {
  70. protected $nextResponse = NULL;
  71. public function setNextResponse(Response $response) {
  72. $this->nextResponse = $response;
  73. }
  74. protected function doRequest($request) {
  75. if (NULL === $this->nextResponse) {
  76. return new Response();
  77. }
  78. $response = $this->nextResponse;
  79. $this->nextResponse = NULL;
  80. return $response;
  81. }
  82. }