LinkTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. public function testBaseUriIsOptionalWhenLinkUrlIsAbsolute()
  25. {
  26. $dom = new \DOMDocument();
  27. $dom->loadHTML('<html><a href="https://example.com/foo">foo</a></html>');
  28. $link = new Link($dom->getElementsByTagName('a')->item(0));
  29. $this->assertSame('https://example.com/foo', $link->getUri());
  30. }
  31. /**
  32. * @expectedException \InvalidArgumentException
  33. */
  34. public function testAbsoluteBaseUriIsMandatoryWhenLinkUrlIsRelative()
  35. {
  36. $dom = new \DOMDocument();
  37. $dom->loadHTML('<html><a href="/foo">foo</a></html>');
  38. $link = new Link($dom->getElementsByTagName('a')->item(0), 'example.com');
  39. $link->getUri();
  40. }
  41. public function testGetNode()
  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($node, $link->getNode(), '->getNode() returns the node associated with the link');
  48. }
  49. public function testGetMethod()
  50. {
  51. $dom = new \DOMDocument();
  52. $dom->loadHTML('<html><a href="/foo">foo</a></html>');
  53. $node = $dom->getElementsByTagName('a')->item(0);
  54. $link = new Link($node, 'http://example.com/');
  55. $this->assertEquals('GET', $link->getMethod(), '->getMethod() returns the method of the link');
  56. $link = new Link($node, 'http://example.com/', 'post');
  57. $this->assertEquals('POST', $link->getMethod(), '->getMethod() returns the method of the link');
  58. }
  59. /**
  60. * @dataProvider getGetUriTests
  61. */
  62. public function testGetUri($url, $currentUri, $expected)
  63. {
  64. $dom = new \DOMDocument();
  65. $dom->loadHTML(sprintf('<html><a href="%s">foo</a></html>', $url));
  66. $link = new Link($dom->getElementsByTagName('a')->item(0), $currentUri);
  67. $this->assertEquals($expected, $link->getUri());
  68. }
  69. /**
  70. * @dataProvider getGetUriTests
  71. */
  72. public function testGetUriOnArea($url, $currentUri, $expected)
  73. {
  74. $dom = new \DOMDocument();
  75. $dom->loadHTML(sprintf('<html><map><area href="%s" /></map></html>', $url));
  76. $link = new Link($dom->getElementsByTagName('area')->item(0), $currentUri);
  77. $this->assertEquals($expected, $link->getUri());
  78. }
  79. /**
  80. * @dataProvider getGetUriTests
  81. */
  82. public function testGetUriOnLink($url, $currentUri, $expected)
  83. {
  84. $dom = new \DOMDocument();
  85. $dom->loadHTML(sprintf('<html><head><link href="%s" /></head></html>', $url));
  86. $link = new Link($dom->getElementsByTagName('link')->item(0), $currentUri);
  87. $this->assertEquals($expected, $link->getUri());
  88. }
  89. public function getGetUriTests()
  90. {
  91. return [
  92. ['/foo', 'http://localhost/bar/foo/', 'http://localhost/foo'],
  93. ['/foo', 'http://localhost/bar/foo', 'http://localhost/foo'],
  94. ['
  95. /foo', 'http://localhost/bar/foo/', 'http://localhost/foo'],
  96. ['/foo
  97. ', 'http://localhost/bar/foo', 'http://localhost/foo'],
  98. ['foo', 'http://localhost/bar/foo/', 'http://localhost/bar/foo/foo'],
  99. ['foo', 'http://localhost/bar/foo', 'http://localhost/bar/foo'],
  100. ['', 'http://localhost/bar/', 'http://localhost/bar/'],
  101. ['#', 'http://localhost/bar/', 'http://localhost/bar/#'],
  102. ['#bar', 'http://localhost/bar?a=b', 'http://localhost/bar?a=b#bar'],
  103. ['#bar', 'http://localhost/bar/#foo', 'http://localhost/bar/#bar'],
  104. ['?a=b', 'http://localhost/bar#foo', 'http://localhost/bar?a=b'],
  105. ['?a=b', 'http://localhost/bar/', 'http://localhost/bar/?a=b'],
  106. ['http://login.foo.com/foo', 'http://localhost/bar/', 'http://login.foo.com/foo'],
  107. ['https://login.foo.com/foo', 'https://localhost/bar/', 'https://login.foo.com/foo'],
  108. ['mailto:foo@bar.com', 'http://localhost/foo', 'mailto:foo@bar.com'],
  109. // tests schema relative URL (issue #7169)
  110. ['//login.foo.com/foo', 'http://localhost/bar/', 'http://login.foo.com/foo'],
  111. ['//login.foo.com/foo', 'https://localhost/bar/', 'https://login.foo.com/foo'],
  112. ['?foo=2', 'http://localhost?foo=1', 'http://localhost?foo=2'],
  113. ['?foo=2', 'http://localhost/?foo=1', 'http://localhost/?foo=2'],
  114. ['?foo=2', 'http://localhost/bar?foo=1', 'http://localhost/bar?foo=2'],
  115. ['?foo=2', 'http://localhost/bar/?foo=1', 'http://localhost/bar/?foo=2'],
  116. ['?bar=2', 'http://localhost?foo=1', 'http://localhost?bar=2'],
  117. ['foo', 'http://login.foo.com/bar/baz?/query/string', 'http://login.foo.com/bar/foo'],
  118. ['.', 'http://localhost/foo/bar/baz', 'http://localhost/foo/bar/'],
  119. ['./', 'http://localhost/foo/bar/baz', 'http://localhost/foo/bar/'],
  120. ['./foo', 'http://localhost/foo/bar/baz', 'http://localhost/foo/bar/foo'],
  121. ['..', 'http://localhost/foo/bar/baz', 'http://localhost/foo/'],
  122. ['../', 'http://localhost/foo/bar/baz', 'http://localhost/foo/'],
  123. ['../foo', 'http://localhost/foo/bar/baz', 'http://localhost/foo/foo'],
  124. ['../..', 'http://localhost/foo/bar/baz', 'http://localhost/'],
  125. ['../../', 'http://localhost/foo/bar/baz', 'http://localhost/'],
  126. ['../../foo', 'http://localhost/foo/bar/baz', 'http://localhost/foo'],
  127. ['../../foo', 'http://localhost/bar/foo/', 'http://localhost/foo'],
  128. ['../bar/../../foo', 'http://localhost/bar/foo/', 'http://localhost/foo'],
  129. ['../bar/./../../foo', 'http://localhost/bar/foo/', 'http://localhost/foo'],
  130. ['../../', 'http://localhost/', 'http://localhost/'],
  131. ['../../', 'http://localhost', 'http://localhost/'],
  132. ['/foo', 'http://localhost?bar=1', 'http://localhost/foo'],
  133. ['/foo', 'http://localhost#bar', 'http://localhost/foo'],
  134. ['/foo', 'file:///', 'file:///foo'],
  135. ['/foo', 'file:///bar/baz', 'file:///foo'],
  136. ['foo', 'file:///', 'file:///foo'],
  137. ['foo', 'file:///bar/baz', 'file:///bar/foo'],
  138. ];
  139. }
  140. }