View source
<?php
namespace Drupal\entity_legal\Tests;
class EntityLegalDocumentAcceptanceTest extends EntityLegalTestBase {
public function testSubmissionForm() {
$document = $this
->createDocument(TRUE, TRUE);
$version = $this
->createDocumentVersion($document, TRUE);
$acceptance_user = $this
->drupalCreateUser([
$document
->getPermissionView(),
$document
->getPermissionExistingUser(),
]);
$this
->drupalLogin($acceptance_user);
$document_url = $document
->toUrl();
$this
->drupalGet($document_url);
$this
->assertFieldByName('agree', NULL, 'Agree checkbox found');
$this
->assertFieldByName('op', 'Submit', 'Submit button found');
$this
->assertText($document
->label(), 'Document title found');
$this
->assertText($version
->get('entity_legal_document_text')[0]->value, 'Version document text found');
$this
->drupalPostForm($document_url, [
'agree' => 1,
], 'Submit');
$this
->drupalGet($document_url);
$this
->assertNoFieldByName('op', 'Submit', 'Submit button not found');
$document = $this
->getUncachedEntity(ENTITY_LEGAL_DOCUMENT_ENTITY_NAME, $document
->id());
$this
->assertTrue($document
->userHasAgreed($acceptance_user), 'User has accepted');
$new_version = $this
->createDocumentVersion($document, TRUE);
$document = $this
->getUncachedEntity(ENTITY_LEGAL_DOCUMENT_ENTITY_NAME, $document
->id());
$this
->assertFalse($document
->userHasAgreed($acceptance_user), 'User has not accepted new version');
$this
->drupalGet($document_url);
$this
->assertFieldByName('agree', NULL, 'Agree checkbox found');
$this
->assertFieldByName('op', 'Submit', 'Submit button found');
$this
->assertText($document
->label(), 'Document title found');
$this
->assertText($new_version
->get('entity_legal_document_text')[0]->value, 'New version document text found');
$this
->drupalPostForm($document_url, [
'agree' => 1,
], 'Submit');
$this
->drupalGet($document_url);
$this
->assertNoFieldByName('op', 'Submit', 'Submit button not found');
$document = $this
->getUncachedEntity(ENTITY_LEGAL_DOCUMENT_ENTITY_NAME, $document
->id());
$this
->assertTrue($document
->userHasAgreed($acceptance_user), 'User has accepted new version');
$acceptance_storage = \Drupal::entityTypeManager()
->getStorage(ENTITY_LEGAL_DOCUMENT_ACCEPTANCE_ENTITY_NAME);
$version
->delete();
$aid_count = $acceptance_storage
->getQuery()
->condition('uid', $acceptance_user
->id())
->count()
->execute();
$this
->assertEqual(1, $aid_count);
$acceptance_user
->delete();
$aid_count = $acceptance_storage
->getQuery()
->condition('uid', $acceptance_user
->id())
->count()
->execute();
$this
->assertEqual(0, $aid_count);
}
}