Drupal 8.0.0 beta 12. More info: https://www.drupal.org/node/2514176
This commit is contained in:
commit
9921556621
13277 changed files with 1459781 additions and 0 deletions
|
@ -0,0 +1,6 @@
|
|||
name: 'Quick Edit test'
|
||||
type: module
|
||||
description: 'Support module for the Quick Edit module tests.'
|
||||
core: 8.x
|
||||
package: Testing
|
||||
version: VERSION
|
52
core/modules/quickedit/tests/modules/quickedit_test.module
Normal file
52
core/modules/quickedit/tests/modules/quickedit_test.module
Normal file
|
@ -0,0 +1,52 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Helper module for the Quick Edit tests.
|
||||
*/
|
||||
|
||||
use Drupal\Core\Entity\EntityInterface;
|
||||
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
|
||||
use Drupal\Core\Language\LanguageInterface;
|
||||
|
||||
/**
|
||||
* Implements hook_entity_view_alter().
|
||||
*/
|
||||
function quickedit_test_entity_view_alter(&$build, EntityInterface $entity, EntityViewDisplayInterface $display) {
|
||||
if ($entity->getEntityTypeId() == 'node' && $entity->bundle() == 'article') {
|
||||
$build['pseudo'] = array(
|
||||
'#theme' => 'field',
|
||||
'#title' => 'My pseudo field',
|
||||
'#field_name' => 'quickedit_test_pseudo_field',
|
||||
'#label_display' => 'Label',
|
||||
'#entity_type' => $entity->getEntityTypeId(),
|
||||
'#bundle' => $entity->bundle(),
|
||||
'#language' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
|
||||
'#field_type' => 'pseudo',
|
||||
'#view_mode' => 'default',
|
||||
'#object' => $entity,
|
||||
'#access' => TRUE,
|
||||
'#items' => array(
|
||||
0 => array(
|
||||
'value' => 'pseudo field',
|
||||
),
|
||||
),
|
||||
0 => array(
|
||||
'#markup' => 'pseudo field',
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_quickedit_render_field().
|
||||
*/
|
||||
function quickedit_test_quickedit_render_field(EntityInterface $entity, $field_name, $view_mode_id, $langcode) {
|
||||
$entity = \Drupal::entityManager()->getTranslationFromContext($entity, $langcode);
|
||||
return array(
|
||||
'#prefix' => '<div class="quickedit-test-wrapper">',
|
||||
'field' => $entity->get($field_name)->view($view_mode_id),
|
||||
'#suffix' => '</div>',
|
||||
);
|
||||
}
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\quickedit_test\MockEditEntityFieldAccessCheck.
|
||||
*/
|
||||
|
||||
namespace Drupal\quickedit_test;
|
||||
|
||||
use Drupal\Core\Entity\EntityInterface;
|
||||
use Drupal\quickedit\Access\EditEntityFieldAccessCheckInterface;
|
||||
|
||||
/**
|
||||
* Access check for editing entity fields.
|
||||
*/
|
||||
class MockEditEntityFieldAccessCheck implements EditEntityFieldAccessCheckInterface {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function accessEditEntityField(EntityInterface $entity, $field_name) {
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,63 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\quickedit_test\Plugin\InPlaceEditor\WysiwygEditor.
|
||||
*/
|
||||
|
||||
namespace Drupal\quickedit_test\Plugin\InPlaceEditor;
|
||||
|
||||
use Drupal\Core\Field\FieldItemListInterface;
|
||||
use Drupal\quickedit\Plugin\InPlaceEditorBase;
|
||||
|
||||
/**
|
||||
* Defines the 'wysiwyg' in-place editor.
|
||||
*
|
||||
* @InPlaceEditor(
|
||||
* id = "wysiwyg",
|
||||
* alternativeTo = {"plain_text"}
|
||||
* )
|
||||
*/
|
||||
class WysiwygEditor extends InPlaceEditorBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function isCompatible(FieldItemListInterface $items) {
|
||||
$field_definition = $items->getFieldDefinition();
|
||||
|
||||
// This editor is incompatible with multivalued fields.
|
||||
if ($field_definition->getFieldStorageDefinition()->getCardinality() != 1) {
|
||||
return FALSE;
|
||||
}
|
||||
// This editor is compatible with formatted ("rich") text fields; but only
|
||||
// if there is a currently active text format and that text format is the
|
||||
// 'full_html' text format.
|
||||
elseif (in_array($field_definition->getType(), array('text', 'text_long', 'text_with_summary'), TRUE)) {
|
||||
if ($items[0]->format === 'full_html') {
|
||||
return TRUE;
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getMetadata(FieldItemListInterface $items) {
|
||||
$metadata['format'] = $items[0]->format;
|
||||
return $metadata;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getAttachments() {
|
||||
return array(
|
||||
'library' => array(
|
||||
'quickedit_test/not-existing-wysiwyg',
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,158 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Tests\quickedit\Unit\Access\EditEntityFieldAccessCheckTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\Tests\quickedit\Unit\Access;
|
||||
|
||||
use Drupal\Core\Access\AccessResult;
|
||||
use Drupal\quickedit\Access\EditEntityFieldAccessCheck;
|
||||
use Drupal\Tests\UnitTestCase;
|
||||
use Drupal\field\FieldStorageConfigInterface;
|
||||
use Drupal\Core\Entity\EntityInterface;
|
||||
use Drupal\Core\Language\LanguageInterface;
|
||||
|
||||
/**
|
||||
* @coversDefaultClass \Drupal\quickedit\Access\EditEntityFieldAccessCheck
|
||||
* @group Access
|
||||
* @group quickedit
|
||||
*/
|
||||
class EditEntityFieldAccessCheckTest extends UnitTestCase {
|
||||
|
||||
/**
|
||||
* The tested access checker.
|
||||
*
|
||||
* @var \Drupal\quickedit\Access\EditEntityFieldAccessCheck
|
||||
*/
|
||||
protected $editAccessCheck;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
$this->editAccessCheck = new EditEntityFieldAccessCheck();
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides test data for testAccess().
|
||||
*
|
||||
* @see \Drupal\Tests\edit\Unit\quickedit\Access\EditEntityFieldAccessCheckTest::testAccess()
|
||||
*/
|
||||
public function providerTestAccess() {
|
||||
$editable_entity = $this->createMockEntity();
|
||||
$editable_entity->expects($this->any())
|
||||
->method('access')
|
||||
->will($this->returnValue(AccessResult::allowed()->cachePerPermissions()));
|
||||
|
||||
$non_editable_entity = $this->createMockEntity();
|
||||
$non_editable_entity->expects($this->any())
|
||||
->method('access')
|
||||
->will($this->returnValue(AccessResult::neutral()->cachePerPermissions()));
|
||||
|
||||
$field_storage_with_access = $this->getMockBuilder('Drupal\field\Entity\FieldStorageConfig')
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
$field_storage_with_access->expects($this->any())
|
||||
->method('access')
|
||||
->will($this->returnValue(AccessResult::allowed()));
|
||||
$field_storage_without_access = $this->getMockBuilder('Drupal\field\Entity\FieldStorageConfig')
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
$field_storage_without_access->expects($this->any())
|
||||
->method('access')
|
||||
->will($this->returnValue(AccessResult::neutral()));
|
||||
|
||||
$data = array();
|
||||
$data[] = array($editable_entity, $field_storage_with_access, AccessResult::allowed()->cachePerPermissions());
|
||||
$data[] = array($non_editable_entity, $field_storage_with_access, AccessResult::neutral()->cachePerPermissions());
|
||||
$data[] = array($editable_entity, $field_storage_without_access, AccessResult::neutral()->cachePerPermissions());
|
||||
$data[] = array($non_editable_entity, $field_storage_without_access, AccessResult::neutral()->cachePerPermissions());
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the method for checking access to routes.
|
||||
*
|
||||
* @param \Drupal\Core\Entity\EntityInterface $entity
|
||||
* A mocked entity.
|
||||
* @param \Drupal\field\FieldStorageConfigInterface $field_storage
|
||||
* A mocked field storage.
|
||||
* @param bool|null $expected_result
|
||||
* The expected result of the access call.
|
||||
*
|
||||
* @dataProvider providerTestAccess
|
||||
*/
|
||||
public function testAccess(EntityInterface $entity, FieldStorageConfigInterface $field_storage = NULL, $expected_result) {
|
||||
$field_name = 'valid';
|
||||
$entity_with_field = clone $entity;
|
||||
$entity_with_field->expects($this->any())
|
||||
->method('get')
|
||||
->with($field_name)
|
||||
->will($this->returnValue($field_storage));
|
||||
$entity_with_field->expects($this->once())
|
||||
->method('hasTranslation')
|
||||
->with(LanguageInterface::LANGCODE_NOT_SPECIFIED)
|
||||
->will($this->returnValue(TRUE));
|
||||
|
||||
$account = $this->getMock('Drupal\Core\Session\AccountInterface');
|
||||
$access = $this->editAccessCheck->access($entity_with_field, $field_name, LanguageInterface::LANGCODE_NOT_SPECIFIED, $account);
|
||||
$this->assertEquals($expected_result, $access);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests checking access to routes that result in AccessResult::isForbidden().
|
||||
*
|
||||
* @dataProvider providerTestAccessForbidden
|
||||
*/
|
||||
public function testAccessForbidden($field_name, $langcode) {
|
||||
$account = $this->getMock('Drupal\Core\Session\AccountInterface');
|
||||
$entity = $this->createMockEntity();
|
||||
$this->assertEquals(AccessResult::forbidden(), $this->editAccessCheck->access($entity, $field_name, $langcode, $account));
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides test data for testAccessForbidden.
|
||||
*/
|
||||
public function providerTestAccessForbidden() {
|
||||
$data = array();
|
||||
// Tests the access method without a field_name.
|
||||
$data[] = array(NULL, LanguageInterface::LANGCODE_NOT_SPECIFIED);
|
||||
// Tests the access method with a non-existent field.
|
||||
$data[] = array('not_valid', LanguageInterface::LANGCODE_NOT_SPECIFIED);
|
||||
// Tests the access method without a langcode.
|
||||
$data[] = array('valid', NULL);
|
||||
// Tests the access method with an invalid langcode.
|
||||
$data[] = array('valid', 'xx-lolspeak');
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a mock entity.
|
||||
*
|
||||
* @return \Drupal\Core\Entity\EntityInterface|\PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
protected function createMockEntity() {
|
||||
$entity = $this->getMockBuilder('Drupal\entity_test\Entity\EntityTest')
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
|
||||
$entity->expects($this->any())
|
||||
->method('hasTranslation')
|
||||
->will($this->returnValueMap(array(
|
||||
array(LanguageInterface::LANGCODE_NOT_SPECIFIED, TRUE),
|
||||
array('xx-lolspeak', FALSE),
|
||||
)));
|
||||
$entity->expects($this->any())
|
||||
->method('hasField')
|
||||
->will($this->returnValueMap(array(
|
||||
array('valid', TRUE),
|
||||
array('not_valid', FALSE),
|
||||
)));
|
||||
|
||||
return $entity;
|
||||
}
|
||||
|
||||
}
|
Reference in a new issue