You are here

public function SmartDefaultSettingsTest::providerCandidates in Drupal 10

Data provider for testing getCandidates() and ::selectCandidate().

Return value

\Generator

File

core/modules/ckeditor5/tests/src/Unit/SmartDefaultSettingsTest.php, line 95

Class

SmartDefaultSettingsTest
@coversDefaultClass \Drupal\ckeditor5\SmartDefaultSettings @group ckeditor5

Namespace

Drupal\Tests\ckeditor5\Unit

Code

public function providerCandidates() : \Generator {
  $generate_definition = function (string $label_and_id, array $overrides) : CKEditor5PluginDefinition {
    $annotation = [
      'provider' => 'test',
      'id' => "test_{$label_and_id}",
      'drupal' => [
        'label' => "{$label_and_id}",
      ],
      'ckeditor5' => [
        'plugins' => [],
      ],
    ];
    foreach ($overrides as $path => $value) {
      NestedArray::setValue($annotation, explode('.', $path), $value);
    }
    $annotation_instance = new CKEditor5Plugin($annotation);
    $definition = $annotation_instance
      ->get();
    return $definition;
  };
  (yield 'Tag needed, no match due to no plugin supporting it' => [
    HTMLRestrictions::emptySet(),
    HTMLRestrictions::fromString('<foo>'),
    [
      $generate_definition('foo', [
        'drupal.elements' => FALSE,
      ]),
    ],
    [],
    [],
  ]);
  (yield 'Tag needed, single match without surplus' => [
    HTMLRestrictions::emptySet(),
    HTMLRestrictions::fromString('<foo>'),
    [
      $generate_definition('foo', [
        'drupal.elements' => [
          '<foo>',
        ],
      ]),
    ],
    [
      'foo' => [
        '-attributes-none-' => [
          'test_foo' => 0,
        ],
      ],
    ],
    // Perfect surplus score, but also only choice available.
    [
      'test_foo' => [
        '-attributes-none-' => [
          'foo' => NULL,
        ],
      ],
    ],
  ]);
  (yield 'Tag needed, no match due to plugins only supporting attributes on the needed tag' => [
    HTMLRestrictions::emptySet(),
    HTMLRestrictions::fromString('<foo>'),
    [
      $generate_definition('foo', [
        'drupal.elements' => [
          '<foo bar baz>',
        ],
      ]),
    ],
    [],
    // No choice available due to the tag not being creatable.
    [],
  ]);
  $various_foo_definitions = [
    $generate_definition('all_attrs', [
      'drupal.elements' => [
        '<foo *>',
      ],
    ]),
    $generate_definition('attrs', [
      'drupal.elements' => [
        '<foo bar baz>',
      ],
    ]),
    $generate_definition('attr_values', [
      'drupal.elements' => [
        '<foo bar="a b">',
      ],
    ]),
    $generate_definition('plain', [
      'drupal.elements' => [
        '<foo>',
      ],
    ]),
    $generate_definition('tags', [
      'drupal.elements' => [
        '<foo>',
        '<bar>',
        '<baz>',
      ],
    ]),
    $generate_definition('tags_and_attrs', [
      'drupal.elements' => [
        '<foo bar baz>',
        '<bar>',
        '<baz>',
      ],
    ]),
    $generate_definition('tags_and_attr_values', [
      'drupal.elements' => [
        '<foo bar="a b" baz>',
        '<bar>',
        '<baz>',
      ],
    ]),
  ];
  (yield 'Tag needed, multiple matches' => [
    HTMLRestrictions::emptySet(),
    HTMLRestrictions::fromString('<foo>'),
    $various_foo_definitions,
    [
      'foo' => [
        '-attributes-none-' => [
          'test_plain' => 0,
          'test_tags' => 2000000,
        ],
      ],
    ],
    // test_plain (elements: `<foo>`) has perfect surplus score.
    [
      'test_plain' => [
        '-attributes-none-' => [
          'foo' => NULL,
        ],
      ],
    ],
  ]);
  (yield 'Attribute needed, multiple matches' => [
    HTMLRestrictions::fromString('<foo>'),
    HTMLRestrictions::fromString('<foo bar>'),
    $various_foo_definitions,
    [
      'foo' => [
        'bar' => [
          // Because `<foo bar>` allowed.
          TRUE => [
            'test_all_attrs' => 100000,
            // This will be selected.
            'test_attrs' => 1100,
            'test_tags_and_attrs' => 2001100,
          ],
          // Because `<foo bar="a">` allowed.
          'a' => [
            TRUE => [
              'test_attr_values' => 0,
              'test_tags_and_attr_values' => 2001100,
            ],
          ],
          // Because `<foo bar="b">` allowed.
          'b' => [
            TRUE => [
              'test_attr_values' => 0,
              'test_tags_and_attr_values' => 2001100,
            ],
          ],
        ],
        // Note that `test_plain` and `test_tags` are absent.
        '-attributes-none-' => [
          'test_all_attrs' => 100000,
          'test_attrs' => 1100,
          'test_attr_values' => 0,
          'test_tags_and_attrs' => 2001100,
          'test_tags_and_attr_values' => 2001100,
        ],
      ],
    ],
    // test_attrs (elements: `<foo bar baz>`) has best surplus score, despite
    // allowing one extra attribute and any value on that attribute.
    [
      'test_attrs' => [
        'bar' => [
          'foo' => TRUE,
        ],
      ],
    ],
  ]);
  (yield 'Attribute value needed, multiple matches' => [
    HTMLRestrictions::fromString('<foo>'),
    HTMLRestrictions::fromString('<foo bar="b">'),
    $various_foo_definitions,
    [
      'foo' => [
        'bar' => [
          'b' => [
            TRUE => [
              'test_all_attrs' => 100000,
              'test_attrs' => 2200,
              // This will be selected.
              'test_attr_values' => 1001,
              'test_tags_and_attrs' => 2002200,
              'test_tags_and_attr_values' => 2002101,
            ],
          ],
        ],
        // Note that `test_plain` and `test_tags` are absent.
        '-attributes-none-' => [
          'test_all_attrs' => 100000,
          'test_attrs' => 2200,
          'test_attr_values' => 1001,
          'test_tags_and_attrs' => 2002200,
          'test_tags_and_attr_values' => 2002101,
        ],
      ],
    ],
    // test_attr_values (elements: `<foo bar="a b">`) has best surplus score,
    // despite allowing one extra attribute value.
    [
      'test_attr_values' => [
        'bar' => [
          'foo' => [
            'b' => TRUE,
          ],
        ],
      ],
    ],
  ]);
}