3 namespace Drupal\Tests\embed\Functional;
5 use Drupal\Component\Serialization\Json;
6 use Drupal\filter\Entity\FilterFormat;
7 use Drupal\node\Entity\Node;
8 use Drupal\node\Entity\NodeType;
9 use Drupal\Tests\BrowserTestBase;
12 * Tests Embed's preview functionality.
16 class EmbedPreviewTest extends BrowserTestBase {
21 public static $modules = ['embed_test', 'filter'];
24 * Tests that out-of-band assets are included with previews.
26 public function testPreview() {
32 $filter_format = FilterFormat::create([
36 $filter_format->filters()->addInstanceId('embed_test_node', [
37 'id' => 'embed_test_node',
38 'provider' => 'embed_test',
42 $filter_format->save();
44 $node = Node::create([
50 $account = $this->drupalCreateUser(['use text format foo']);
51 $this->drupalLogin($account);
53 $response = $this->drupalGet('/embed/preview/foo', [
55 'value' => 'node:' . $node->id(),
59 $this->assertSession()->statusCodeEquals(200);
61 // Assert the presence of commands to add out-of-band assets to the page, as
62 // done by embed_test_node_view_alter().
63 $commands = Json::decode($response);
64 // There should be more than one command.
65 $this->assertGreaterThan(1, count($commands));
66 // There should be a command to add jQuery to the page.
67 $this->assertMatch($commands, function (array $command) {
68 return $command['command'] == 'insert' && $command['method'] == 'append' && $command['selector'] == 'body' && strpos($command['data'], 'jquery.min.js') > 0;
73 * Asserts that at least one item in an array matches a predicate.
77 * @param callable $predicate
78 * The predicate against which to test the items.
80 protected function assertMatch(array $items, callable $predicate) {
81 $items = array_filter($items, $predicate);
82 $this->assertNotEmpty($items);