Example #1
0
 /**
  * Tests using entity fields of the file field type.
  */
 public function testFileItem()
 {
     // Create a test entity with the
     $entity = entity_create('entity_test');
     $entity->file_test->target_id = $this->file->id();
     $entity->file_test->display = 1;
     $entity->file_test->description = $description = $this->randomName();
     $entity->name->value = $this->randomName();
     $entity->save();
     $entity = entity_load('entity_test', $entity->id());
     $this->assertTrue($entity->file_test instanceof FieldItemListInterface, 'Field implements interface.');
     $this->assertTrue($entity->file_test[0] instanceof FieldItemInterface, 'Field item implements interface.');
     $this->assertEqual($entity->file_test->target_id, $this->file->id());
     $this->assertEqual($entity->file_test->display, 1);
     $this->assertEqual($entity->file_test->description, $description);
     $this->assertEqual($entity->file_test->entity->getFileUri(), $this->file->getFileUri());
     $this->assertEqual($entity->file_test->entity->url(), $url = file_create_url($this->file->getFileUri()));
     $this->assertEqual($entity->file_test->entity->id(), $this->file->id());
     $this->assertEqual($entity->file_test->entity->uuid(), $this->file->uuid());
     // Make sure the computed files reflects updates to the file.
     file_put_contents('public://example-2.txt', $this->randomName());
     $file2 = entity_create('file', array('uri' => 'public://example-2.txt'));
     $file2->save();
     $entity->file_test->target_id = $file2->id();
     $this->assertEqual($entity->file_test->entity->id(), $file2->id());
     $this->assertEqual($entity->file_test->entity->getFileUri(), $file2->getFileUri());
     // Test the deletion of an entity having an entity reference field targeting
     // a non-existing entity.
     $file2->delete();
     $entity->delete();
 }
Example #2
0
 /**
  * Tests using entity fields of the file field type.
  */
 public function testFileItem()
 {
     // Check that the selection handler was automatically assigned to
     // 'default:file'.
     $field_definition = FieldConfig::load('entity_test.entity_test.file_test');
     $handler_id = $field_definition->getSetting('handler');
     $this->assertEqual($handler_id, 'default:file');
     // Create a test entity with the
     $entity = EntityTest::create();
     $entity->file_test->target_id = $this->file->id();
     $entity->file_test->display = 1;
     $entity->file_test->description = $description = $this->randomMachineName();
     $entity->name->value = $this->randomMachineName();
     $entity->save();
     $entity = EntityTest::load($entity->id());
     $this->assertTrue($entity->file_test instanceof FieldItemListInterface, 'Field implements interface.');
     $this->assertTrue($entity->file_test[0] instanceof FieldItemInterface, 'Field item implements interface.');
     $this->assertEqual($entity->file_test->target_id, $this->file->id());
     $this->assertEqual($entity->file_test->display, 1);
     $this->assertEqual($entity->file_test->description, $description);
     $this->assertEqual($entity->file_test->entity->getFileUri(), $this->file->getFileUri());
     $this->assertEqual($entity->file_test->entity->url(), $url = file_create_url($this->file->getFileUri()));
     $this->assertEqual($entity->file_test->entity->id(), $this->file->id());
     $this->assertEqual($entity->file_test->entity->uuid(), $this->file->uuid());
     // Make sure the computed files reflects updates to the file.
     file_put_contents('public://example-2.txt', $this->randomMachineName());
     $file2 = File::create(['uri' => 'public://example-2.txt']);
     $file2->save();
     $entity->file_test->target_id = $file2->id();
     $this->assertEqual($entity->file_test->entity->id(), $file2->id());
     $this->assertEqual($entity->file_test->entity->getFileUri(), $file2->getFileUri());
     // Test the deletion of an entity having an entity reference field targeting
     // a non-existing entity.
     $file2->delete();
     $entity->delete();
     // Test the generateSampleValue() method.
     $entity = EntityTest::create();
     $entity->file_test->generateSampleItems();
     $this->entityValidateAndSave($entity);
     // Verify that the sample file was stored in the correct directory.
     $uri = $entity->file_test->entity->getFileUri();
     $this->assertEqual($this->directory, dirname(file_uri_target($uri)));
     // Make sure the computed files reflects updates to the file.
     file_put_contents('public://example-3.txt', $this->randomMachineName());
     // Test unsaved file entity.
     $file3 = File::create(['uri' => 'public://example-3.txt']);
     $display = entity_get_display('entity_test', 'entity_test', 'default');
     $display->setComponent('file_test', ['label' => 'above', 'type' => 'file_default', 'weight' => 1])->save();
     $entity = EntityTest::create();
     $entity->file_test = array('entity' => $file3);
     $uri = $file3->getFileUri();
     $output = entity_view($entity, 'default');
     \Drupal::service('renderer')->renderRoot($output);
     $this->assertTrue(!empty($entity->file_test->entity));
     $this->assertEqual($entity->file_test->entity->getFileUri(), $uri);
 }
 /**
  * Tests using entity fields of the image field type.
  */
 public function testImageItem()
 {
     // Create a test entity with the image field set.
     $entity = EntityTest::create();
     $entity->image_test->target_id = $this->image->id();
     $entity->image_test->alt = $alt = $this->randomMachineName();
     $entity->image_test->title = $title = $this->randomMachineName();
     $entity->name->value = $this->randomMachineName();
     $entity->save();
     $entity = entity_load('entity_test', $entity->id());
     $this->assertTrue($entity->image_test instanceof FieldItemListInterface, 'Field implements interface.');
     $this->assertTrue($entity->image_test[0] instanceof FieldItemInterface, 'Field item implements interface.');
     $this->assertEqual($entity->image_test->target_id, $this->image->id());
     $this->assertEqual($entity->image_test->alt, $alt);
     $this->assertEqual($entity->image_test->title, $title);
     $image = $this->imageFactory->get('public://example.jpg');
     $this->assertEqual($entity->image_test->width, $image->getWidth());
     $this->assertEqual($entity->image_test->height, $image->getHeight());
     $this->assertEqual($entity->image_test->entity->id(), $this->image->id());
     $this->assertEqual($entity->image_test->entity->uuid(), $this->image->uuid());
     // Make sure the computed entity reflects updates to the referenced file.
     file_unmanaged_copy(\Drupal::root() . '/core/misc/druplicon.png', 'public://example-2.jpg');
     $image2 = File::create(['uri' => 'public://example-2.jpg']);
     $image2->save();
     $entity->image_test->target_id = $image2->id();
     $entity->image_test->alt = $new_alt = $this->randomMachineName();
     // The width and height is only updated when width is not set.
     $entity->image_test->width = NULL;
     $entity->save();
     $this->assertEqual($entity->image_test->entity->id(), $image2->id());
     $this->assertEqual($entity->image_test->entity->getFileUri(), $image2->getFileUri());
     $image = $this->imageFactory->get('public://example-2.jpg');
     $this->assertEqual($entity->image_test->width, $image->getWidth());
     $this->assertEqual($entity->image_test->height, $image->getHeight());
     $this->assertEqual($entity->image_test->alt, $new_alt);
     // Check that the image item can be set to the referenced file directly.
     $entity->image_test = $this->image;
     $this->assertEqual($entity->image_test->target_id, $this->image->id());
     // Delete the image and try to save the entity again.
     $this->image->delete();
     $entity = EntityTest::create(array('mame' => $this->randomMachineName()));
     $entity->save();
     // Test image item properties.
     $expected = array('target_id', 'entity', 'alt', 'title', 'width', 'height');
     $properties = $entity->getFieldDefinition('image_test')->getFieldStorageDefinition()->getPropertyDefinitions();
     $this->assertEqual(array_keys($properties), $expected);
     // Test the generateSampleValue() method.
     $entity = EntityTest::create();
     $entity->image_test->generateSampleItems();
     $this->entityValidateAndSave($entity);
     $this->assertEqual($entity->image_test->entity->get('filemime')->value, 'image/jpeg');
 }
Example #4
0
 /**
  * Tests using entity fields of the file field type.
  */
 public function testFileItem()
 {
     // Create a test entity with the
     $entity = entity_create('entity_test');
     $entity->file_test->target_id = $this->file->id();
     $entity->file_test->display = 1;
     $entity->file_test->description = $description = $this->randomMachineName();
     $entity->name->value = $this->randomMachineName();
     $entity->save();
     $entity = entity_load('entity_test', $entity->id());
     $this->assertTrue($entity->file_test instanceof FieldItemListInterface, 'Field implements interface.');
     $this->assertTrue($entity->file_test[0] instanceof FieldItemInterface, 'Field item implements interface.');
     $this->assertEqual($entity->file_test->target_id, $this->file->id());
     $this->assertEqual($entity->file_test->display, 1);
     $this->assertEqual($entity->file_test->description, $description);
     $this->assertEqual($entity->file_test->entity->getFileUri(), $this->file->getFileUri());
     $this->assertEqual($entity->file_test->entity->url(), $url = file_create_url($this->file->getFileUri()));
     $this->assertEqual($entity->file_test->entity->id(), $this->file->id());
     $this->assertEqual($entity->file_test->entity->uuid(), $this->file->uuid());
     // Make sure the computed files reflects updates to the file.
     file_put_contents('public://example-2.txt', $this->randomMachineName());
     $file2 = entity_create('file', array('uri' => 'public://example-2.txt'));
     $file2->save();
     $entity->file_test->target_id = $file2->id();
     $this->assertEqual($entity->file_test->entity->id(), $file2->id());
     $this->assertEqual($entity->file_test->entity->getFileUri(), $file2->getFileUri());
     // Test the deletion of an entity having an entity reference field targeting
     // a non-existing entity.
     $file2->delete();
     $entity->delete();
     // Test the generateSampleValue() method.
     $entity = entity_create('entity_test');
     $entity->file_test->generateSampleItems();
     $this->entityValidateAndSave($entity);
     // Make sure the computed files reflects updates to the file.
     file_put_contents('public://example-3.txt', $this->randomMachineName());
     // Test unsaved file entity.
     $file3 = entity_create('file', array('uri' => 'public://example-3.txt'));
     $display = entity_get_display('entity_test', 'entity_test', 'default');
     $display->setComponent('file_test', ['label' => 'above', 'type' => 'file_default', 'weight' => 1])->save();
     $entity = entity_create('entity_test');
     $entity->file_test = array('entity' => $file3);
     $uri = $file3->getFileUri();
     $output = entity_view($entity, 'default');
     \Drupal::service('renderer')->renderRoot($output);
     $this->assertTrue(!empty($entity->file_test->entity));
     $this->assertEqual($entity->file_test->entity->getFileUri(), $uri);
 }
 /**
  * Tests using entity fields of the image field type.
  */
 public function testImageItem()
 {
     // Create a test entity with the image field set.
     $entity = entity_create('entity_test');
     $entity->image_test->target_id = $this->image->id();
     $entity->image_test->alt = $alt = $this->randomMachineName();
     $entity->image_test->title = $title = $this->randomMachineName();
     $entity->name->value = $this->randomMachineName();
     $entity->save();
     $entity = entity_load('entity_test', $entity->id());
     $this->assertTrue($entity->image_test instanceof FieldItemListInterface, 'Field implements interface.');
     $this->assertTrue($entity->image_test[0] instanceof FieldItemInterface, 'Field item implements interface.');
     $this->assertEqual($entity->image_test->target_id, $this->image->id());
     $this->assertEqual($entity->image_test->alt, $alt);
     $this->assertEqual($entity->image_test->title, $title);
     $image = $this->imageFactory->get('public://example.jpg');
     $this->assertEqual($entity->image_test->width, $image->getWidth());
     $this->assertEqual($entity->image_test->height, $image->getHeight());
     $this->assertEqual($entity->image_test->entity->id(), $this->image->id());
     $this->assertEqual($entity->image_test->entity->uuid(), $this->image->uuid());
     // Make sure the computed entity reflects updates to the referenced file.
     file_unmanaged_copy(DRUPAL_ROOT . '/core/misc/feed.png', 'public://example-2.jpg');
     $image2 = entity_create('file', array('uri' => 'public://example-2.jpg'));
     $image2->save();
     $entity->image_test->target_id = $image2->id();
     $entity->image_test->alt = $new_alt = $this->randomMachineName();
     // The width and height is only updated when width is not set.
     $entity->image_test->width = NULL;
     $entity->save();
     $this->assertEqual($entity->image_test->entity->id(), $image2->id());
     $this->assertEqual($entity->image_test->entity->getFileUri(), $image2->getFileUri());
     $image = $this->imageFactory->get('public://example-2.jpg');
     $this->assertEqual($entity->image_test->width, $image->getWidth());
     $this->assertEqual($entity->image_test->height, $image->getHeight());
     $this->assertEqual($entity->image_test->alt, $new_alt);
     // Check that the image item can be set to the referenced file directly.
     $entity->image_test = $this->image;
     $this->assertEqual($entity->image_test->target_id, $this->image->id());
     // Delete the image and try to save the entity again.
     $this->image->delete();
     $entity = entity_create('entity_test', array('mame' => $this->randomMachineName()));
     $entity->save();
     // Test the generateSampleValue() method.
     $entity = entity_create('entity_test');
     $entity->image_test->generateSampleItems();
     $this->entityValidateAndSave($entity);
 }
 /**
  * Tests usage of the image field formatters.
  */
 function testImageFormatterTheme()
 {
     /** @var \Drupal\Core\Render\RendererInterface $renderer */
     $renderer = $this->container->get('renderer');
     // Create an image.
     $files = $this->drupalGetTestFiles('image');
     $file = reset($files);
     $original_uri = file_unmanaged_copy($file->uri, 'public://', FILE_EXISTS_RENAME);
     // Create a style.
     $style = entity_create('image_style', array('name' => 'test', 'label' => 'Test'));
     $style->save();
     $url = $style->buildUrl($original_uri);
     // Create a test entity with the image field set.
     $entity = entity_create('entity_test');
     $entity->image_test->target_id = $this->image->id();
     $entity->image_test->alt = NULL;
     $entity->image_test->uri = $original_uri;
     $image = $this->imageFactory->get('public://example.jpg');
     $entity->save();
     // Create the base element that we'll use in the tests below.
     $path = $this->randomMachineName();
     $base_element = array('#theme' => 'image_formatter', '#image_style' => 'test', '#item' => $entity->image_test, '#url' => Url::fromUri('base:' . $path));
     // Test using theme_image_formatter() with a NULL value for the alt option.
     $element = $base_element;
     $this->setRawContent($renderer->renderRoot($element));
     $elements = $this->xpath('//a[@href=:path]/img[@class="image-style-test" and @src=:url and @width=:width and @height=:height]', array(':path' => base_path() . $path, ':url' => $url, ':width' => $image->getWidth(), ':height' => $image->getHeight()));
     $this->assertEqual(count($elements), 1, 'theme_image_formatter() correctly renders with a NULL value for the alt option.');
     // Test using theme_image_formatter() without an image title, alt text, or
     // link options.
     $element = $base_element;
     $element['#item']->alt = '';
     $this->setRawContent($renderer->renderRoot($element));
     $elements = $this->xpath('//a[@href=:path]/img[@class="image-style-test" and @src=:url and @width=:width and @height=:height and @alt=""]', array(':path' => base_path() . $path, ':url' => $url, ':width' => $image->getWidth(), ':height' => $image->getHeight()));
     $this->assertEqual(count($elements), 1, 'theme_image_formatter() correctly renders without title, alt, or path options.');
     // Link the image to a fragment on the page, and not a full URL.
     $fragment = $this->randomMachineName();
     $element = $base_element;
     $element['#url'] = Url::fromRoute('<none>', [], ['fragment' => $fragment]);
     $this->setRawContent($renderer->renderRoot($element));
     $elements = $this->xpath('//a[@href=:fragment]/img[@class="image-style-test" and @src=:url and @width=:width and @height=:height and @alt=""]', array(':fragment' => '#' . $fragment, ':url' => $url, ':width' => $image->getWidth(), ':height' => $image->getHeight()));
     $this->assertEqual(count($elements), 1, 'theme_image_formatter() correctly renders a link fragment.');
 }