View source
<?php
namespace Drupal\yamlform\Tests;
use Drupal\simpletest\WebTestBase;
use Drupal\yamlform\Entity\YamlFormSubmission;
class YamlFormSubmissionStorageTest extends WebTestBase {
use YamlFormTestTrait;
protected static $modules = [
'system',
'user',
'yamlform',
];
public function setUp() {
parent::setUp();
}
public function tearDown() {
$this
->purgeSubmissions();
parent::tearDown();
}
public function testSubmissionStorage() {
$storage = \Drupal::entityTypeManager()
->getStorage('yamlform_submission');
$yamlform = $this
->createYamlForm();
$user1 = $this
->drupalCreateUser();
$this
->drupalLogin($user1);
$user1_submissions = [
YamlFormSubmission::load($this
->postSubmission($yamlform)),
YamlFormSubmission::load($this
->postSubmission($yamlform)),
YamlFormSubmission::load($this
->postSubmission($yamlform)),
];
$user2 = $this
->drupalCreateUser();
$this
->drupalLogin($user2);
$user2_submissions = [
YamlFormSubmission::load($this
->postSubmission($yamlform)),
YamlFormSubmission::load($this
->postSubmission($yamlform)),
YamlFormSubmission::load($this
->postSubmission($yamlform)),
];
$admin_user = $this
->drupalCreateUser([
'administer yamlform',
]);
$this
->assertEqual($storage
->getTotal($yamlform), 6);
$this
->assertEqual($storage
->getTotal($yamlform, NULL, $user1), 3);
$this
->assertEqual($storage
->getTotal($yamlform, NULL, $user2), 3);
$this
->assertEqual($storage
->getNextSubmission($user1_submissions[0], NULL, $user1)
->id(), $user1_submissions[1]
->id(), "User 1 can navigate forward to user 1's next submission");
$this
->assertNull($storage
->getNextSubmission($user1_submissions[2], NULL, $user1), "User 1 can't navigate forward to user 2's next submission");
$this
->drupalLogin($admin_user);
$this
->assertEqual($storage
->getNextSubmission($user1_submissions[2], NULL)
->id(), $user2_submissions[0]
->id(), "Admin user can navigate between user submissions");
$this
->drupalLogout();
$this
->assertEqual($storage
->getPreviousSubmission($user1_submissions[1], NULL, $user1)
->id(), $user1_submissions[0]
->id(), "User 1 can navigate backward to user 1's previous submission");
$this
->assertNull($storage
->getPreviousSubmission($user2_submissions[0], NULL, $user2), "User 2 can't navigate backward to user 1's previous submission");
$this
->drupalLogin($admin_user);
$this
->assertEqual($storage
->getPreviousSubmission($user2_submissions[0], NULL)
->id(), $user1_submissions[2]
->id(), "Admin user can navigate between user submissions");
$this
->drupalLogout();
$yamlform
->setSetting('draft', TRUE)
->save();
$this
->drupalLogin($user1);
$this
->postSubmission($yamlform, [], t('Save Draft'));
$this
->drupalLogin($user2);
$this
->postSubmission($yamlform, [], t('Save Draft'));
$this
->assertEqual($storage
->getTotal($yamlform), 6);
$this
->assertEqual($storage
->getTotal($yamlform, NULL, $user1), 3);
$this
->assertEqual($storage
->getTotal($yamlform, NULL, $user2), 3);
$this
->drupalLogin($user1);
$this
->postSubmission($yamlform);
$this
->drupalLogin($user2);
$this
->postSubmission($yamlform);
$this
->assertEqual($storage
->getTotal($yamlform), 8);
$this
->assertEqual($storage
->getTotal($yamlform, NULL, $user1), 4);
$this
->assertEqual($storage
->getTotal($yamlform, NULL, $user2), 4);
}
}