LinkTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\DomCrawler\Tests;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\DomCrawler\Link;
  13. class LinkTest extends TestCase
  14. {
  15. /**
  16. * @expectedException \LogicException
  17. */
  18. public function testConstructorWithANonATag()
  19. {
  20. $dom = new \DOMDocument();
  21. $dom->loadHTML('<html><div><div></html>');
  22. new Link($dom->getElementsByTagName('div')->item(0), 'http://www.example.com/');
  23. }
  24. /**
  25. * @expectedException \InvalidArgumentException
  26. */
  27. public function testConstructorWithAnInvalidCurrentUri()
  28. {
  29. $dom = new \DOMDocument();
  30. $dom->loadHTML('<html><a href="/foo">foo</a></html>');
  31. new Link($dom->getElementsByTagName('a')->item(0), 'example.com');
  32. }
  33. public function testGetNode()
  34. {
  35. $dom = new \DOMDocument();
  36. $dom->loadHTML('<html><a href="/foo">foo</a></html>');
  37. $node = $dom->getElementsByTagName('a')->item(0);
  38. $link = new Link($node, 'http://example.com/');
  39. $this->assertEquals($node, $link->getNode(), '->getNode() returns the node associated with the link');
  40. }
  41. public function testGetMethod()
  42. {
  43. $dom = new \DOMDocument();
  44. $dom->loadHTML('<html><a href="/foo">foo</a></html>');
  45. $node = $dom->getElementsByTagName('a')->item(0);
  46. $link = new Link($node, 'http://example.com/');
  47. $this->assertEquals('GET', $link->getMethod(), '->getMethod() returns the method of the link');
  48. $link = new Link($node, 'http://example.com/', 'post');
  49. $this->assertEquals('POST', $link->getMethod(), '->getMethod() returns the method of the link');
  50. }
  51. /**
  52. * @dataProvider getGetUriTests
  53. */
  54. public function testGetUri($url, $currentUri, $expected)
  55. {
  56. $dom = new \DOMDocument();
  57. $dom->loadHTML(sprintf('<html><a href="%s">foo</a></html>', $url));
  58. $link = new Link($dom->getElementsByTagName('a')->item(0), $currentUri);
  59. $this->assertEquals($expected, $link->getUri());
  60. }
  61. /**
  62. * @dataProvider getGetUriTests
  63. */
  64. public function testGetUriOnArea($url, $currentUri, $expected)
  65. {
  66. $dom = new \DOMDocument();
  67. $dom->loadHTML(sprintf('<html><map><area href="%s" /></map></html>', $url));
  68. $link = new Link($dom->getElementsByTagName('area')->item(0), $currentUri);
  69. $this->assertEquals($expected, $link->getUri());
  70. }
  71. /**
  72. * @dataProvider getGetUriTests
  73. */
  74. public function testGetUriOnLink($url, $currentUri, $expected)
  75. {
  76. $dom = new \DOMDocument();
  77. $dom->loadHTML(sprintf('<html><head><link href="%s" /></head></html>', $url));
  78. $link = new Link($dom->getElementsByTagName('link')->item(0), $currentUri);
  79. $this->assertEquals($expected, $link->getUri());
  80. }
  81. public function getGetUriTests()
  82. {
  83. return [
  84. ['/foo', 'http://localhost/bar/foo/', 'http://localhost/foo'],
  85. ['/foo', 'http://localhost/bar/foo', 'http://localhost/foo'],
  86. ['
  87. /foo', 'http://localhost/bar/foo/', 'http://localhost/foo'],
  88. ['/foo
  89. ', 'http://localhost/bar/foo', 'http://localhost/foo'],
  90. ['foo', 'http://localhost/bar/foo/', 'http://localhost/bar/foo/foo'],
  91. ['foo', 'http://localhost/bar/foo', 'http://localhost/bar/foo'],
  92. ['', 'http://localhost/bar/', 'http://localhost/bar/'],
  93. ['#', 'http://localhost/bar/', 'http://localhost/bar/#'],
  94. ['#bar', 'http://localhost/bar?a=b', 'http://localhost/bar?a=b#bar'],
  95. ['#bar', 'http://localhost/bar/#foo', 'http://localhost/bar/#bar'],
  96. ['?a=b', 'http://localhost/bar#foo', 'http://localhost/bar?a=b'],
  97. ['?a=b', 'http://localhost/bar/', 'http://localhost/bar/?a=b'],
  98. ['http://login.foo.com/foo', 'http://localhost/bar/', 'http://login.foo.com/foo'],
  99. ['https://login.foo.com/foo', 'https://localhost/bar/', 'https://login.foo.com/foo'],
  100. ['mailto:foo@bar.com', 'http://localhost/foo', 'mailto:foo@bar.com'],
  101. // tests schema relative URL (issue #7169)
  102. ['//login.foo.com/foo', 'http://localhost/bar/', 'http://login.foo.com/foo'],
  103. ['//login.foo.com/foo', 'https://localhost/bar/', 'https://login.foo.com/foo'],
  104. ['?foo=2', 'http://localhost?foo=1', 'http://localhost?foo=2'],
  105. ['?foo=2', 'http://localhost/?foo=1', 'http://localhost/?foo=2'],
  106. ['?foo=2', 'http://localhost/bar?foo=1', 'http://localhost/bar?foo=2'],
  107. ['?foo=2', 'http://localhost/bar/?foo=1', 'http://localhost/bar/?foo=2'],
  108. ['?bar=2', 'http://localhost?foo=1', 'http://localhost?bar=2'],
  109. ['foo', 'http://login.foo.com/bar/baz?/query/string', 'http://login.foo.com/bar/foo'],
  110. ['.', 'http://localhost/foo/bar/baz', 'http://localhost/foo/bar/'],
  111. ['./', 'http://localhost/foo/bar/baz', 'http://localhost/foo/bar/'],
  112. ['./foo', 'http://localhost/foo/bar/baz', 'http://localhost/foo/bar/foo'],
  113. ['..', 'http://localhost/foo/bar/baz', 'http://localhost/foo/'],
  114. ['../', 'http://localhost/foo/bar/baz', 'http://localhost/foo/'],
  115. ['../foo', 'http://localhost/foo/bar/baz', 'http://localhost/foo/foo'],
  116. ['../..', 'http://localhost/foo/bar/baz', 'http://localhost/'],
  117. ['../../', 'http://localhost/foo/bar/baz', 'http://localhost/'],
  118. ['../../foo', 'http://localhost/foo/bar/baz', 'http://localhost/foo'],
  119. ['../../foo', 'http://localhost/bar/foo/', 'http://localhost/foo'],
  120. ['../bar/../../foo', 'http://localhost/bar/foo/', 'http://localhost/foo'],
  121. ['../bar/./../../foo', 'http://localhost/bar/foo/', 'http://localhost/foo'],
  122. ['../../', 'http://localhost/', 'http://localhost/'],
  123. ['../../', 'http://localhost', 'http://localhost/'],
  124. ['/foo', 'http://localhost?bar=1', 'http://localhost/foo'],
  125. ['/foo', 'http://localhost#bar', 'http://localhost/foo'],
  126. ['/foo', 'file:///', 'file:///foo'],
  127. ['/foo', 'file:///bar/baz', 'file:///foo'],
  128. ['foo', 'file:///', 'file:///foo'],
  129. ['foo', 'file:///bar/baz', 'file:///bar/foo'],
  130. ];
  131. }
  132. }