protected function setUp() { parent::setUp(); // Create a test entity to serialize. $this->values = array('name' => $this->randomMachineName(), 'user_id' => \Drupal::currentUser()->id(), 'field_test_text' => array('value' => $this->randomMachineName(), 'format' => 'full_html')); $this->entity = entity_create('entity_test_mulrev', $this->values); $this->entity->save(); $this->serializer = $this->container->get('serializer'); $this->installConfig(array('field')); }
protected function setUp() { parent::setUp(); // User create needs sequence table. $this->installSchema('system', array('sequences')); // Create a test user to use as the entity owner. $this->user = \Drupal::entityManager()->getStorage('user')->create(['name' => 'serialization_test_user', 'mail' => '*****@*****.**', 'pass' => '123456']); $this->user->save(); // Create a test entity to serialize. $this->values = array('name' => $this->randomMachineName(), 'user_id' => $this->user->id(), 'field_test_text' => array('value' => $this->randomMachineName(), 'format' => 'full_html')); $this->entity = entity_create('entity_test_mulrev', $this->values); $this->entity->save(); $this->serializer = $this->container->get('serializer'); $this->installConfig(array('field')); }
/** * Tests required validation. * * @covers ::validate * @covers ::isValidationRequired * @covers ::setValidationRequired * @covers ::save * @covers ::preSave * * @expectedException \LogicException * @expectedExceptionMessage Entity validation was skipped. */ public function testRequiredValidation() { $validator = $this->getMock('\\Symfony\\Component\\Validator\\ValidatorInterface'); /** @var \Symfony\Component\Validator\ConstraintViolationList|\PHPUnit_Framework_MockObject_MockObject $empty_violation_list */ $empty_violation_list = $this->getMockBuilder('\\Symfony\\Component\\Validator\\ConstraintViolationList')->setMethods(NULL)->getMock(); $validator->expects($this->at(0))->method('validate')->with($this->entity->getTypedData())->will($this->returnValue($empty_violation_list)); $this->typedDataManager->expects($this->any())->method('getValidator')->will($this->returnValue($validator)); /** @var \Drupal\Core\Entity\EntityStorageInterface|\PHPUnit_Framework_MockObject_MockObject $storage */ $storage = $this->getMock('\\Drupal\\Core\\Entity\\EntityStorageInterface'); $storage->expects($this->any())->method('save')->willReturnCallback(function (ContentEntityInterface $entity) use($storage) { $entity->preSave($storage); }); $this->entityManager->expects($this->any())->method('getStorage')->with($this->entityTypeId)->will($this->returnValue($storage)); // Check that entities can be saved normally when validation is not // required. $this->assertFalse($this->entity->isValidationRequired()); $this->entity->save(); // Make validation required and check that if the entity is validated, it // can be saved normally. $this->entity->setValidationRequired(TRUE); $this->assertTrue($this->entity->isValidationRequired()); $this->entity->validate(); $this->entity->save(); // Check that the "validated" status is reset after saving the entity and // that trying to save a non-validated entity when validation is required // results in an exception. $this->assertTrue($this->entity->isValidationRequired()); $this->entity->save(); }
/** * Saves the entity. * Mostly, you'd better use WorkflowTransitionInterface::execute(); * * {@inheritdoc} */ public function save() { // return parent::save(); // Avoid custom actions for subclass WorkflowScheduledTransition. if ($this->isScheduled()) { return parent::save(); } if ($this->getEntityTypeId() != 'workflow_transition') { return parent::save(); } $transition = $this; $entity_type = $transition->getTargetEntityTypeId(); $entity_id = $transition->getTargetEntityId(); $field_name = $transition->getFieldName(); // Remove any scheduled state transitions. foreach (WorkflowScheduledTransition::loadMultipleByProperties($entity_type, [$entity_id], [], $field_name) as $scheduled_transition) { /* @var WorkflowTransitionInterface $scheduled_transition */ $scheduled_transition->delete(); } // Check for no transition. if ($this->getFromSid() == $this->getToSid()) { if (!$this->getComment()) { // Write comment into history though. return SAVED_UPDATED; } } $hid = $this->id(); if (!$hid) { // Insert the transition. Make sure it hasn't already been inserted. // @todo: Allow a scheduled transition per revision. // @todo: Allow a state per language version (langcode). $found_transition = self::loadByProperties($entity_type, $entity_id, [], $field_name); if ($found_transition && $found_transition->getTimestamp() == REQUEST_TIME && $found_transition->getToSid() == $this->getToSid()) { return SAVED_UPDATED; } else { return parent::save(); } } else { // Update the transition. return parent::save(); } }
/** * Save the field collection item. * * By default, always save the host entity, so modules are able to react * upon changes to the content of the host and any 'last updated' dates of * entities get updated. * * For creating an item a host entity has to be specified via setHostEntity() * before this function is invoked. For the link between the entities to be * fully established, the host entity object has to be updated to include a * reference on this field collection item during saving. So do not skip * saving the host for creating items. * * @param $skip_host_save * (internal) If TRUE is passed, the host entity is not saved automatically * and therefore no link is created between the host and the item or * revision updates might be skipped. Use with care. */ public function save($skip_host_save = FALSE) { /* TODO: Need this. // Make sure we have a host entity during creation. if (!empty($this->is_new) && !(isset($this->hostEntityId) || isset($this->hostEntity) || isset($this->hostEntityRevisionId))) { throw new Exception("Unable to create a field collection item without a given host entity."); } */ // Only save directly if we are told to skip saving the host entity. Else, // we always save via the host as saving the host might trigger saving // field collection items anyway (e.g. if a new revision is created). if ($skip_host_save) { return parent::save(); } else { $host_entity = $this->getHost(); if (!$host_entity) { throw new \Exception('Unable to save a field collection item without a valid reference to a host entity'); } /* TODO: Need this. // If this is creating a new revision, also do so for the host entity. if (!empty($this->revision) || !empty($this->is_new_revision)) { $host_entity->revision = TRUE; if (!empty($this->default_revision)) { entity_revision_set_default($this->hostEntityType, $host_entity); } } */ // Set the host entity reference, so the item will be saved with the host. // @see field_collection_field_presave() $delta = $this->getDelta(); $value = $host_entity->{$this->bundle()}->getValue(); if (isset($delta)) { $value[$delta] = array('field_collection_item' => $this); } else { $value[] = array('field_collection_item' => $this); } $host_entity->{$this->bundle()}->setValue($value); return $host_entity->save(); } }
/** * Saves an entity permanently. * * When saving existing entities, the entity is assumed to be complete, * partial updates of entities are not supported. * * @return int * Either SAVED_NEW or SAVED_UPDATED, depending on the operation performed. * * @throws \Drupal\Core\Entity\EntityStorageException * In case of failures an exception is thrown. */ protected function realSave() { return parent::save(); }
/** * {@inheritdoc} */ public function save() { parent::save(); image_path_flush($this->uri->value); }
/** * {@inheritdoc} */ public function save() { if (is_null($this->getStartTime())) { $this->setCreatedTime(microtime(TRUE) * 1000000); } parent::save(); }