setNewRevision example


  protected function createNodeRevision(NodeInterface $node) {
    // Create revision with a random title and body and update variables.     $node->title = $this->randomMachineName();
    $node->body = [
      'value' => $this->randomMachineName(32),
      'format' => filter_default_format(),
    ];
    $node->setNewRevision();
    // Ensure the revision author is a different user.     $node->setRevisionUserId($this->revisionUser->id());
    $node->save();

    return $node;
  }

  /** * Checks node revision operations. */
  public function testRevisions() {
    
// Create one node of which an additional revision author will be the     // author.     $node2 = Node::create([
      'type' => 'default',
      'title' => $this->randomMachineName(),
    ]);
    $node2->setRevisionUserId($no_author->id());
    $node2->save();
    $expected_result[] = ['nid' => $node2->id()];

    // Force to add a new revision.     $node2->setNewRevision(TRUE);
    $node2->setRevisionUserId($author->id());
    $node2->save();

    // Create one node on which the author has neither authorship of revisions     // or the main node.     $node3 = Node::create([
      'type' => 'default',
      'title' => $this->randomMachineName(),
    ]);
    $node3->setOwner($no_author);
    $node3->save();

    
'label' => 'Article',
    ]);
    $node_type->save();
    $node = Node::create([
      'title' => 'Test node',
      'type' => 'article',
      'revision_timestamp' => 1000,
    ]);
    $node->save();

    $node->setRevisionCreationTime(1200);
    $node->setNewRevision(TRUE);
    $node->save();

    $node->setRevisionCreationTime(1400);
    $node->setNewRevision(TRUE);
    $node->save();

    $view = Views::getView('test_node_revision_timestamp');
    $this->executeView($view);

    $this->assertIdenticalResultset($view[
      ['vid' => 3, 'revision_timestamp' => 1400],
      [
ConfigurableLanguage::createFromLangcode('fr')->save();

    /** @var \Drupal\node\NodeInterface $node */
    $node = Node::create([
      'type' => $this->createContentType()->id(),
      'title' => 'Default 1',
    ]);
    $node->addTranslation('fr', [
      'title' => 'French 1',
    ]);
    $node->save();
    $node->setNewRevision();
    $node->setTitle('Default 2');
    $node->getTranslation('fr')->setTitle('French 2');
    $node->save();

    $migration = [
      'source' => [
        'plugin' => 'embedded_data',
        'data_rows' => [
          [
            'nid' => $node->id(),
            'vid' => $node->getRevisionId(),
            
$this->executeView($view);
    $this->assertIdenticalResultset($view[
      [
        'id' => 1,
        'revision_id' => 1,
        'revision_user' => $primary_author->id(),
      ],
    ]static::$columnMap);

    // Test results shows the revision author.     $block_content->setRevisionUser($secondary_author);
    $block_content->setNewRevision();
    $block_content->save();

    $view = Views::getView('test_block_content_revision_user');
    $this->executeView($view);
    $this->assertIdenticalResultset($view[
      [
        'id' => 1,
        'revision_id' => 2,
        'revision_user' => $secondary_author->id(),
      ],
    ]static::$columnMap);

    
/** * Creates a new revision for a given media item. * * @param \Drupal\media\MediaInterface $media * A media object. * * @return \Drupal\media\MediaInterface * A media object with up to date revision information. */
  protected function createMediaRevision(MediaInterface $media) {
    $media->setName($this->randomMachineName());
    $media->setNewRevision();
    $media->save();
    return $media;
  }

  /** * Asserts that an entity has a certain number of revisions. * * @param \Drupal\Core\Entity\EntityInterface $entity * The entity in question. * @param int $expected_revisions * The expected number of revisions. * * @internal */


    $this->assertEquals($entity->getChangedTimeAcrossTranslations()$entity->getChangedTime(), 'Changed time of original language is the same as changed time across all translations.');

    $this->assertTrue(
      $this->getRevisionTranslationAffectedFlag($entity),
      'Changed flag of original language is set for a new entity.'
    );

    $changed_en = $entity->getChangedTime();

    $entity->setNewRevision();
    // Save entity without any changes but create new revision.     $entity->save();
    // A new revision without any changes should not set a new changed time.     $this->assertEquals($changed_en$entity->getChangedTime(), 'Changed time of original language did not change.');

    $this->assertFalse(
      $this->getRevisionTranslationAffectedFlag($entity),
      'Changed flag of original language is not set for new revision without changes.'
    );

    $entity->setNewRevision();
    
/** * Tests draft revisions ID conflicts. */
  public function testDraftRevisionIdConflicts() {
    // Create a published node of type page.     $node = Node::create(['type' => 'page', 'title' => 'foo']);
    $node->moderation_state->value = 'published';
    $node->save();

    // Create a draft revision.     $node->moderation_state->value = 'draft';
    $node->setNewRevision(TRUE);
    $node->save();

    // Insert data in the d6_node_revision:page migration mapping table to     // simulate a previously migrated node revision.     $id_map = $this->getMigration('d6_node_revision:page')->getIdMap();
    $table_name = $id_map->mapTableName();
    $id_map->getDatabase()->insert($table_name)
      ->fields([
        'source_ids_hash' => 1,
        'sourceid1' => 1,
        'destid1' => 1,
      ])
$workspace_1->save();
    $this->workspaceManager->setActiveWorkspace($workspace_1);

    $workspace_1_node_1 = $this->createNode(['status' => FALSE]);
    $workspace_1_node_2 = $this->createNode(['status' => FALSE]);

    // Check that the workspace tracks the initial revisions for both nodes.     $initial_revisions = $workspace_association->getAssociatedInitialRevisions($workspace_1->id(), 'node');
    $this->assertCount(2, $initial_revisions);

    for ($i = 0; $i < 4; $i++) {
      $workspace_1_node_1->setNewRevision(TRUE);
      $workspace_1_node_1->save();

      $workspace_1_node_2->setNewRevision(TRUE);
      $workspace_1_node_2->save();
    }

    // The workspace should now track 2 nodes.     $tracked_entities = $workspace_association->getTrackedEntities($workspace_1->id());
    $this->assertCount(2, $tracked_entities['node']);

    // Since all the revisions were created inside a workspace, including the
// Use an explicit changed time to ensure the expected order in the content     // admin listing. We want these to appear in the table in the same order as     // they appear in the following code, and the 'moderated_content' view has a     // table style configuration with a default sort on the 'changed' field     // descending.     $time = \Drupal::time()->getRequestTime();
    $excluded_nodes['published_page'] = $this->drupalCreateNode(['type' => 'page', 'changed' => $time--, 'moderation_state' => 'published']);
    $excluded_nodes['published_article'] = $this->drupalCreateNode(['type' => 'article', 'changed' => $time--, 'moderation_state' => 'published']);

    $excluded_nodes['unmoderated_type'] = $this->drupalCreateNode(['type' => 'unmoderated_type', 'changed' => $time--]);
    $excluded_nodes['unmoderated_type']->setNewRevision(TRUE);
    $excluded_nodes['unmoderated_type']->isDefaultRevision(FALSE);
    $excluded_nodes['unmoderated_type']->changed->value = $time--;
    $excluded_nodes['unmoderated_type']->save();

    $nodes['published_then_draft_article'] = $this->drupalCreateNode(['type' => 'article', 'changed' => $time--, 'moderation_state' => 'published', 'title' => 'first article - published']);
    $nodes['published_then_draft_article']->setNewRevision(TRUE);
    $nodes['published_then_draft_article']->setTitle('first article - draft');
    $nodes['published_then_draft_article']->moderation_state->value = 'draft';
    $nodes['published_then_draft_article']->changed->value = $time--;
    $nodes['published_then_draft_article']->save();

    

  public function testViewAdd() {
    $this->drupalCreateContentType(['type' => 'article']);
    // Create two nodes with two revision.     $node_storage = \Drupal::entityTypeManager()->getStorage('node');
    /** @var \Drupal\node\NodeInterface $node */
    $node = $node_storage->create(['title' => $this->randomString(), 'type' => 'article', 'changed' => REQUEST_TIME + 40]);
    $node->save();

    $node = $node->createDuplicate();
    $node->setNewRevision();
    $node->changed->value = REQUEST_TIME + 20;
    $node->save();

    $node = $node_storage->create(['title' => $this->randomString(), 'type' => 'article', 'changed' => REQUEST_TIME + 30]);
    $node->save();

    $node = $node->createDuplicate();
    $node->setNewRevision();
    $node->changed->value = REQUEST_TIME + 10;
    $node->save();

    

    $workflow->getTypePlugin()->setConfiguration($configuration);
    $workflow->save();

    // Create a published default revision and one forward draft revision.     $node = Node::create([
      'type' => 'example',
      'title' => 'Test Node',
      'moderation_state' => 'published',
    ]);
    $node->save();
    $node->setNewRevision();
    $node->moderation_state = 'draft';
    $node->save();

    // Create a draft default revision.     $second_node = Node::create([
      'type' => 'example',
      'title' => 'Second Node',
      'moderation_state' => 'draft',
    ]);
    $second_node->save();

    
    $entity->setRevisionTranslationAffected(TRUE);
    // Changed field.     $entity->setChangedTime(time() + 1);

    // Check that the revision metadata fields and the changed field have been     // skipped when comparing same revisions.     $this->assertFalse($entity->hasTranslationChanges(), 'ContentEntityBase::hasTranslationChanges() found no changes when comparing different revisions.');

    // Check that the revision metadata fields and the changed field have been     // skipped when comparing same revisions with enforced new revision to be     // created on save.     $entity->setNewRevision(TRUE);
    $this->assertFalse($entity->hasTranslationChanges(), 'ContentEntityBase::hasTranslationChanges() found no changes when comparing different revisions.');

    // Save the entity in new revision with changes on the revision metadata     // fields.     $entity->save();

    // Check that the revision metadata fields and the changed field have been     // skipped when comparing different revisions.     $entity = $storage->loadRevision($entity_previous_rev_id);
    $this->assertFalse($entity->hasTranslationChanges(), 'ContentEntityBase::hasTranslationChanges() found no changes when comparing different revisions.');
  }

}
/** * Checks that the Revision tab is displayed correctly. */
  public function testDisplayRevisionTab() {
    $this->drupalPlaceBlock('local_tasks_block');

    $this->drupalLogin($this->editor);

    // Set page revision setting 'create new revision'. This will mean new     // revisions are created by default when the node is edited.     $type = NodeType::load('page');
    $type->setNewRevision(TRUE);
    $type->save();

    // Create the node.     $node = $this->drupalCreateNode();

    // Verify the checkbox is checked on the node edit form.     $this->drupalGet('node/' . $node->id() . '/edit');
    $this->assertSession()->checkboxChecked('edit-revision');

    // Uncheck the create new revision checkbox and save the node.     $edit = ['revision' => FALSE];
    
$blocks = [];
    $logs = [];

    // Get original block.     $blocks[] = $block->getRevisionId();
    $logs[] = '';

    // Create three revisions.     $revision_count = 3;
    for ($i = 0; $i < $revision_count$i++) {
      $block->setNewRevision(TRUE);
      $block->setRevisionLogMessage($this->randomMachineName(32));
      $block->setRevisionUser($this->adminUser);
      $block->setRevisionCreationTime(REQUEST_TIME);
      $logs[] = $block->getRevisionLogMessage();
      $block->save();
      $blocks[] = $block->getRevisionId();
    }

    $this->blocks = $blocks;
    $this->revisionLogs = $logs;
  }

  
Home | Imprint | This part of the site doesn't use cookies.