This repository has been archived by the owner on Jan 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #2741373 by phenaproxima, slashrsm, Dave Reid: Embedded preview…
…s do not support attachments.
- Loading branch information
Showing
5 changed files
with
142 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ core: 8.x | |
package: Testing | ||
dependencies: | ||
- embed | ||
- node |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |