enforceIsNew example

$test_nid = $max_nid + mt_rand(1000, 1000000);
    $title = $this->randomMachineName(8);
    $node = [
      'title' => $title,
      'body' => [['value' => $this->randomMachineName(32)]],
      'uid' => $this->webUser->id(),
      'type' => 'article',
      'nid' => $test_nid,
    ];
    /** @var \Drupal\node\NodeInterface $node */
    $node = Node::create($node);
    $node->enforceIsNew();

    $this->assertEquals($this->webUser->id()$node->getOwnerId());

    $node->save();
    // Test the import.     $node_by_nid = Node::load($test_nid);
    $this->assertNotEmpty($node_by_nid, 'Node load by node ID.');

    $node_by_title = $this->drupalGetNodeByTitle($title);
    $this->assertNotEmpty($node_by_title, 'Node load by node title.');
  }

  
case 5:
          case 6:
            $node->book['bid'] = $root;
            $node->book['pid'] = $root + 2;
            $node->book['depth'] = 3;
            break;
        }
        // This is necessary to make the table of contents consistent across         // test runs.         $node->book['weight'] = $i;
        $node->nid->value = $root + $i;
        $node->enforceIsNew();
        $node->save();
      }
    }
    \Drupal::currentUser()->setAccount($this->createUser(['access content']));
  }

  /** * Tests various book manager methods return correct translations. * * @dataProvider langcodesProvider */
  
/** * {@inheritdoc} */
  public function label() {
    return $this->storage->label();
  }

  /** * {@inheritdoc} */
  public function enforceIsNew($value = TRUE) {
    return $this->storage->enforceIsNew($value);
  }

  /** * {@inheritdoc} */
  public function toArray() {
    return $this->storage->toArray();
  }

  /** * {@inheritdoc} */
    for ($i = 1; $i <= 3; $i++) {
      $file = File::create([
        'fid' => $i,
        'uid' => 1,
        'filename' => 'druplicon.txt',
        'uri' => "public://druplicon-$i.txt",
        'filemime' => 'text/plain',
        'created' => 1,
        'changed' => 1,
      ]);
      $file->setPermanent();
      $file->enforceIsNew();
      file_put_contents($file->getFileUri(), 'hello world');

      // Save it, inserting a new record.       $file->save();
      $id_mappings['d6_file'][] = [[$i][$i]];
    }
    $this->prepareMigrations($id_mappings);

    $this->migrateContent(['translations']);
    // Since we are only testing a subset of the file migration, do not check     // that the full file migration has been run.
else {
      // Attempt to ensure we always have a bundle.       if ($bundle = $this->getBundle($row)) {
        $row->setDestinationProperty($this->getKey('bundle')$bundle);
      }

      // Stubs might need some required fields filled in.       if ($row->isStub()) {
        $this->processStubRow($row);
      }
      $entity = $this->storage->create($row->getDestination());
      $entity->enforceIsNew();
    }
    return $entity;
  }

  /** * Gets the entity ID of the row. * * @param \Drupal\migrate\Row $row * The row of data. * * @return string * The entity ID for the row that we are importing. */

  public function testCreateWithPredefinedUuid() {
    $this->cacheTagsInvalidator->invalidateTags(Argument::cetera())->shouldNotBeCalled();

    $entity = $this->getMockEntity();
    $entity->set('id', 'foo');
    $entity->set('langcode', 'hu');
    $entity->set('uuid', 'baz');
    $entity->setOriginalId('foo');
    $entity->enforceIsNew();

    $this->moduleHandler->invokeAll('test_entity_type_create', [$entity])
      ->shouldBeCalled();
    $this->moduleHandler->invokeAll('entity_create', [$entity, 'test_entity_type'])
      ->shouldBeCalled();

    $this->uuidService->generate()->shouldNotBeCalled();

    $entity = $this->entityStorage->create(['id' => 'foo', 'uuid' => 'baz']);
    $this->assertInstanceOf(EntityInterface::class$entity);
    $this->assertSame('foo', $entity->id());
    

  public function testViewsHandlerRelationshipUserFileData(): void {
    $file = File::create([
      'fid' => 2,
      'uid' => 2,
      'filename' => 'image-test.jpg',
      'uri' => "public://image-test.jpg",
      'filemime' => 'image/jpeg',
      'created' => 1,
      'changed' => 1,
    ]);
    $file->enforceIsNew()->setPermanent();
    file_put_contents($file->getFileUri()file_get_contents('core/tests/fixtures/files/image-1.png'));
    $file->save();

    $account = $this->createUser();
    $account->user_file->target_id = 2;
    $account->save();

    $view = Views::getView('test_file_user_file_data');
    // Tests \Drupal\taxonomy\Plugin\views\relationship\NodeTermData::calculateDependencies().     $expected = [
      'module' => [
        
$this->installConfig(['node']);
    $this->installSchema('node', ['node_access']);
    $this->installSchema('system', ['sequences']);

    // Create a new user which needs to have UID 1, because that is expected by     // the assertions from     // \Drupal\migrate_drupal\Tests\d6\MigrateNodeRevisionTest.     User::create([
      'uid' => 1,
      'name' => $this->randomMachineName(),
      'status' => 1,
    ])->enforceIsNew()->save();

    $this->migrateUsers(FALSE);
    $this->migrateFields();
    $this->executeMigration('d6_node_settings');
  }

}

  public function create(array $values = []) {
    $entity_class = $this->getEntityClass();
    $entity_class::preCreate($this$values);

    // Assign a new UUID if there is none yet.     if ($this->uuidKey && $this->uuidService && !isset($values[$this->uuidKey])) {
      $values[$this->uuidKey] = $this->uuidService->generate();
    }

    $entity = $this->doCreate($values);
    $entity->enforceIsNew();

    $entity->postCreate($this);

    // Modules might need to add or change the data initially held by the new     // entity object, for instance to fill-in default values.     $this->invokeHook('create', $entity);

    return $entity;
  }

  /** * Performs storage-specific creation of entities. * * @param array $values * An array of values to set, keyed by property name. * * @return \Drupal\Core\Entity\EntityInterface */
$this->assertFalse(Database::getConnection()->schema()->indexExists($table, 'value_format'), 'No index named value_format exists in $table');
    }

    // Add data so the table cannot be dropped.     $entity = $this->container->get('entity_type.manager')
      ->getStorage($entity_type)
      ->create([
        'id' => 1,
        'revision_id' => 1,
      ]);
    $entity->$field_name->value = 'field data';
    $entity->enforceIsNew();
    $entity->save();

    // Add an index.     $field_storage->setIndexes(['value' => [['value', 255]]]);
    $field_storage->save();
    foreach ($tables as $table) {
      $this->assertTrue(Database::getConnection()->schema()->indexExists($table, "{$field_name}_value"), "Index on value created in $table");
    }

    // Add a different index, removing the existing custom one.     $field_storage->setIndexes(['value_format' => [['value', 127]['format', 127]]]);
    
'entity_type' => 'entity_test',
        'bundle' => 'entity_test',
      ])->save();
    }

    $entity = $this->entityStorage->create([
      'id' => 1,
      'user_id' => 1,
      'field_test_1' => 1,
      'field_test_2' => 2,
    ]);
    $entity->enforceIsNew();
    $entity->save();

    $entity = $this->entityStorage->create([
      'id' => 2,
      'user_id' => 2,
      'field_test_1' => 1,
      'field_test_2' => 7,
    ]);
    $entity->enforceIsNew();
    $entity->save();
    $entity = $this->entityStorage->create([
      
FieldConfig::create($this->field)->save();
    $display_repository->getFormDisplay($this->field['entity_type']$this->field['bundle'])
      ->setComponent($this->field['field_name'])
      ->save();

    // Create two entities.     $entity_type = 'entity_test';
    $storage = $this->container->get('entity_type.manager')
      ->getStorage($entity_type);

    $entity_1 = $storage->create(['id' => 1]);
    $entity_1->enforceIsNew();
    $entity_1->field_single->value = 1;
    $entity_1->field_unlimited->value = 2;
    $entity_1->save();

    $entity_2 = $storage->create(['id' => 2]);
    $entity_2->enforceIsNew();
    $entity_2->field_single->value = 10;
    $entity_2->field_unlimited->value = 11;
    $entity_2->save();

    // Display the 'combined form'.
$entity_class = $this->getEntityClass($bundle);
    // @todo Decide what to do if preCreate() tries to change the bundle.     // @see https://www.drupal.org/project/drupal/issues/3230792     $entity_class::preCreate($this$values);

    // Assign a new UUID if there is none yet.     if ($this->uuidKey && $this->uuidService && !isset($values[$this->uuidKey])) {
      $values[$this->uuidKey] = $this->uuidService->generate();
    }

    $entity = $this->doCreate($values);
    $entity->enforceIsNew();

    $entity->postCreate($this);

    // Modules might need to add or change the data initially held by the new     // entity object, for instance to fill-in default values.     $this->invokeHook('create', $entity);

    return $entity;
  }

  /** * {@inheritdoc} */
    $array['level1']['level2a'] = 9;
    $array['level1a']['level2'] = 9;
    // The tests match array.level1.level2.     $array['level1']['level2'] = 1;
    $entity = ConfigQueryTest::create([
      'label' => 'entity_1',
      'id' => '1',
      'number' => 31,
      'array' => $array,
    ]);
    $this->entities[] = $entity;
    $entity->enforceIsNew();
    $entity->save();

    $array['level1']['level2'] = 2;
    $entity = ConfigQueryTest::create([
      'label' => 'entity_2',
      'id' => '2',
      'number' => 41,
      'array' => $array,
    ]);
    $this->entities[] = $entity;
    $entity->enforceIsNew();
    
// Clear out any hook calls.     file_test_reset();

    $this->installConfig(['system']);
    $this->installEntitySchema('file');
    $this->installEntitySchema('user');
    $this->installSchema('file', ['file_usage']);

    // Make sure that a user with uid 1 exists, self::createFile() relies on     // it.     $user = User::create(['uid' => 1, 'name' => $this->randomMachineName()]);
    $user->enforceIsNew();
    $user->save();
    \Drupal::currentUser()->setAccount($user);
  }

  /** * Asserts that the specified file hooks were called only once. * * @param array $expected * Array with string containing with the hook name, e.g. 'load', 'save', * 'insert', etc. */
  
Home | Imprint | This part of the site doesn't use cookies.