loadUnchanged example

    $this->drupalGet($style_path . '/effects/' . $uuids['image_crop'] . '/delete');
    $this->submitForm([], 'Delete');
    // Confirm that the form submission was successful.     $this->assertSession()->statusCodeEquals(200);
    $image_crop_effect = $style->getEffect($uuids['image_crop']);
    $this->assertSession()->statusMessageContains("The image effect {$image_crop_effect->label()} has been deleted.", 'status');
    // Confirm that there is no longer a link to the effect.     $this->assertSession()->linkByHrefNotExists($style_path . '/effects/' . $uuids['image_crop'] . '/delete');
    // Refresh the image style information and verify that the effect was     // actually deleted.     $entity_type_manager = $this->container->get('entity_type.manager');
    $style = $entity_type_manager->getStorage('image_style')->loadUnchanged($style->id());
    $this->assertFalse($style->getEffects()->has($uuids['image_crop'])new FormattableMarkup(
      'Effect with ID %uuid no longer found on image style %style',
      [
        '%uuid' => $uuids['image_crop'],
        '%style' => $style->label(),
      ]));

    // Additional test on Rotate effect, for transparent background.     $edit = [
      'data[degrees]' => 5,
      'data[random]' => 0,
      
public static function create(ContainerInterface $container) {
    return new static(
      $container->get('entity_type.manager')
    );
  }

  /** * {@inheritdoc} */
  public function validate($entity, Constraint $constraint) {
    if ($entity && !$entity->isNew() && !$entity->isDefaultRevision()) {
      $original = $this->entityTypeManager->getStorage($entity->getEntityTypeId())->loadUnchanged($entity->id());

      // Ensure that empty items do not affect the comparison checks below.       // @todo Remove this filtering when       // https://www.drupal.org/project/drupal/issues/3039031 is fixed.       $entity->parent->filterEmptyItems();
      if (($entity->parent->isEmpty() !== $original->parent->isEmpty()) || !$entity->parent->equals($original->parent)) {
        $this->context->buildViolation($constraint->message)
          ->atPath('menu_parent')
          ->addViolation();
      }
      if (!$entity->weight->equals($original->weight)) {
        
// Check that the default revision status was stored correctly.     $storage = $this->entityTypeManager->getStorage($entity_type_id);
    foreach ([TRUE, FALSE, TRUE, FALSE] as $index => $expected) {
      /** @var \Drupal\entity_test_revlog\Entity\EntityTestMulWithRevisionLog $revision */
      $revision = $storage->loadRevision($index + 1);
      $this->assertEquals($expected$revision->wasDefaultRevision());
    }

    // Check that the default revision is flagged correctly.     /** @var \Drupal\entity_test_revlog\Entity\EntityTestMulWithRevisionLog $entity */
    $entity = $storage->loadUnchanged($entity->id());
    $this->assertTrue($entity->wasDefaultRevision());

    // Check that the "revision_default" flag cannot be changed once set.     /** @var \Drupal\entity_test_revlog\Entity\EntityTestMulWithRevisionLog $entity2 */
    $entity2 = EntityTestMulWithRevisionLog::create([
      'type' => $entity_type_id,
    ]);
    $entity2->save();
    $this->assertTrue($entity2->wasDefaultRevision());
    $entity2->isDefaultRevision(FALSE);
    $entity2->save();
    
// If we edit the item, we should get a new revision.     $this->drupalGet('/media/1/edit');
    $assert->checkboxChecked('Create new revision');
    $page = $this->getSession()->getPage();
    $page->fillField('Name', 'Foo');
    $page->pressButton('Save');
    $this->assertRevisionCount($media, 2);

    // Confirm the correct revision title appears on "view revisions" page.     $media = $this->container->get('entity_type.manager')
      ->getStorage('media')
      ->loadUnchanged(1);
    $this->drupalGet("media/" . $media->id() . "/revisions/" . $media->getRevisionId() . "/view");
    $assert->pageTextContains('Foo');
  }

  /** * Tests creating revisions of an Image media item. */
  public function testImageMediaRevision() {
    $assert = $this->assertSession();

    $this->createMediaType('image', ['id' => 'image', 'new_revision' => TRUE]);

    
$edit['title[0][value]'] = str_pad($number, 2, '0', STR_PAD_LEFT) . ' - test node ' . $this->randomMachineName(10);
    $edit['body[0][value]'] = 'test body ' . $this->randomMachineName(32) . ' ' . $this->randomMachineName(32);
    $edit['book[bid]'] = $book_nid;

    if ($parent !== NULL) {
      $this->drupalGet('node/add/book');
      $this->submitForm($edit, 'Change book (update list of parents)');

      $edit['book[pid]'] = $parent;
      $this->submitForm($edit, 'Save');
      // Make sure the parent was flagged as having children.       $parent_node = \Drupal::entityTypeManager()->getStorage('node')->loadUnchanged($parent);
      $this->assertNotEmpty($parent_node->book['has_children'], 'Parent node is marked as having children');
    }
    else {
      $this->drupalGet('node/add/book');
      $this->submitForm($edit, 'Save');
    }

    // Check to make sure the book node was created.     $node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
    $this->assertNotNull(($node === FALSE ? NULL : $node), 'Book node found in database.');
    $number++;

    
$edit['title[0][value]'] = $this->randomMachineName();
    $this->drupalGet('node/add/' . $type_name);
    $this->submitForm($edit, 'Save');
    $new_node = $this->drupalGetNodeByTitle($edit['title[0][value]']);

    // Can't use submitForm() to set hidden fields.     $this->drupalGet('node/' . $new_node->id() . '/edit');
    $this->getSession()->getPage()->find('css', 'input[name="' . $field_name . '[0][fids]"]')->setValue($node_file->id());
    $this->getSession()->getPage()->pressButton('Save');
    $this->assertSession()->addressEquals('node/' . $new_node->id());
    // Make sure the submitted hidden file field is empty.     $new_node = \Drupal::entityTypeManager()->getStorage('node')->loadUnchanged($new_node->id());
    $this->assertTrue($new_node->get($field_name)->isEmpty());
    // Attempt to reuse the existing file when creating a new node, and confirm     // that access is still denied.     $edit = [];
    $edit['title[0][value]'] = $this->randomMachineName();
    // Can't use submitForm() to set hidden fields.     $this->drupalGet('node/add/' . $type_name);
    $this->getSession()->getPage()->find('css', 'input[name="title[0][value]"]')->setValue($edit['title[0][value]']);
    $this->getSession()->getPage()->find('css', 'input[name="' . $field_name . '[0][fids]"]')->setValue($node_file->id());
    $this->getSession()->getPage()->pressButton('Save');
    $new_node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
    
/** * Saves the passed entity and reloads it, enabling compatibility mode. * * @param \Drupal\Core\Entity\EntityInterface $entity * The entity to be saved. * * @return \Drupal\Core\Entity\EntityInterface * The saved entity. */
  protected function saveEntity(EntityInterface $entity) {
    $entity->save();
    $entity = \Drupal::entityTypeManager()->getStorage('entity_test_mul')->loadUnchanged($entity->id());
    return $entity;
  }

}
return TRUE;
    }

    // $this->original only exists during save. See     // \Drupal\Core\Entity\EntityStorageBase::save(). If it exists we re-use it     // here for performance reasons.     /** @var \Drupal\Core\Entity\ContentEntityBase $original */
    $original = $this->original ? $this->original : NULL;

    if (!$original) {
      $id = $this->getOriginalId() !== NULL ? $this->getOriginalId() : $this->id();
      $original = $this->entityTypeManager()->getStorage($this->getEntityTypeId())->loadUnchanged($id);
    }

    // If the current translation has just been added, we have a change.     $translated = count($this->translations) > 1;
    if ($translated && !$original->hasTranslation($this->activeLangcode)) {
      return TRUE;
    }

    // Compare field item current values with the original ones to determine     // whether we have changes. If a field is not translatable and the entity is     // translated we skip it because, depending on the use case, it would make
$this->config('jsonapi.settings')->set('read_only', FALSE)->save(TRUE);

    // Try changing user 1's email.     $user1 = $original_normalization;
    $user1['data']['attributes']['mail'] = 'another_email_address@example.com';
    $user1['data']['attributes']['uid'] = 1;
    $user1['data']['attributes']['name'] = 'another_user_name';
    $user1['data']['attributes']['pass']['existing'] = $this->account->passRaw;
    $request_options[RequestOptions::BODY] = Json::encode($user1);
    $response = $this->request('PATCH', $url$request_options);
    // Ensure the email address has not changed.     $this->assertEquals('admin@example.com', $this->entityStorage->loadUnchanged(1)->getEmail());
    $this->assertResourceErrorResponse(403, 'The current user is not allowed to PATCH the selected field (uid). The entity ID cannot be changed.', $url$response, '/data/attributes/uid');
  }

  /** * Tests GETting privacy-sensitive base fields. */
  public function testGetMailFieldOnlyVisibleToOwner() {
    // Create user B, with the same roles (and hence permissions) as user A.     $user_a = $this->account;
    $pass = \Drupal::service('password_generator')->generate();
    $user_b = User::create([
      
$this->installEntitySchema($entity_type);
      $storage = \Drupal::entityTypeManager()->getStorage($entity_type);

      $entity = $storage->create([
        'name' => 'foo',
        'user_id' => $user->id(),
      ]);

      $entity->save();
      $entity_id = $entity->id();
      $entity_rev_id = $entity->getRevisionId();
      $entity = $storage->loadUnchanged($entity_id);

      $entity->setNewRevision(TRUE);
      $entity->setNewRevision(FALSE);
      $entity->save();
      $entity = $storage->loadUnchanged($entity_id);

      $this->assertEquals($entity_rev_id$entity->getRevisionId(), 'A new entity revision was not created.');
    }
  }

  /** * Tests that revision translations are correctly detected. * * @covers \Drupal\Core\Entity\ContentEntityStorageBase::isAnyStoredRevisionTranslated */

  protected function getOriginalEntity(ContentEntityInterface $entity) {
    if (!isset($entity->original)) {
      $storage = $this->entityTypeManager->getStorage($entity->getEntityTypeId());
      $original = $entity->isDefaultRevision() ? $storage->loadUnchanged($entity->id()) : $storage->loadRevision($entity->getLoadedRevisionId());
    }
    else {
      $original = $entity->original;
    }
    return $original;
  }

  /** * {@inheritdoc} */
  public function synchronizeItems(array &$values, array $unchanged_items$sync_langcode, array $translations, array $properties) {
    
'entity_type' => $entity->getEntityTypeId(),
      'field_name' => 'body',
    ]);
    $field_storage->save();

    FieldConfig::create([
      'field_storage' => $field_storage,
      'bundle' => $entity->bundle(),
    ])->save();

    // Now that the field has been created, there should be no violations.     $this->assertCount(0, $storage->loadUnchanged(1)->validate());
  }

}
// Check that the API doesn't allow us to change the book outline for     // pending revisions.     $child->book['bid'] = $book_2->id();
    $child->setNewRevision(TRUE);
    $child->isDefaultRevision(FALSE);

    $this->assertFalse($book_manager->updateOutline($child), 'A pending revision can not change the book outline.');

    // Check that the API doesn't allow us to change the book parent for     // pending revisions.     $child = \Drupal::entityTypeManager()->getStorage('node')->loadUnchanged($child->id());
    $child->book['pid'] = $book_1_child->id();
    $child->setNewRevision(TRUE);
    $child->isDefaultRevision(FALSE);

    $this->assertFalse($book_manager->updateOutline($child), 'A pending revision can not change the book outline.');

    // Check that the API doesn't allow us to change the book weight for     // pending revisions.     $child = \Drupal::entityTypeManager()->getStorage('node')->loadUnchanged($child->id());
    $child->book['weight'] = 2;
    $child->setNewRevision(TRUE);
    

    );
    $this->drupalGet($add_translation_url);
    $this->assertFlagWidget();
    $edit = [
      'title[0][value]' => 'Test 1.3 FR',
      'moderation_state[0][state]' => 'published',
    ];
    $this->submitForm($edit, 'Save (this translation)');

    // Create an English draft.     $entity = $this->storage->loadUnchanged($id);
    $en_edit_url = $this->getEditUrl($entity);
    $this->drupalGet($en_edit_url);
    $this->assertFlagWidget();
  }

  /** * Checks whether the flag widget is displayed. * * @internal */
  protected function assertFlagWidget(): void {
    
$new_filepath = 'public://' . $new_filename;
    file_put_contents($new_filepath$this->randomMachineName());
    $this->drupalGet("media/1/edit");
    $page->pressButton('Remove');
    $result = $assert_session->waitForField("files[{$source_field_id}_0]");
    $this->assertNotEmpty($result);
    $page->attachFileToField("files[{$source_field_id}_0]", \Drupal::service('file_system')->realpath($new_filepath));
    $result = $assert_session->waitForButton('Remove');
    $this->assertNotEmpty($result);
    $page->pressButton('Save');
    /** @var \Drupal\media\MediaInterface $media */
    $media = \Drupal::entityTypeManager()->getStorage('media')->loadUnchanged(1);
    $this->assertEquals($new_filename$media->getName());
    $assert_session->statusMessageContains("$new_filename has been updated.", 'status');
  }

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