View source
<?php
namespace Drupal\Tests\datalayer\Kernel;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Language\Language;
use Drupal\node\Entity\Node;
use Drupal\user\Entity\User;
use Drupal\taxonomy\Entity\Term;
use Drupal\node\Entity\NodeType;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
use Symfony\Component\Routing\Route;
class DataLayerKernelTest extends KernelTestBase {
public static $modules = [
'datalayer',
'system',
'user',
'node',
'taxonomy',
'field',
'text',
];
protected $user;
protected $node;
protected $term;
public function setUp() {
parent::setUp();
$this
->installEntitySchema('user');
$this
->installEntitySchema('node');
$this
->installEntitySchema('taxonomy_term');
$this
->installConfig([
'system',
'datalayer',
]);
$this
->setupMockFieldConfig();
}
public function setupMockFieldConfig() {
NodeType::create([
'type' => 'article',
])
->save();
FieldStorageConfig::create([
'entity_type' => 'node',
'field_name' => 'field_tags',
'type' => 'entity_reference',
'settings' => [
'target_type' => 'taxonomy_term',
],
])
->save();
FieldConfig::create([
'field_name' => 'field_tags',
'entity_type' => 'node',
'bundle' => 'article',
])
->save();
}
public function testDataLayerDefaults() {
$this
->assertEqual([
'drupalLanguage' => Language::$defaultValues['id'],
'drupalCountry' => $this
->config('system.date')
->get('country.default'),
'siteName' => $this
->config('system.site')
->get('name'),
], _datalayer_defaults());
}
public function testDataLayerAddWillAddData() {
$this
->setupEmptyDataLayer();
$this
->assertEqual([
'foo' => 'bar',
], datalayer_add([
'foo' => 'bar',
]));
}
public function testDataLayerAddDoesNotOverwriteByDefault() {
$this
->setupEmptyDataLayer();
datalayer_add([
'foo' => 'bar',
]);
$this
->assertEqual([
'foo' => 'bar',
], datalayer_add([
'foo' => 'baz',
]));
}
public function testDataLayerAddWillOverwriteWithFlag() {
$this
->setupEmptyDataLayer();
datalayer_add([
'foo' => 'bar',
]);
$this
->assertEqual([
'foo' => 'baz',
], datalayer_add([
'foo' => 'baz',
], TRUE));
}
public function testDataLayerMenuGetAnyObjectReturnsNullWithoutContentEntityInterface() {
$result = _datalayer_menu_get_any_object();
$this
->assertNull($result);
}
public function testDataLayerMenuGetAnyObjectReturnsObject() {
$this
->setupMockRouteMatch();
$object = _datalayer_menu_get_any_object();
$this
->assertTrue(is_object($object));
$this
->assertEqual($object
->getEntityTypeId(), 'node');
}
public function testDataLayerGetEntityTermsReturnsEmptyArray() {
$this
->setupMockNode();
$terms = _datalayer_get_entity_terms($this->node);
$this
->assertEqual([], $terms);
}
public function testDataLayerGetEntityTermsReturnsTermArray() {
$this
->setupMockNodeWithTerm();
$terms = _datalayer_get_entity_terms($this->node);
$this
->assertEqual([
'tags' => [
1 => 'someTag',
],
], $terms);
}
public function testDataLayerGetEntityDataReturnsEntityDataArray() {
$this
->setupEmptyDataLayer();
$this
->setupMockNodeWithTerm();
$entity_data = _datalayer_get_entity_data($this->node);
$this
->assertEqual($this
->getExpectedEntityDataArray(), $entity_data);
}
public function setupMockUser() {
$data = [
'uid' => 1,
'name' => 'admin',
'password' => 'password',
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
];
$this->user = User::create($data);
$this->user
->save();
}
public function setupEmptyDataLayer() {
$data =& drupal_static('datalayer_add', []);
}
public function setupMockNode() {
$this
->setupMockUser();
$data = [
'uid' => $this->user
->id(),
'name' => 'admin',
'type' => 'article',
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
'title' => 'My Article',
'status' => 1,
'nid' => 1,
'vid' => 1,
'type' => 'article',
'created' => '1435019805',
];
$this->node = Node::create($data);
$this->node
->save();
}
public function setupMockNodeWithTerm() {
$this
->setupMockNode();
$this
->setupMockEntityTerm();
$this->node
->set('field_tags', [
'target_id' => $this->term
->id(),
]);
}
public function setupMockRouteMatch() {
$this
->setupMockNode();
$request =& drupal_static(__FUNCTION__);
if (!$request) {
$request = \Drupal::request()
->create('/node/1', 'GET', [
'node' => $this->node,
]);
$request->attributes
->set(RouteObjectInterface::ROUTE_OBJECT, new Route('/node/{node}', [
'node' => 1,
]));
$request->attributes
->set(RouteObjectInterface::ROUTE_NAME, 'entity.node.canonical');
$request->attributes
->set('node', $this->node);
$this->container
->get('request_stack')
->push($request);
}
}
public function setupMockEntityTerm() {
$this->term = Term::create([
'name' => 'someTag',
'vid' => 'tags',
'tid' => 1,
]);
$this->term
->save();
}
public function getExpectedEntityDataArray() {
return [
'entityType' => 'node',
'entityBundle' => 'article',
'entityId' => '1',
'entityUuid' => $this->node->uuid
->getString(),
'entityTitle' => 'My Article',
'entityLangcode' => 'und',
'entityVid' => '1',
'entityName' => 'admin',
'entityUid' => '1',
'entityCreated' => '1435019805',
'entityStatus' => '1',
'entityTaxonomy' => [
'tags' => [
1 => 'someTag',
],
],
];
}
}