View source
<?php
namespace Drupal\Tests\yoast_seo\Kernel;
use Drupal\Core\Datetime\Entity\DateFormat;
use Drupal\KernelTests\KernelTestBase;
use Drupal\node\Entity\Node;
use Drupal\Tests\node\Traits\ContentTypeCreationTrait;
use Drupal\Tests\node\Traits\NodeCreationTrait;
use Drupal\Tests\yoast_seo\Traits\YoastSEOTestTrait;
class EntityAnalyserTest extends KernelTestBase {
use YoastSEOTestTrait;
use ContentTypeCreationTrait {
createContentType as drupalCreateContentType;
}
use NodeCreationTrait {
createNode as drupalCreateNode;
}
protected $entityAnalyzer;
protected static $modules = [
'yoast_seo',
'node',
'datetime',
'user',
'system',
'filter',
'field',
'text',
'token',
'metatag',
];
public function setUp() {
parent::setUp();
$this
->installSchema('system', 'sequences');
$this
->installSchema('node', 'node_access');
$this
->installEntitySchema('user');
$this
->installEntitySchema('node');
$this
->installEntitySchema('date_format');
$this
->installConfig('filter');
$this
->installConfig('node');
$this
->installConfig('metatag');
$this
->drupalCreateContentType([
'type' => 'article',
'name' => 'Article',
'display_submitted' => FALSE,
]);
DateFormat::create([
'id' => 'fallback',
'label' => 'Fallback',
'pattern' => 'Y-m-d',
])
->save();
$this
->createYoastSeoField('node', 'article', 'field_seo', 'SEO');
$this->entityAnalyzer = $this->container
->get('yoast_seo.entity_analyser');
}
public function testEntityPreviewWithUnsavedNode() {
$unsaved_node = Node::create([
'type' => 'article',
'title' => $this
->randomMachineName(8),
'body' => [
[
'value' => $this
->randomMachineName(32),
'format' => filter_default_format(),
],
],
'uid' => 0,
]);
$preview_data = $this->entityAnalyzer
->createEntityPreview($unsaved_node);
$this
->assertNotEmpty($preview_data['title']);
$this
->assertNotEmpty($preview_data['text']);
$this
->assertEmpty($preview_data['url']);
}
public function testEntityPreviewWithSavedNode() {
$node = $this
->drupalCreateNode([
'type' => 'article',
]);
$preview_data = $this->entityAnalyzer
->createEntityPreview($node);
$this
->assertNotEmpty($preview_data['title']);
$this
->assertNotEmpty($preview_data['text']);
$this
->assertNotEmpty($preview_data['url']);
}
}