Skip to content
This repository has been archived by the owner on Jan 5, 2018. It is now read-only.

Commit

Permalink
Issue #2741373 by phenaproxima, slashrsm, Dave Reid: Embedded preview…
Browse files Browse the repository at this point in the history
…s do not support attachments.
  • Loading branch information
phenaproxima authored and slashrsm committed Jul 10, 2016
1 parent d7beee3 commit 1352819
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/Controller/EmbedController.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,14 @@ public function preview(Request $request, FilterFormatInterface $filter_format)
throw new NotFoundHttpException();
}

$output = check_markup($text, $filter_format->id());
$build = array(
'#type' => 'processed_text',
'#text' => $text,
'#format' => $filter_format->id(),
);

$response = new AjaxResponse();
$response->addCommand(new EmbedInsertCommand($output));
$response->addCommand(new EmbedInsertCommand($build));
return $response;
}

Expand Down
1 change: 1 addition & 0 deletions tests/embed_test/embed_test.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ core: 8.x
package: Testing
dependencies:
- embed
- node
8 changes: 8 additions & 0 deletions tests/embed_test/embed_test.module
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
<?php

/**
* Implements hook_ENTITY_TYPE_view_alter().
*/
function embed_test_node_view_alter(array &$build) {
// @see \Drupal\Tests\embed\Functional\EmbedPreviewTest::testPreview
$build['#attached']['library'][] = 'core/jquery';
}
41 changes: 41 additions & 0 deletions tests/embed_test/src/Plugin/Filter/EntityEmbedByID.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Drupal\embed_test\Plugin\Filter;

use Drupal\filter\FilterProcessResult;
use Drupal\filter\Plugin\FilterBase;
use Drupal\node\Entity\Node;

/**
* Renders a full node view from an embed code like node:NID.
*
* @Filter(
* id = "embed_test_node",
* title = @Translation("Test Node"),
* description = @Translation("Embeds nodes using node:NID embed codes."),
* type = Drupal\filter\Plugin\FilterInterface::TYPE_MARKUP_LANGUAGE
* )
*/
class EntityEmbedByID extends FilterBase {

/**
* {@inheritdoc}
*/
public function process($text, $langcode) {
$result = new FilterProcessResult($text);

$matches = array();
preg_match_all('/node:([0-9]+)/', $text, $matches);

foreach ($matches[0] as $i => $search) {
$node = Node::load($matches[1][$i]);
$build = \Drupal::entityTypeManager()->getViewBuilder('node')->view($node);
$replace = \Drupal::service('renderer')->render($build);
$text = str_replace($search, $replace, $text);
}

$result->setProcessedText($text);
return $result;
}

}
85 changes: 85 additions & 0 deletions tests/src/Functional/EmbedPreviewTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

namespace Drupal\Tests\embed\Functional;

use Drupal\Component\Serialization\Json;
use Drupal\filter\Entity\FilterFormat;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\Tests\BrowserTestBase;

/**
* Tests Embed's preview functionality.
*
* @group embed
*/
class EmbedPreviewTest extends BrowserTestBase {

/**
* {@inheritdoc}
*/
public static $modules = ['embed_test', 'filter'];

/**
* Tests that out-of-band assets are included with previews.
*/
public function testPreview() {
NodeType::create([
'type' => 'baz',
'label' => 'Bazzz',
])->save();

$filter_format = FilterFormat::create([
'format' => 'foo',
'name' => 'Foo',
]);
$filter_format->filters()->addInstanceId('embed_test_node', [
'id' => 'embed_test_node',
'provider' => 'embed_test',
'status' => TRUE,
'settings' => [],
]);
$filter_format->save();

$node = Node::create([
'title' => 'Foobaz',
'type' => 'baz',
]);
$node->save();

$account = $this->drupalCreateUser(['use text format foo']);
$this->drupalLogin($account);

$response = $this->drupalGet('/embed/preview/foo', [
'query' => [
'value' => 'node:' . $node->id(),
],
]);

$this->assertSession()->statusCodeEquals(200);

// Assert the presence of commands to add out-of-band assets to the page, as
// done by embed_test_node_view_alter().
$commands = Json::decode($response);
// There should be more than one command.
$this->assertGreaterThan(1, count($commands));
// There should be a command to add jQuery to the page.
$this->assertMatch($commands, function (array $command) {
return $command['command'] == 'insert' && $command['method'] == 'append' && $command['selector'] == 'body' && strpos($command['data'], 'jquery.min.js') > 0;
});
}

/**
* Asserts that at least one item in an array matches a predicate.
*
* @param array $items
* The items to test.
* @param callable $predicate
* The predicate against which to test the items.
*/
protected function assertMatch(array $items, callable $predicate) {
$items = array_filter($items, $predicate);
$this->assertNotEmpty($items);
}

}

0 comments on commit 1352819

Please sign in to comment.