View source
<?php
namespace Drupal\search_autocomplete\Tests\Views;
use Drupal;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Component\Utility\Html;
use Drupal\node\Entity\Node;
use Drupal\views\Tests\ViewTestBase;
class CallbackViewsTest extends ViewTestBase {
public static $modules = [
'search',
'image',
'user',
'node',
'views_ui',
'search_autocomplete',
];
public $adminUser;
protected $strictConfigSchema = FALSE;
protected $nodeStorage;
public static function getInfo() {
return [
'name' => 'Callback view configurationt tests.',
'description' => 'Tests the callback view display.',
'group' => 'Search Autocomplete',
];
}
public function testDefaultView() {
$this
->drupalGet("admin/structure/views");
$this
->assertRaw(t('Nodes Autocompletion Callbacks'));
$this
->assertRaw(t('autocompletion_callbacks_nodes'));
$this
->assertRaw(t('Words Autocompletion Callbacks'));
$this
->assertRaw(t('autocompletion_callbacks_words'));
}
public function testDefaultViewContent() {
$actual_json = $this
->drupalGet("callback/nodes");
$expected = [];
$this
->assertIdentical($actual_json, json_encode($expected), 'The expected JSON output was found.');
$this
->createNodes(5, "article", $expected);
$this
->createNodes(5, "page", $expected);
$this
->drupalLogout();
$actual_json = $this
->drupalGet("callback/nodes");
$expected_string = json_encode($expected);
$this
->assertIdentical($actual_json, $expected_string);
$actual_json = $this
->drupalGet("callback/nodes");
$this
->assertIdentical($actual_json, $expected_string);
}
protected function createNodes($number, $type, &$expected) {
$type = $this
->drupalCreateContentType([
'type' => $type,
'name' => $type,
]);
for ($i = 1; $i < $number; $i++) {
$settings = [
'body' => [
[
'value' => $this
->randomMachineName(32),
'format' => filter_default_format(),
],
],
'type' => $type
->id(),
'created' => 123456789,
'title' => $type
->id() . ' ' . $i,
'status' => TRUE,
'promote' => rand(0, 1) == 1,
'sticky' => rand(0, 1) == 1,
'uid' => Drupal::currentUser()
->id(),
];
$node = Node::create($settings);
$status = $node
->save();
$this
->assertEqual($status, SAVED_NEW, new FormattableMarkup('Created node %title.', [
'%title' => $node
->label(),
]));
$result = [
'value' => $type
->id() . ' ' . $i,
'fields' => [
'title' => $type
->id() . ' ' . $i,
'created' => 'by ' . $this->adminUser
->getUsername() . ' | Thu, 11/29/1973 - 21:33',
],
'link' => $node
->toUrl('canonical', [
'absolute' => TRUE,
])
->toString(),
];
if ($i == 1) {
$result += [
'group' => [
'group_id' => strtolower(Html::cleanCssIdentifier($type
->label())),
'group_name' => $type
->label() . "s",
],
];
}
$expected[] = $result;
}
return $expected;
}
protected function setUp($import_test_views = TRUE) {
parent::setUp();
$this->adminUser = $this
->drupalCreateUser([
'access content',
'administer views',
'administer search autocomplete',
]);
$this
->drupalLogin($this->adminUser);
$this->nodeStorage = $this->container
->get('entity.manager')
->getStorage('node');
}
}