diff --git a/src/Tests/GoogleVisionTestBase.php b/src/Tests/GoogleVisionTestBase.php new file mode 100644 index 0000000..980688b --- /dev/null +++ b/src/Tests/GoogleVisionTestBase.php @@ -0,0 +1,187 @@ +entityTypeManager = $this->container->get('entity_type.manager'); + } + + /** + * Create a new image field. + * + * @param string $name + * The name of the new field (all lowercase), exclude the "field_" prefix. + * @param string $entity_type + * The entity type that this field will be added to. + * @param string $type_name + * The node type that this field will be added to. + * @param array $storage_settings + * A list of field storage settings that will be added to the defaults. + * @param array $field_settings + * A list of instance settings that will be added to the instance defaults. + * @param array $widget_settings + * Widget settings to be added to the widget defaults. + * @param array $formatter_settings + * Formatter settings to be added to the formatter defaults. + * @param string $description + * A description for the field. + */ + public function createImageField($name, $entity_type, $type_name, $storage_settings = [], $field_settings = [], $widget_settings = [], $formatter_settings = [], $description = '') { + FieldStorageConfig::create([ + 'field_name' => $name, + 'entity_type' => $entity_type, + 'type' => 'image', + 'settings' => $storage_settings, + 'cardinality' => 1, + ])->save(); + + $field_config = FieldConfig::create([ + 'field_name' => $name, + 'label' => $name, + 'entity_type' => $entity_type, + 'bundle' => $type_name, + 'settings' => $field_settings, + 'description' => $description, + ])->addConstraint('UserEmotion'); + $field_config->save(); + + $form_display = $this->entityTypeManager + ->getStorage('entity_form_display') + ->load($entity_type . '.' . $type_name . '.' . 'default'); + if (!$form_display) { + $values = [ + 'targetEntityType' => $entity_type, + 'bundle' => $type_name, + 'mode' => 'default', + 'status' => TRUE, + ]; + $form_display = $this->entityTypeManager + ->getStorage('entity_form_display') + ->create($values); + } + $form_display->setComponent($name, [ + 'type' => 'image_image', + 'settings' => $widget_settings, + ])->save(); + + $display = $this->entityTypeManager + ->getStorage('entity_view_display') + ->load($entity_type . '.' . $type_name . '.' . 'default'); + + if (!$display) { + $values = [ + 'targetEntityType' => $entity_type, + 'bundle' => $type_name, + 'mode' => 'default', + 'status' => TRUE, + ]; + $display = $this->entityTypeManager + ->getStorage('entity_view_display') + ->create($values); + } + $display->setComponent($name, [ + 'type' => 'image', + 'settings' => $formatter_settings, + ])->save(); + + return $field_config; + } + + /** + * Get the field id of the image field formed. + * + * @param string $entity_type. + * The entity type the field will be added to. + * @param string $type. + * The node type the field will be added to. + * @return string $field_id. + * The field id of the created field. + */ + public function getImageFieldId($entity_type, $type) { + // Create an image field and add an field to the custom content type. + $storage_settings['default_image'] = [ + 'uuid' => 1, + 'alt' => '', + 'title' => '', + 'width' => 0, + 'height' => 0, + ]; + $field_settings['default_image'] = [ + 'uuid' => 1, + 'alt' => '', + 'title' => '', + 'width' => 0, + 'height' => 0, + ]; + $widget_settings = [ + 'preview_image_style' => 'medium', + ]; + $field = $this->createImageField('images', $entity_type, $type, $storage_settings, $field_settings, $widget_settings); + + // Get the field id and return it. + $field_id = $field->id(); + return $field_id; + } + + /** + * Creates a user with profile picture attached. + * + * @return string $edit['name']. + * The user name of the newly created user. + */ + public function createUserWithProfilePicture() { + //Get an image. + $images = $this->drupalGetTestFiles('image'); + $user = 'user1'; + $pass = 'password1'; + $edit = [ + 'mail' => 'user@user.com', + 'name' => $user, + 'pass[pass1]' => $pass, + 'pass[pass2]' => $pass, + 'files[images_0]' => \Drupal::service('file_system')->realpath($images[0]->uri), + ]; + $this->drupalPostForm(Url::fromRoute('user.admin_create'), $edit, t('Create new account')); + $re_edit = [ + 'pass[pass1]' => $pass, + 'pass[pass2]' => $pass, + 'images[0][alt]' => $this->randomMachineName(), + ]; + $this->drupalPostForm(NULL, $re_edit, t('Create new account')); + + return $edit['name']; + } +} diff --git a/src/Tests/UserEmotionTest.php b/src/Tests/UserEmotionTest.php index d0cb571..0ac3d68 100644 --- a/src/Tests/UserEmotionTest.php +++ b/src/Tests/UserEmotionTest.php @@ -2,29 +2,17 @@ namespace Drupal\google_vision\Tests; -use Drupal\Core\Url; -use Drupal\field\Entity\FieldConfig; -use Drupal\simpletest\WebTestBase; -use Drupal\field\Entity\FieldStorageConfig; - /** * Tests to verify that the Emotion Detection feature works correctly. * * @group google_vision */ -class UserEmotionTest extends WebTestBase { +class UserEmotionTest extends GoogleVisionTestBase { /** * {@inheritdoc} */ - public static $modules = [ - 'google_vision', - 'user', - 'image', - 'field_ui', - 'field', - 'google_vision_test', - ]; + public static $modules = ['user']; /** * A user with permission to create content and upload images. @@ -34,13 +22,6 @@ class UserEmotionTest extends WebTestBase { protected $adminUser; /** - * Retrieves the entity type manager. - * - * @var \Drupal\Core\Entity\EntityTypeManager - */ - protected $entityTypeManager; - - /** * {@inheritdoc} */ protected function setUp() { @@ -54,154 +35,6 @@ class UserEmotionTest extends WebTestBase { 'administer users', ]); $this->drupalLogin($this->adminUser); - - $this->entityTypeManager = \Drupal::entityTypeManager(); - } - - /** - * Create a new image field. - * - * @param string $name - * The name of the new field (all lowercase), exclude the "field_" prefix. - * @param string $entity_type - * The entity type that this field will be added to. - * @param string $type_name - * The node type that this field will be added to. - * @param array $storage_settings - * A list of field storage settings that will be added to the defaults. - * @param array $field_settings - * A list of instance settings that will be added to the instance defaults. - * @param array $widget_settings - * Widget settings to be added to the widget defaults. - * @param array $formatter_settings - * Formatter settings to be added to the formatter defaults. - * @param string $description - * A description for the field. - */ - public function createImageField($name, $entity_type, $type_name, $storage_settings = [], $field_settings = [], $widget_settings = [], $formatter_settings = [], $description = '') { - FieldStorageConfig::create([ - 'field_name' => $name, - 'entity_type' => $entity_type, - 'type' => 'image', - 'settings' => $storage_settings, - 'cardinality' => 1, - ])->save(); - - $field_config = FieldConfig::create([ - 'field_name' => $name, - 'label' => $name, - 'entity_type' => $entity_type, - 'bundle' => $type_name, - 'settings' => $field_settings, - 'description' => $description, - ])->addConstraint('UserEmotion'); - $field_config->save(); - - $form_display = $this->entityTypeManager - ->getStorage('entity_form_display') - ->load($entity_type . '.' . $type_name . '.' . 'default'); - if (!$form_display) { - $values = [ - 'targetEntityType' => $entity_type, - 'bundle' => $type_name, - 'mode' => 'default', - 'status' => TRUE, - ]; - $form_display = $this->entityTypeManager - ->getStorage('entity_form_display') - ->create($values); - } - $form_display->setComponent($name, [ - 'type' => 'image_image', - 'settings' => $widget_settings, - ])->save(); - - $display = $this->entityTypeManager - ->getStorage('entity_view_display') - ->load($entity_type . '.' . $type_name . '.' . 'default'); - - if (!$display) { - $values = [ - 'targetEntityType' => $entity_type, - 'bundle' => $type_name, - 'mode' => 'default', - 'status' => TRUE, - ]; - $display = $this->entityTypeManager - ->getStorage('entity_view_display') - ->create($values); - } - $display->setComponent($name, [ - 'type' => 'image', - 'settings' => $formatter_settings, - ])->save(); - - return $field_config; - } - - /** - * Get the field id of the image field formed. - * - * @param string $entity_type. - * The entity type the field will be added to. - * @param string $type. - * The node type the field will be added to. - * @return string $field_id. - * The field id of the created field. - */ - public function getImageFieldId($entity_type, $type) { - // Create an image field and add an field to the custom content type. - $storage_settings['default_image'] = [ - 'uuid' => 1, - 'alt' => '', - 'title' => '', - 'width' => 0, - 'height' => 0, - ]; - $field_settings['default_image'] = [ - 'uuid' => 1, - 'alt' => '', - 'title' => '', - 'width' => 0, - 'height' => 0, - ]; - $widget_settings = [ - 'preview_image_style' => 'medium', - ]; - $field = $this->createImageField('images', $entity_type, $type, $storage_settings, $field_settings, $widget_settings); - - // Get the field id and return it. - $field_id = $field->id(); - return $field_id; - } - - /** - * Creates a user with profile picture attached. - * - * @return string $edit['name']. - * The user name of the newly created user. - */ - public function createUserWithProfilePicture() { - //Get an image. - $images = $this->drupalGetTestFiles('image'); - $user = 'user1'; - $pass = 'password1'; - $edit = [ - 'mail' => 'user@user.com', - 'name' => $user, - 'pass[pass1]' => $pass, - 'pass[pass2]' => $pass, - 'files[images_0]' => \Drupal::service('file_system')->realpath($images[0]->uri), - ]; - $this->drupalPostForm(Url::fromRoute('user.admin_create'), $edit, t('Create new account')); - $re_edit = [ - 'pass[pass1]' => $pass, - 'pass[pass2]' => $pass, - 'images[0][alt]' => $this->randomMachineName(), - ]; - $this->drupalPostForm(NULL, $re_edit, t('Create new account')); - - return $edit['name']; } /**