LinkTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Cms\Test\Unit\Block\Widget\Page;
  7. class LinkTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @var \Magento\Cms\Block\Widget\Page\Link
  11. */
  12. protected $linkElement;
  13. /**
  14. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  15. */
  16. protected $objectManager;
  17. /**
  18. * @var \Magento\Cms\Helper\Page|\PHPUnit_Framework_MockObject_MockObject
  19. */
  20. protected $mockCmsPage;
  21. /**
  22. * @var \Magento\Cms\Model\ResourceModel\Page|\PHPUnit_Framework_MockObject_MockObject
  23. */
  24. protected $mockResourcePage;
  25. protected function setUp()
  26. {
  27. $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  28. $this->mockCmsPage = $this->createMock(\Magento\Cms\Helper\Page::class);
  29. $this->mockResourcePage = $this->createMock(\Magento\Cms\Model\ResourceModel\Page::class);
  30. $this->linkElement = $this->objectManager->getObject(
  31. \Magento\Cms\Block\Widget\Page\Link::class,
  32. [
  33. 'cmsPage' => $this->mockCmsPage,
  34. 'resourcePage' => $this->mockResourcePage,
  35. ]
  36. );
  37. }
  38. protected function tearDown()
  39. {
  40. $this->linkElement = null;
  41. }
  42. public function testGetHrefEmpty()
  43. {
  44. $this->assertEmpty($this->linkElement->getHref());
  45. }
  46. public function testGetHref()
  47. {
  48. $href = 'localhost';
  49. $this->linkElement->setData('href', $href);
  50. $this->assertEquals($href, $this->linkElement->getHref());
  51. }
  52. public function testGetHrefByPageId()
  53. {
  54. $href = 'pagelink';
  55. $this->mockCmsPage->expects($this->once())
  56. ->method('getPageUrl')
  57. ->willReturn($href);
  58. $this->linkElement->setData('page_id', 1);
  59. $this->assertEquals($href, $this->linkElement->getHref());
  60. }
  61. public function testGetTitleEmpty()
  62. {
  63. $this->assertEmpty($this->linkElement->getTitle());
  64. }
  65. public function testGetTitle()
  66. {
  67. $title = 'Title';
  68. $this->linkElement->setData('title', $title);
  69. $this->assertEquals($title, $this->linkElement->getTitle());
  70. }
  71. public function testGetTitleByPageId()
  72. {
  73. $pageId = 1;
  74. $title = 'Title by page id';
  75. $this->mockResourcePage->expects($this->once())
  76. ->method('getCmsPageTitleById')
  77. ->with($pageId)
  78. ->willReturn($title);
  79. $this->linkElement->setData('page_id', $pageId);
  80. $this->assertEquals($title, $this->linkElement->getTitle());
  81. }
  82. public function testGetTitleByHref()
  83. {
  84. $href = 'localhost';
  85. $title = 'Title by href';
  86. $this->mockResourcePage->expects($this->once())
  87. ->method('setStore')
  88. ->willReturnSelf();
  89. $this->mockResourcePage->expects($this->once())
  90. ->method('getCmsPageTitleByIdentifier')
  91. ->with($href)
  92. ->willReturn($title);
  93. $this->linkElement->setData('href', $href);
  94. $this->assertEquals($title, $this->linkElement->getTitle());
  95. }
  96. public function testGetLabelByAnchorText()
  97. {
  98. $label = 'Test label';
  99. $this->linkElement->setData('anchor_text', $label);
  100. $this->assertEquals($label, $this->linkElement->getLabel());
  101. }
  102. public function testGetLabelLikeTitle()
  103. {
  104. $label = 'Test title';
  105. $this->linkElement->setData('title', $label);
  106. $this->assertEquals($label, $this->linkElement->getLabel());
  107. }
  108. public function testGetLabelByHref()
  109. {
  110. $href = 'localhost';
  111. $label = 'Label by href';
  112. $this->mockResourcePage->expects($this->once())
  113. ->method('setStore')
  114. ->willReturnSelf();
  115. $this->mockResourcePage->expects($this->once())
  116. ->method('getCmsPageTitleByIdentifier')
  117. ->with($href)
  118. ->willReturn($label);
  119. $this->linkElement->setData('href', $href);
  120. $this->assertEquals($label, $this->linkElement->getLabel());
  121. }
  122. public function testGetLabelByPageId()
  123. {
  124. $pageId = 1;
  125. $label = 'Label by page id';
  126. $this->mockResourcePage->expects($this->once())
  127. ->method('getCmsPageTitleById')
  128. ->with($pageId)
  129. ->willReturn($label);
  130. $this->linkElement->setData('page_id', $pageId);
  131. $this->assertEquals($label, $this->linkElement->getLabel());
  132. }
  133. public function testGetLabelEmpty()
  134. {
  135. $this->assertEmpty($this->linkElement->getLabel());
  136. }
  137. }