Chris@0: profile != 'standard') { Chris@0: $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']); Chris@0: $this->drupalCreateContentType(['type' => 'article', 'name' => 'Article']); Chris@0: } Chris@0: Chris@0: $this->adminUser = $this->drupalCreateUser(['access content', 'access administration pages', 'administer site configuration', 'administer content types', 'administer node fields', 'administer nodes', 'create article content', 'edit any article content', 'delete any article content', 'administer image styles', 'administer node display']); Chris@0: $this->drupalLogin($this->adminUser); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Preview an image in a node. Chris@0: * Chris@0: * @param \Drupal\Core\Image\ImageInterface $image Chris@0: * A file object representing the image to upload. Chris@0: * @param string $field_name Chris@0: * Name of the image field the image should be attached to. Chris@0: * @param string $type Chris@0: * The type of node to create. Chris@0: */ Chris@0: public function previewNodeImage($image, $field_name, $type) { Chris@0: $edit = [ Chris@0: 'title[0][value]' => $this->randomMachineName(), Chris@0: ]; Chris@14: $edit['files[' . $field_name . '_0]'] = \Drupal::service('file_system')->realpath($image->uri); Chris@0: $this->drupalPostForm('node/add/' . $type, $edit, t('Preview')); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Upload an image to a node. Chris@0: * Chris@0: * @param $image Chris@0: * A file object representing the image to upload. Chris@0: * @param $field_name Chris@0: * Name of the image field the image should be attached to. Chris@0: * @param $type Chris@0: * The type of node to create. Chris@0: * @param $alt Chris@0: * The alt text for the image. Use if the field settings require alt text. Chris@0: */ Chris@0: public function uploadNodeImage($image, $field_name, $type, $alt = '') { Chris@0: $edit = [ Chris@0: 'title[0][value]' => $this->randomMachineName(), Chris@0: ]; Chris@14: $edit['files[' . $field_name . '_0]'] = \Drupal::service('file_system')->realpath($image->uri); Chris@0: $this->drupalPostForm('node/add/' . $type, $edit, t('Save')); Chris@0: if ($alt) { Chris@0: // Add alt text. Chris@0: $this->drupalPostForm(NULL, [$field_name . '[0][alt]' => $alt], t('Save')); Chris@0: } Chris@0: Chris@0: // Retrieve ID of the newly created node from the current URL. Chris@0: $matches = []; Chris@0: preg_match('/node\/([0-9]+)/', $this->getUrl(), $matches); Chris@0: return isset($matches[1]) ? $matches[1] : FALSE; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Retrieves the fid of the last inserted file. Chris@0: */ Chris@0: protected function getLastFileId() { Chris@18: return (int) \Drupal::entityQueryAggregate('file')->aggregate('fid', 'max')->execute()[0]['fid_max']; Chris@0: } Chris@0: Chris@0: }