/**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();

    $this->installSchema('user', 'users_data');

    $this->flagCountService = \Drupal::service('flag.count');
    $this->flaggingDelete = \Drupal::service('flagging');

    // Create a flag.
    $this->flag = Flag::create([
      'id' => strtolower($this->randomMachineName()),
      'label' => $this->randomString(),
      'entity_type' => 'node',
      'bundles' => ['article'],
      'flag_type' => 'entity:node',
      'link_type' => 'reload',
      'flagTypeConfig' => [],
      'linkTypeConfig' => [],
    ]);
    $this->flag->save();

    // Create a user who may flag.
    $this->adminUser = $this->createUser([
      'administer flags',
    ]);

    $article = NodeType::create(['type' => 'article']);
    $article->save();

    // Create a node to flag.
    $this->node = Node::create([
      'type' => 'article',
      'title' => $this->randomMachineName(8),
    ]);
    $this->node->save();
  }
  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    // Toggle the flag state.
    if ($this->flag->isEnabled()) {
      $this->flag->disable();
    }
    else {
      $this->flag->enable();
    }

    // Save The flag entity.
    $this->flag->save();

    // Redirect to the flag admin page.
    $form_state->setRedirect('entity.flag.collection');
  }
  /**
   * Test the link output.
   */
  public function testLinkLocation() {
    // Turn off all link output for the flag.
    $flag_config = $this->flag->getFlagTypePlugin()->getConfiguration();
    $flag_config['show_as_field'] = FALSE;
    $flag_config['show_in_links'] = [];
    $this->flag->getFlagTypePlugin()->setConfiguration($flag_config);
    $this->flag->save();

    // Check the full node shows no flag link.
    $this->drupalGet('node/' . $this->node->id());
    $this->assertNoPseudofield($this->flag, $this->node);
    // TODO: check no entity link.

    // Check the teaser view mode for the node shows no flag link.
    $this->drupalGet('node');
    $this->assertNoPseudofield($this->flag, $this->node);
    // TODO: check no entity link.

    // Turn on 'show as field'.
    // By default, this will be visible on the field display configuration.
    $flag_config = $this->flag->getFlagTypePlugin()->getConfiguration();
    $flag_config['show_as_field'] = TRUE;
    $flag_config['show_in_links'] = [];
    $this->flag->getFlagTypePlugin()->setConfiguration($flag_config);
    $this->flag->save();

    // Check the full node shows the flag link as a field.
    $this->drupalGet('node/' . $this->node->id());
    $this->assertPseudofield($this->flag, $this->node);
    // TODO: check no entity link.

    // Check the teaser view mode shows the flag link as a field.
    $this->drupalGet('node');
    $this->assertPseudofield($this->flag, $this->node);
    // TODO: check no entity link.

    // Hide the flag field on teaser view mode.
    $edit = [
      'fields[flag_' . $this->flag->id() . '][type]' => 'hidden',
    ];
    $this->drupalPostForm('admin/structure/types/manage/article/display/teaser', $edit, $this->t('Save'));

    // Check the form was saved successfully.
    $this->assertText('Your settings have been saved.');

    // Check the full node still shows the flag link as a field.
    $this->drupalGet('node/' . $this->node->id());
    $this->assertPseudofield($this->flag, $this->node);
    // TODO: check no entity link.

    // Check the teaser view mode does not show the flag link as a field.
    $this->drupalGet('node');
    $this->assertNoPseudofield($this->flag, $this->node);
    // TODO: check no entity link.

    // TODO:
    // Turn on the entity link, and turn off the field.
    // Check the full and teaser view modes.
    // Turn off the entity link for one view mode.
    // Check both view modes are as expected.
  }