getRevisionUser example

// Ensure that the node revision has been created.     $revised_node = $this->drupalGetNodeByTitle($edit['title[0][value]'], TRUE);
    $this->assertNotSame($node->getRevisionId()$revised_node->getRevisionId(), 'A new revision has been created.');
    // Ensure that the node author is preserved when it was not changed in the     // edit form.     $this->assertSame($node->getOwnerId()$revised_node->getOwnerId(), 'The node author has been preserved.');
    // Ensure that the revision authors are different since the revisions were     // made by different users.     $node_storage = \Drupal::service('entity_type.manager')->getStorage('node');
    $first_node_version = $node_storage->loadRevision($node->getRevisionId());
    $second_node_version = $node_storage->loadRevision($revised_node->getRevisionId());
    $this->assertNotSame($first_node_version->getRevisionUser()->id()$second_node_version->getRevisionUser()->id(), 'Each revision has a distinct user.');

    // Check if the node revision checkbox is rendered on node edit form.     $this->drupalGet('node/' . $node->id() . '/edit');
    $this->assertSession()->fieldExists('edit-revision', NULL);

    // Check that details form element opens when there are errors on child     // elements.     $this->drupalGet('node/' . $node->id() . '/edit');
    $edit = [];
    // This invalid date will trigger an error.     $edit['created[0][value][date]'] = $this->randomMachineName(8);
    
    $entity->setNewRevision(TRUE);
    $random_timestamp = rand(1e8, 2e8);
    $this->createRevision($entity$user$random_timestamp, 'This is my log message');

    $revision_id = $entity->getRevisionId();
    $revision_ids[] = $revision_id;

    $storage = \Drupal::entityTypeManager()->getStorage('entity_test_mul_revlog');
    $entity = $storage->loadRevision($revision_id);
    $this->assertEquals($random_timestamp$entity->getRevisionCreationTime());
    $this->assertEquals($user->id()$entity->getRevisionUserId());
    $this->assertEquals($user->id()$entity->getRevisionUser()->id());
    $this->assertEquals('This is my log message', $entity->getRevisionLogMessage());

    // Create the third revision.     $random_timestamp = rand(1e8, 2e8);
    $this->createRevision($entity$user$random_timestamp, 'This is my log message');
    $this->assertItemsTableCount(3, $definition);
    $revision_ids[] = $entity->getRevisionId();

    // Create another 3 revisions.     foreach (range(1, 3) as $count) {
      $timestamp = rand(1e8, 2e8);
      
/** * Test description with entity implementing revision log, with empty values. * * @covers ::getRevisionDescription */
  public function testDescriptionRevLogNullValues(): void {
    $entity = EntityTestWithRevisionLog::create(['type' => 'entity_test_revlog']);
    $entity->setName('view all revisions')->save();

    // Check entity values are still null after saving; they did not receive     // values from currentUser or some other global context.     $this->assertNull($entity->getRevisionUser());
    $this->assertNull($entity->getRevisionUserId());
    $this->assertNull($entity->getRevisionLogMessage());

    $this->drupalGet($entity->toUrl('version-history'));
    $this->assertSession()->elementTextContains('css', 'table tbody tr:nth-child(1)', 'by Anonymous (not verified)');
  }

  /** * Test description with entity, without revision log, no label access. * * @covers ::getRevisionDescription */

  protected function getRevisionDescription(RevisionableInterface $revision): array {
    $context = [];
    if ($revision instanceof RevisionLogInterface) {
      // Use revision link to link to revisions that are not active.       ['type' => $dateFormatType, 'format' => $dateFormatFormat] = $this->getRevisionDescriptionDateFormat($revision);
      $linkText = $this->dateFormatter->format($revision->getRevisionCreationTime()$dateFormatType$dateFormatFormat);

      // @todo Simplify this when https://www.drupal.org/node/2334319 lands.       $username = [
        '#theme' => 'username',
        '#account' => $revision->getRevisionUser(),
      ];
      $context['username'] = $this->renderer->render($username);
    }
    else {
      $linkText = $revision->access('view label') ? $revision->label() : $this->t('- Restricted access -');
    }

    $url = $revision->hasLinkTemplate('revision') ? $revision->toUrl('revision') : NULL;
    $context['revision'] = $url && $url->access()
      ? Link::fromTextAndUrl($linkText$url)->toString()
      : (string) $linkText;
    
$default_revision = $node->getRevisionId();
    $current_revision_displayed = FALSE;

    foreach ($this->getRevisionIds($node$node_storage) as $vid) {
      /** @var \Drupal\node\NodeInterface $revision */
      $revision = $node_storage->loadRevision($vid);
      // Only show revisions that are affected by the language that is being       // displayed.       if ($revision->hasTranslation($langcode) && $revision->getTranslation($langcode)->isRevisionTranslationAffected()) {
        $username = [
          '#theme' => 'username',
          '#account' => $revision->getRevisionUser(),
        ];

        // Use revision link to link to revisions that are not active.         $date = $this->dateFormatter->format($revision->revision_timestamp->value, 'short');

        // We treat also the latest translation-affecting revision as current         // revision, if it was the default revision, as its values for the         // current language will be the same of the current default revision in         // this case.         $is_current_revision = $vid == $default_revision || (!$current_revision_displayed && $revision->wasDefaultRevision());
        if (!$is_current_revision) {
          
    $this->drupalGet("user/" . $account->id() . "/cancel/confirm/$timestamp/" . user_pass_rehash($account$timestamp));
    $user_storage->resetCache([$account->id()]);
    $this->assertNull($user_storage->load($account->id()), 'User is not found in the database.');

    // Confirm that user's content has been attributed to anonymous user.     $anonymous_user = User::getAnonymousUser();
    $node_storage->resetCache([$node->id()]);
    $test_node = $node_storage->load($node->id());
    $this->assertEquals(0, $test_node->getOwnerId(), 'Node of the user has been attributed to anonymous user.');
    $this->assertTrue($test_node->isPublished());
    $test_node = $node_storage->loadRevision($revision);
    $this->assertEquals(0, $test_node->getRevisionUser()->id(), 'Node revision of the user has been attributed to anonymous user.');
    $this->assertTrue($test_node->isPublished());
    $node_storage->resetCache([$revision_node->id()]);
    $test_node = $node_storage->load($revision_node->id());
    $this->assertNotEquals(0, $test_node->getOwnerId(), "Current revision of the user's node was not attributed to anonymous user.");
    $this->assertTrue($test_node->isPublished());

    $storage = \Drupal::entityTypeManager()->getStorage('comment');
    $storage->resetCache([$comment->id()]);
    $test_comment = $storage->load($comment->id());
    $this->assertEquals(0, $test_comment->getOwnerId(), 'Comment of the user has been attributed to anonymous user.');
    $this->assertTrue($test_comment->isPublished());
    
$this->assertSame('filtered_html', $node->body->format);
    $this->assertSame('story', $node->getType(), 'Node has the correct bundle.');
    $this->assertSame('Test title rev 3', $node->getTitle(), 'Node has the correct title.');
    $this->assertSame('1390095702', $node->getCreatedTime(), 'Node has the correct created time.');
    $this->assertFalse($node->isSticky());
    $this->assertSame('1', $node->getOwnerId());
    $this->assertSame('1420861423', $node->getRevisionCreationTime());

    /** @var \Drupal\node\NodeInterface $node_revision */
    $node_revision = \Drupal::entityTypeManager()->getStorage('node')->loadRevision(2001);
    $this->assertSame('Test title rev 3', $node_revision->getTitle());
    $this->assertSame('2', $node_revision->getRevisionUser()->id(), 'Node revision has the correct user');
    $this->assertSame('1', $node_revision->id(), 'Node 1 loaded.');
    $this->assertSame('2001', $node_revision->getRevisionId(), 'Node 1 revision 2001 loaded.');
    // This is empty on the first revision.     $this->assertSame('modified rev 3', $node_revision->revision_log->value);
    $this->assertSame('This is a shared text field', $node->field_test->value);
    $this->assertSame('filtered_html', $node->field_test->format);
    $this->assertSame('10', $node->field_test_two->value);
    $this->assertSame('20', $node->field_test_two[1]->value);

    $this->assertSame('42.42', $node->field_test_three->value, 'Single field second value is correct.');
    $this->assertSame('3412', $node->field_test_integer_selectlist[0]->value);
    
$logs = $this->revisionLogs;

    foreach ($blocks as $delta => $revision_id) {
      // Confirm the correct revision text appears.       /** @var \Drupal\block_content\BlockContentInterface $loaded */
      $loaded = $this->container->get('entity_type.manager')
        ->getStorage('block_content')
        ->loadRevision($revision_id);
      // Verify revision log is the same.       $this->assertEquals($logs[$delta]$loaded->getRevisionLogMessage()new FormattableMarkup('Correct log message found for revision @revision', ['@revision' => $loaded->getRevisionId()]));
      if ($delta > 0) {
        $this->assertInstanceOf(UserInterface::class$loaded->getRevisionUser());
        $this->assertIsNumeric($loaded->getRevisionUserId());
        $this->assertIsNumeric($loaded->getRevisionCreationTime());
      }
    }

    // Confirm that this is the default revision.     $this->assertTrue($loaded->isDefaultRevision(), 'Third block revision is the default one.');

    // Make a new revision and set it to not be default.     // This will create a new revision that is not "front facing".     // Save this as a non-default revision.
$this->assertNull($account, 'User is deleted after JSON:API DELETE operation with user.settings.cancel_method: ' . $cancel_method);

    $node_storage = $this->container->get('entity_type.manager')->getStorage('node');
    $node_storage->resetCache([$node->id()]);
    $test_node = $node_storage->load($node->id());
    $this->assertNotNull($test_node, 'Node of the user is not deleted.');
    $this->assertTrue($test_node->isPublished(), 'Node of the user is still published.');
    $this->assertEquals(0, $test_node->getOwnerId(), 'Node of the user has been attributed to anonymous user.');
    $test_node = $node_storage->loadRevision($node->getRevisionId());
    $this->assertTrue($test_node->isPublished(), 'Node revision of the user is still published.');
    $this->assertEquals(0, $test_node->getRevisionUser()->id(), 'Node revision of the user has been attributed to anonymous user.');
  }

  /** * Tests if JSON:API respects user.settings.cancel_method: user_cancel_delete. */
  public function testDeleteRespectsUserCancelDelete() {
    $cancel_method = 'user_cancel_delete';
    $this->config('jsonapi.settings')->set('read_only', FALSE)->save(TRUE);
    $this->config('user.settings')->set('cancel_method', $cancel_method)->save(TRUE);

    $account = $this->createAnotherEntity($cancel_method);
    

  public function testNodeRevision() {
    $node = \Drupal::entityTypeManager()->getStorage('node')->loadRevision(2001);
    /** @var \Drupal\node\NodeInterface $node */
    $this->assertSame('1', $node->id());
    $this->assertSame('2001', $node->getRevisionId());
    $this->assertSame('und', $node->langcode->value);
    $this->assertSame('Test title rev 3', $node->getTitle());
    $this->assertSame('body test rev 3', $node->body->value);
    $this->assertSame('teaser test rev 3', $node->body->summary);
    $this->assertSame('2', $node->getRevisionUser()->id());
    $this->assertSame('modified rev 3', $node->revision_log->value);
    $this->assertSame('1420861423', $node->getRevisionCreationTime());

    $this->assertRevision(1, 'und', 'Test title', NULL, 1390095702);
    $this->assertRevision(3, 'und', 'Test title rev 3', NULL, 1420718386);
    $this->assertRevision(4, 'und', 'Test page title rev 4', NULL, 1390095701);
    $this->assertRevision(5, 'und', 'Test title rev 2', 'modified rev 2', 1390095703);
    $this->assertRevision(6, 'und', 'Node 4', NULL, 1390095701);
    $this->assertRevision(7, 'und', 'Node 5', NULL, 1390095701);
    $this->assertRevision(8, 'und', 'Node 6', NULL, 1390095701);
    $this->assertRevision(9, 'und', 'Node 7', NULL, 1390095701);
    
foreach (array_keys($this->getTranslationLanguages()) as $langcode) {
      $translation = $this->getTranslation($langcode);

      // If no owner has been set explicitly, make the anonymous user the owner.       if (!$translation->getOwner()) {
        $translation->setOwnerId(0);
      }
    }

    // If no revision author has been set explicitly, make the node owner the     // revision author.     if (!$this->getRevisionUser()) {
      $this->setRevisionUserId($this->getOwnerId());
    }
  }

  /** * {@inheritdoc} */
  public function preSaveRevision(EntityStorageInterface $storage, \stdClass $record) {
    parent::preSaveRevision($storage$record);

    if (!$this->isNewRevision() && isset($this->original) && (!isset($record->revision_log) || $record->revision_log === '')) {
      

  protected function assertRevision(int $id, string $title, int $uid, ?string $log, int $timestamp): void {
    $revision = \Drupal::entityTypeManager()->getStorage('node')->loadRevision($id);
    $this->assertInstanceOf(NodeInterface::class$revision);
    $this->assertEquals($title$revision->getTitle());
    $this->assertEquals($uid$revision->getRevisionUser()->id());
    $this->assertEquals($log$revision->revision_log->value);
    $this->assertEquals($timestamp$revision->getRevisionCreationTime());
  }

  /** * Tests node migration from Drupal 7 to 8. */
  public function testNode() {
    // Confirm there are only classic node migration map tables. This shows     // that only the classic migration ran.     $results = $this->nodeMigrateMapTableCount('7');
    
Home | Imprint | This part of the site doesn't use cookies.