class EntityFormTest in Acquia Content Hub 8
PHPUnit test for the EntityForm class.
@coversDefaultClass Drupal\acquia_contenthub\Form\EntityForm
@group acquia_contenthub
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\acquia_contenthub\Unit\Form\EntityFormTest
Expanded class hierarchy of EntityFormTest
File
- tests/
src/ Unit/ Form/ EntityFormTest.php, line 17
Namespace
Drupal\Tests\acquia_contenthub\Unit\FormView source
class EntityFormTest extends UnitTestCase {
/**
* The current user.
*
* @var \Drupal\Core\Session\AccountInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $currentUser;
/**
* The Content Hub Entities Tracking Service.
*
* @var \Drupal\acquia_contenthub\ContentHubEntitiesTracking|\PHPUnit_Framework_MockObject_MockObject
*/
private $contentHubEntitiesTracking;
/**
* The EntityForm that is being tested.
*
* @var \Drupal\acquia_contenthub\Form\EntityForm
*/
private $entityForm;
/**
* {@inheritdoc}
*/
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);
}
/**
* Tests the getForm() method, node has no id.
*
* @covers ::getForm
*/
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);
}
/**
* Tests the getForm() method, node is not imported.
*
* @covers ::getForm
*/
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);
}
/**
* Tests the getForm() method, node is imported.
*
* @param bool $has_local_change
* Has local change flag.
* @param bool $is_auto_update
* Is auto update flag.
* @param string $has_local_change_text
* "Has local change" text.
*
* @covers ::getForm
*
* @dataProvider providerTestGetFormEntityFormNodeIsImported
*/
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']));
}
/**
* Data provider for testGetFormEntityFormNodeIsImported().
*
* @return array
* Data.
*/
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;
}
/**
* Tests the attachSubmitHandler() method, not attachable.
*
* @covers ::attachSubmitHandler
*/
public function testAttachSubmitHandlerNotAttachable() {
$form_actions = [];
$form_submit_handler = 'my_handler';
$this->entityForm
->attachSubmitHandler($form_actions, $form_submit_handler);
$this
->assertEquals([], $form_actions);
}
/**
* Tests the attachSubmitHandler() method, attachable.
*
* @covers ::attachSubmitHandler
*/
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);
}
/**
* Tests the saveSettings() method, has empty form value.
*
* @covers ::saveSettings
*/
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);
}
/**
* Tests the saveSettings() method, node is not imported.
*
* @covers ::saveSettings
*/
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);
}
/**
* Tests the saveSettings() method, actually set and save.
*
* @param bool $old_auto_update_flag
* Has local change flag.
* @param int $new_auto_update_flag
* Set to auto update flag.
* @param string $method_name
* The method name to be called before save().
* @param string $method_parameter
* The method parameter to be called before save().
*
* @covers ::saveSettings
*
* @dataProvider providerTestSaveSettingsSetAndSave
*/
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);
}
/**
* Data provider for testSaveSettingsSetAndSave().
*
* @return array
* Data.
*/
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 = [];
// Case 1.
// disabled -> disabled: setAutoUpdate(FALSE)
$data['no local change, set auto update false'] = [
$get_auto_update_false,
$set_auto_update_false,
$expect_call_set_auto_update,
$expect_parameter_false,
];
// Case 2.
// disabled -> enabled: setPendingSync()
// local change -> enabled: setPendingSync()
// pending sync -> enabled: setPendingSync()
// Note: when user indicates "enable", don't set "enable", instead, set to
// the only state "pending sync" that will lead to "enabled".
$data['no local change, set auto update true'] = [
$get_auto_update_false,
$set_auto_update_true,
$expect_call_set_pending_sync,
$expect_no_parameter,
];
// Case 3.
// enabled -> disabled: setAutoUpdate(FALSE)
$data['yes local change, set auto update false'] = [
$get_auto_update_true,
$set_auto_update_false,
$expect_call_set_auto_update,
$expect_parameter_false,
];
// Case 4.
// enabled -> disabled: setAutoUpdate(TRUE)
$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;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
EntityFormTest:: |
private | property | The Content Hub Entities Tracking Service. | |
EntityFormTest:: |
private | property | The current user. | |
EntityFormTest:: |
private | property | The EntityForm that is being tested. | |
EntityFormTest:: |
public | function | Data provider for testGetFormEntityFormNodeIsImported(). | |
EntityFormTest:: |
public | function | Data provider for testSaveSettingsSetAndSave(). | |
EntityFormTest:: |
public | function |
Overrides UnitTestCase:: |
|
EntityFormTest:: |
public | function | Tests the attachSubmitHandler() method, attachable. | |
EntityFormTest:: |
public | function | Tests the attachSubmitHandler() method, not attachable. | |
EntityFormTest:: |
public | function | Tests the getForm() method, node is imported. | |
EntityFormTest:: |
public | function | Tests the getForm() method, node has no id. | |
EntityFormTest:: |
public | function | Tests the getForm() method, node is not imported. | |
EntityFormTest:: |
public | function | Tests the saveSettings() method, has empty form value. | |
EntityFormTest:: |
public | function | Tests the saveSettings() method, node is not imported. | |
EntityFormTest:: |
public | function | Tests the saveSettings() method, actually set and save. | |
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |