Yaffs site version 1.1
[yaffs-website] / web / modules / contrib / embed / tests / src / Functional / EmbedPreviewTest.php
1 <?php
2
3 namespace Drupal\Tests\embed\Functional;
4
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;
10
11 /**
12  * Tests Embed's preview functionality.
13  *
14  * @group embed
15  */
16 class EmbedPreviewTest extends BrowserTestBase {
17
18   /**
19    * {@inheritdoc}
20    */
21   public static $modules = ['embed_test', 'filter'];
22
23   /**
24    * Tests that out-of-band assets are included with previews.
25    */
26   public function testPreview() {
27     NodeType::create([
28       'type' => 'baz',
29       'label' => 'Bazzz',
30     ])->save();
31
32     $filter_format = FilterFormat::create([
33       'format' => 'foo',
34       'name' => 'Foo',
35     ]);
36     $filter_format->filters()->addInstanceId('embed_test_node', [
37       'id' => 'embed_test_node',
38       'provider' => 'embed_test',
39       'status' => TRUE,
40       'settings' => [],
41     ]);
42     $filter_format->save();
43
44     $node = Node::create([
45       'title' => 'Foobaz',
46       'type' => 'baz',
47     ]);
48     $node->save();
49
50     $account = $this->drupalCreateUser(['use text format foo']);
51     $this->drupalLogin($account);
52
53     $response = $this->drupalGet('/embed/preview/foo', [
54       'query' => [
55         'value' => 'node:' . $node->id(),
56       ],
57     ]);
58
59     $this->assertSession()->statusCodeEquals(200);
60
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;
69     });
70   }
71
72   /**
73    * Asserts that at least one item in an array matches a predicate.
74    *
75    * @param array $items
76    *   The items to test.
77    * @param callable $predicate
78    *   The predicate against which to test the items.
79    */
80   protected function assertMatch(array $items, callable $predicate) {
81     $items = array_filter($items, $predicate);
82     $this->assertNotEmpty($items);
83   }
84
85 }