View source
<?php
namespace Drupal\Tests\acquia_contenthub\Unit\Form;
use Drupal\acquia_contenthub\Form\EntityForm;
use Drupal\Tests\UnitTestCase;
require_once __DIR__ . '/../Polyfill/Drupal.php';
class EntityFormTest extends UnitTestCase {
private $currentUser;
private $contentHubEntitiesTracking;
private $entityForm;
public function setUp() : void {
parent::setUp();
$this->currentUser = $this
->createMock('Drupal\\Core\\Session\\AccountInterface');
$this->contentHubEntitiesTracking = $this
->getMockBuilder('Drupal\\acquia_contenthub\\ContentHubEntitiesTracking')
->disableOriginalConstructor()
->getMock();
$this->entityForm = new EntityForm($this->currentUser, $this->contentHubEntitiesTracking);
}
public function testGetFormNodeNoId() {
$node = $this
->createMock('\\Drupal\\node\\NodeInterface');
$node
->expects($this
->once())
->method('id')
->willReturn(NULL);
$this->contentHubEntitiesTracking
->expects($this
->never())
->method('loadImportedByDrupalEntity');
$form = $this->entityForm
->getForm($node);
$this
->assertNull($form);
}
public function testGetFormNodeNotImported() {
$node = $this
->createMock('\\Drupal\\node\\NodeInterface');
$node
->expects($this
->once())
->method('id')
->willReturn(12);
$node
->expects($this
->once())
->method('getEntityTypeId')
->willReturn('node');
$this->contentHubEntitiesTracking
->expects($this
->once())
->method('loadImportedByDrupalEntity')
->with('node', 12)
->willReturn(NULL);
$this->contentHubEntitiesTracking
->expects($this
->never())
->method('hasLocalChange');
$form = $this->entityForm
->getForm($node);
$this
->assertNull($form);
}
public function testGetFormEntityFormNodeIsImported($has_local_change, $is_auto_update, $has_local_change_text) {
$node = $this
->createMock('\\Drupal\\node\\NodeInterface');
$node
->expects($this
->once())
->method('id')
->willReturn(12);
$node
->expects($this
->once())
->method('getEntityTypeId')
->willReturn('node');
$this->contentHubEntitiesTracking
->expects($this
->once())
->method('loadImportedByDrupalEntity')
->with('node', 12)
->willReturn($this->contentHubEntitiesTracking);
$this->currentUser
->expects($this
->once())
->method('hasPermission')
->with('administer acquia content hub')
->willReturn(TRUE);
$this->contentHubEntitiesTracking
->expects($this
->once())
->method('hasLocalChange')
->willReturn($has_local_change);
$this->contentHubEntitiesTracking
->expects($this
->once())
->method('isAutoUpdate')
->willReturn($is_auto_update);
$this->entityForm
->setStringTranslation($this
->getStringTranslationStub());
$form = $this->entityForm
->getForm($node);
$this
->assertEquals($has_local_change_text, $form['auto_update_label']['#markup']);
$this
->assertTrue($has_local_change === isset($form['auto_update_local_changes_label']));
}
public function providerTestGetFormEntityFormNodeIsImported() {
$yes_local_change = TRUE;
$no_local_change = FALSE;
$yes_auto_update = TRUE;
$no_auto_update = FALSE;
$yes_local_change_text = 'This syndicated content has been modified locally, therefore it is no longer automatically synchronized to its original content.';
$no_local_change_text = 'This is a syndicated content. What happens if its original content is updated?';
$data = [];
$data['yes local change, no auto update'] = [
$yes_local_change,
$no_auto_update,
$yes_local_change_text,
];
$data['no local change, yes auto update'] = [
$no_local_change,
$yes_auto_update,
$no_local_change_text,
];
return $data;
}
public function testAttachSubmitHandlerNotAttachable() {
$form_actions = [];
$form_submit_handler = 'my_handler';
$this->entityForm
->attachSubmitHandler($form_actions, $form_submit_handler);
$this
->assertEquals([], $form_actions);
}
public function testAttachSubmitHandlerAttachable() {
$form_actions = [
'preview' => [
'#type' => 'submit',
],
'ignored_action' => [
'#type' => 'ignored_type',
'#submit' => [
'existing_submit_handler',
],
],
'attached_action' => [
'#type' => 'submit',
'#submit' => [
'existing_submit_handler',
],
],
];
$expected = [
'preview' => [
'#type' => 'submit',
],
'ignored_action' => [
'#type' => 'ignored_type',
'#submit' => [
'existing_submit_handler',
],
],
'attached_action' => [
'#type' => 'submit',
'#submit' => [
'my_handler',
'existing_submit_handler',
],
],
];
$form_submit_handler = 'my_handler';
$this->entityForm
->attachSubmitHandler($form_actions, $form_submit_handler);
$this
->assertEquals($expected, $form_actions);
}
public function testSaveSettingsEmptyValue() {
$form_state = $this
->createMock('Drupal\\Core\\Form\\FormState');
$form_state
->expects($this
->once())
->method('isValueEmpty')
->willReturn(TRUE);
$form_state
->expects($this
->never())
->method('getFormObject');
$this->entityForm
->saveSettings($form_state);
}
public function testSaveSettingsNodeIsNotImported() {
$form_state = $this
->createMock('Drupal\\Core\\Form\\FormState');
$form_state
->expects($this
->once())
->method('isValueEmpty')
->willReturn(FALSE);
$node = $this
->createMock('\\Drupal\\node\\NodeInterface');
$node
->expects($this
->once())
->method('getEntityTypeId')
->willReturn('node');
$node
->expects($this
->once())
->method('id')
->willReturn(12);
$form_object = $this
->createMock('Drupal\\Core\\Entity\\EntityFormInterface');
$form_object
->expects($this
->once())
->method('getEntity')
->willReturn($node);
$form_state
->expects($this
->once())
->method('getFormObject')
->willReturn($form_object);
$this->contentHubEntitiesTracking
->expects($this
->once())
->method('loadImportedByDrupalEntity')
->with('node', 12)
->willReturn(NULL);
$this->contentHubEntitiesTracking
->expects($this
->never())
->method('hasLocalChange');
$this->entityForm
->saveSettings($form_state);
}
public function testSaveSettingsSetAndSave($old_auto_update_flag, $new_auto_update_flag, $method_name, $method_parameter) {
$node = $this
->createMock('\\Drupal\\node\\NodeInterface');
$node
->expects($this
->once())
->method('getEntityTypeId')
->willReturn('node');
$node
->expects($this
->once())
->method('id')
->willReturn(12);
$form_object = $this
->createMock('Drupal\\Core\\Entity\\EntityFormInterface');
$form_object
->expects($this
->once())
->method('getEntity')
->willReturn($node);
$form_state = $this
->createMock('Drupal\\Core\\Form\\FormState');
$form_state
->expects($this
->once())
->method('isValueEmpty')
->willReturn(FALSE);
$form_state
->expects($this
->once())
->method('getFormObject')
->willReturn($form_object);
$this->contentHubEntitiesTracking
->expects($this
->once())
->method('loadImportedByDrupalEntity')
->with('node', 12)
->willReturn($this->contentHubEntitiesTracking);
$this->contentHubEntitiesTracking
->expects($this
->once())
->method('isAutoUpdate')
->willReturn($old_auto_update_flag);
$form_state
->expects($this
->once())
->method('getValue')
->with('acquia_contenthub')
->willReturn([
'auto_update' => $new_auto_update_flag,
]);
$method = $this->contentHubEntitiesTracking
->expects($this
->once())
->method($method_name);
if ($method_parameter) {
$method
->with($method_parameter);
}
$this->contentHubEntitiesTracking
->expects($this
->once())
->method('save');
$this->entityForm
->saveSettings($form_state);
}
public function providerTestSaveSettingsSetAndSave() {
$get_auto_update_true = TRUE;
$get_auto_update_false = FALSE;
$set_auto_update_true = 1;
$set_auto_update_false = 0;
$expect_call_set_pending_sync = 'setPendingSync';
$expect_call_set_auto_update = 'setAutoUpdate';
$expect_parameter_true = TRUE;
$expect_parameter_false = FALSE;
$expect_no_parameter = NULL;
$data = [];
$data['no local change, set auto update false'] = [
$get_auto_update_false,
$set_auto_update_false,
$expect_call_set_auto_update,
$expect_parameter_false,
];
$data['no local change, set auto update true'] = [
$get_auto_update_false,
$set_auto_update_true,
$expect_call_set_pending_sync,
$expect_no_parameter,
];
$data['yes local change, set auto update false'] = [
$get_auto_update_true,
$set_auto_update_false,
$expect_call_set_auto_update,
$expect_parameter_false,
];
$data['yes local change, set auto update true'] = [
$get_auto_update_true,
$set_auto_update_true,
$expect_call_set_auto_update,
$expect_parameter_true,
];
return $data;
}
}