Unverified Commit 12aec14a authored by Alex Pott's avatar Alex Pott
Browse files

Issue #2750925 by quietone, vakulrai, pavnish, Neslee Canil Pinto,...

Issue #2750925 by quietone, vakulrai, pavnish, Neslee Canil Pinto, ankithashetty, Meenakshi_j, nikitagupta, Suresh Prabhu Parkala, JvE, Gauravmahlawat, larowlan, Kristen Pol, paulocs, acbramley, catch: Text item sample generation fails if max length < 3
parent f0a97d0c
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -80,7 +80,8 @@ public static function generateSampleValue(FieldDefinitionInterface $field_defin
    }
    else {
      // Textfield handling.
      $value = substr($random->sentences(mt_rand(1, $settings['max_length'] / 3), FALSE), 0, $settings['max_length']);
      $max = ceil($settings['max_length'] / 3);
      $value = substr($random->sentences(mt_rand(1, $max), FALSE), 0, $settings['max_length']);
    }

    $values = [
+60 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\Tests\text\Kernel;

use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\KernelTests\KernelTestBase;
use Drupal\text\Plugin\Field\FieldType\TextItemBase;

/**
 * Tests TextItemBase.
 *
 * @group text
 */
class TextItemBaseTest extends KernelTestBase {

  /**
   * {@inheritdoc}
   */
  protected static $modules = ['filter', 'text'];

  /**
   * Tests creation of sample values.
   *
   * @covers ::generateSampleValue
   * @dataProvider providerTextFieldSampleValue
   */
  public function testTextFieldSampleValue($max_length) {
    // Create a text field.
    $field_definition = BaseFieldDefinition::create('text')
      ->setTargetEntityTypeId('foo');

    // Ensure testing of max_lengths from 1 to 3 because generateSampleValue
    // creates a sentence with a maximum number of words set to 1/3 of the
    // max_length of the field.
    $field_definition->setSetting('max_length', $max_length);
    $sample_value = TextItemBase::generateSampleValue($field_definition);
    $this->assertEquals($max_length, strlen($sample_value['value']));
  }

  /**
   * Data provider for testTextFieldSampleValue.
   */
  public function providerTextFieldSampleValue() {
    return [
      [
        1,
      ],
      [
        2,
      ],
      [
        3,
      ],
      [
        4,
      ],
    ];
  }

}