View source
<?php
namespace Drupal\Tests\radioactivity\Unit;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Site\Settings;
use Drupal\Tests\UnitTestCase;
use Drupal\radioactivity\IncidentInterface;
use Drupal\radioactivity\RestIncidentStorage;
class RestIncidentStorageTest extends UnitTestCase {
private $sut;
public function setUp() : void {
parent::setUp();
$this->sut = $this
->getMockBuilder(RestIncidentStorage::class)
->disableOriginalConstructor()
->setMethods([
'getDefaultEndpoint',
'getIncidentsFromStorage',
'clearIncidentStorage',
])
->getMock();
new Settings([
'hash_salt' => 'liesjeleerdelotjelopen',
]);
}
public function testAddIncident() {
$incident = $this
->getMockBuilder(IncidentInterface::class)
->disableOriginalConstructor()
->getMock();
$this
->expectException("Exception");
$this
->expectExceptionMessage("The Radioactivity rest endpoint expects incidents to be added somewhere else.");
$this->sut
->addIncident($incident);
}
public function testGetSingleIncident() {
$incidentData = Json::decode('[[{"fn":"field_name","et":"entity_type","id":"99","e":10,"h":"fb05739ec66040df9e6c53330755753c02e73621"}]]');
$this->sut
->expects($this
->once())
->method('getIncidentsFromStorage')
->will($this
->returnValue($incidentData));
$result = $this->sut
->getIncidents();
$this
->assertCount(1, $result);
$this
->assertInstanceOf(IncidentInterface::class, $result[0]);
}
public function testGetInvalidIncident() {
$incidentData = Json::decode('[[{"fn":"field_name","et":"entity_type","id":"99","e":10,"h":"invalid-hash"}]]');
$this->sut
->expects($this
->once())
->method('getIncidentsFromStorage')
->will($this
->returnValue($incidentData));
$result = $this->sut
->getIncidents();
$this
->assertCount(0, $result);
}
public function testGetMultipleIncidents() {
$this->sut
->expects($this
->once())
->method('getIncidentsFromStorage')
->will($this
->returnValue($this
->getMultipleIncidentData()));
$result = $this->sut
->getIncidents();
$this
->assertCount(4, $result);
$this
->assertInstanceOf(IncidentInterface::class, $result[0]);
}
public function testGetIncidentsByType() {
$this->sut
->expects($this
->any())
->method('getIncidentsFromStorage')
->will($this
->returnValue($this
->getMultipleIncidentData()));
$result = $this->sut
->getIncidentsByType();
$this
->assertEquals([
'entity_type',
'node',
], array_keys($result));
$this
->assertEquals([
99,
88,
], array_keys($result['entity_type']));
$this
->assertInstanceOf(IncidentInterface::class, $result['entity_type'][99][0]);
$this
->assertEquals([
123,
], array_keys($result['node']));
$this
->assertInstanceOf(IncidentInterface::class, $result['node'][123][0]);
$result = $this->sut
->getIncidentsByType('entity_type');
$this
->assertEquals([
'entity_type',
], array_keys($result));
$this
->assertEquals([
99,
88,
], array_keys($result['entity_type']));
$this
->assertInstanceOf(IncidentInterface::class, $result['entity_type'][99][0]);
$result = $this->sut
->getIncidentsByType('node');
$this
->assertEquals([
123,
], array_keys($result['node']));
$this
->assertInstanceOf(IncidentInterface::class, $result['node'][123][0]);
$result = $this->sut
->getIncidentsByType('unknown_entity');
$this
->assertArrayNotHasKey('unknown_entity', $result);
}
public function testClearIncidents() {
$this->sut
->expects($this
->once())
->method('clearIncidentStorage');
$this->sut
->clearIncidents();
}
private function getMultipleIncidentData() {
$jsonData = '[[{"fn":"field_name","et":"entity_type","id":"99","e":10,"h":"fb05739ec66040df9e6c53330755753c02e73621"}],' . PHP_EOL . '[{"fn":"field_name","et":"entity_type","id":"88","e":10,"h":"5aae235691b06918a2a8eb3833bfb22a67d9b183"}],' . PHP_EOL . '[{"fn":"field_name","et":"entity_type","id":"88","e":10,"h":"5aae235691b06918a2a8eb3833bfb22a67d9b183"}],' . PHP_EOL . '[{"fn":"field_other","et":"node","id":"123","e":10,"h":"dedd866aefddfd94049f6fd8cf1b8d02065343e6"}]]';
return Json::decode($jsonData);
}
}