Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
HistoryTest.php
Go to the documentation of this file.
1 <?php
7 
11 class HistoryTest extends \PHPUnit\Framework\TestCase
12 {
16  protected $objectManager;
17 
21  protected $adminHelperMock;
22 
26  protected $commentsHistory;
27 
31  protected $coreRegistryMock;
32 
36  protected $localeDateMock;
37 
41  protected $contextMock;
42 
43  protected function setUp()
44  {
45  $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
46 
47  $this->coreRegistryMock = $this->createMock(\Magento\Framework\Registry::class);
48  $this->adminHelperMock = $this->createMock(\Magento\Sales\Helper\Admin::class);
49 
50  $this->contextMock = $this->getMockBuilder(\Magento\Backend\Block\Template\Context::class)
51  ->disableOriginalConstructor()
52  ->setMethods(['getLocaleDate'])
53  ->getMock();
54 
55  $this->localeDateMock = $this->getMockBuilder(\Magento\Framework\Stdlib\DateTime\TimezoneInterface::class)
56  ->getMock();
57 
58  $this->contextMock->expects($this->any())->method('getLocaleDate')->will(
59  $this->returnValue($this->localeDateMock)
60  );
61 
62  $this->commentsHistory = $this->objectManager->getObject(
63  \Magento\Sales\Block\Adminhtml\Order\View\Tab\History::class,
64  [
65  'adminHelper' => $this->adminHelperMock,
66  'registry' => $this->coreRegistryMock,
67  'context' => $this->contextMock,
68  'localeDate' => $this->localeDateMock
69  ]
70  );
71  }
72 
73  public function testGetItemComment()
74  {
75  $expectation = 'Authorized amount of £20.00 Transaction ID: &quot;XXX123123XXX&quot;';
76  $item['comment'] = 'Authorized amount of £20.00 Transaction ID: "XXX123123XXX"';
77  $this->adminHelperMock->expects($this->once())
78  ->method('escapeHtmlWithLinks')
79  ->with($item['comment'], ['b', 'br', 'strong', 'i', 'u', 'a'])
80  ->willReturn($expectation);
81  $this->assertEquals($expectation, $this->commentsHistory->getItemComment($item));
82  }
83 
84  public function testGetItemCommentIsNotSet()
85  {
86  $item = [];
87  $this->adminHelperMock->expects($this->never())->method('escapeHtmlWithLinks');
88  $this->assertEquals('', $this->commentsHistory->getItemComment($item));
89  }
90 
91  public function testGetItemCreatedAtDate()
92  {
93  $date = new \DateTime;
94  $item = ['created_at' => $date ];
95 
96  $this->localeDateMock->expects($this->once())
97  ->method('formatDateTime')
98  ->with($date, \IntlDateFormatter::MEDIUM, \IntlDateFormatter::NONE)
99  ->willReturn('date');
100 
101  $this->assertEquals('date', $this->commentsHistory->getItemCreatedAt($item));
102  }
103 
104  public function testGetItemCreatedAtTime()
105  {
106  $date = new \DateTime;
107  $item = ['created_at' => $date ];
108 
109  $this->localeDateMock->expects($this->once())
110  ->method('formatDateTime')
111  ->with($date, \IntlDateFormatter::NONE, \IntlDateFormatter::MEDIUM)
112  ->willReturn('time');
113 
114  $this->assertEquals('time', $this->commentsHistory->getItemCreatedAt($item, 'time'));
115  }
116 
117  public function testGetItemCreatedAtEmpty()
118  {
119  $item = ['title' => "Test" ];
120 
121  $this->localeDateMock->expects($this->never())->method('formatDateTime');
122  $this->assertEquals('', $this->commentsHistory->getItemCreatedAt($item));
123  $this->assertEquals('', $this->commentsHistory->getItemCreatedAt($item, 'time'));
124  }
125 }