You are here

function SupportTicketSaveTest::testTimestamps in Support Ticketing System 8

Verifies accuracy of the "created" and "changed" timestamp functionality.

File

modules/support_ticket/src/Tests/SupportTicketSaveTest.php, line 84
Contains \Drupal\support_ticket\Tests\SupportTicketSaveTest.

Class

SupportTicketSaveTest
Tests $support_ticket->save() for saving tickets.

Namespace

Drupal\support_ticket\Tests

Code

function testTimestamps() {

  // Use the default timestamps.
  $edit = array(
    'uid' => $this->webUser
      ->id(),
    'support_ticket_type' => 'ticket',
    'title' => $this
      ->randomMachineName(8),
  );
  entity_create('support_ticket', $edit)
    ->save();
  $support_ticket = $this
    ->supportTicketGetTicketByTitle($edit['title']);
  $this
    ->assertEqual($support_ticket
    ->getCreatedTime(), REQUEST_TIME, 'Creating a support_ticket sets default "created" timestamp.');
  $this
    ->assertEqual($support_ticket
    ->getChangedTime(), REQUEST_TIME, 'Creating a support_ticket sets default "changed" timestamp.');

  // Store the timestamps.
  $created = $support_ticket
    ->getCreatedTime();
  $support_ticket
    ->save();
  $support_ticket = $this
    ->supportTicketGetTicketByTitle($edit['title'], TRUE);
  $this
    ->assertEqual($support_ticket
    ->getCreatedTime(), $created, 'Updating a support_ticket preserves "created" timestamp.');

  // Programmatically set the timestamps using hook_ENTITY_TYPE_presave().
  $support_ticket->title = 'testing_support_ticket_presave';
  $support_ticket
    ->save();
  $support_ticket = $this
    ->supportTicketGetTicketByTitle('testing_support_ticket_presave', TRUE);
  $this
    ->assertEqual($support_ticket
    ->getCreatedTime(), 280299600, 'Saving a support_ticket uses "created" timestamp set in presave hook.');
  $this
    ->assertEqual($support_ticket
    ->getChangedTime(), 979534800, 'Saving a support_ticket uses "changed" timestamp set in presave hook.');

  // Programmatically set the timestamps on the support_ticket.
  $edit = array(
    'uid' => $this->webUser
      ->id(),
    'support_ticket_type' => 'ticket',
    'title' => $this
      ->randomMachineName(8),
    'created' => 280299600,
    // Sun, 19 Nov 1978 05:00:00 GMT
    'changed' => 979534800,
  );
  entity_create('support_ticket', $edit)
    ->save();
  $support_ticket = $this
    ->supportTicketGetTicketByTitle($edit['title']);
  $this
    ->assertEqual($support_ticket
    ->getCreatedTime(), 280299600, 'Creating a support_ticket programmatically uses programmatically set "created" timestamp.');
  $this
    ->assertEqual($support_ticket
    ->getChangedTime(), 979534800, 'Creating a support_ticket programmatically uses programmatically set "changed" timestamp.');

  // Update the timestamps.
  $support_ticket
    ->setCreatedTime(979534800);
  $support_ticket->changed = 280299600;
  $support_ticket
    ->save();
  $support_ticket = $this
    ->supportTicketGetTicketByTitle($edit['title'], TRUE);
  $this
    ->assertEqual($support_ticket
    ->getCreatedTime(), 979534800, 'Updating a support_ticket uses user-set "created" timestamp.');

  // Allowing setting changed timestamps is required, see
  // Drupal\ticket_translation\ContentTranslationMetadataWrapper::setChangedTime($timestamp)
  // for example.
  $this
    ->assertEqual($support_ticket
    ->getChangedTime(), 280299600, 'Updating a support_ticket uses user-set "changed" timestamp.');
}