class SmartDefaultSettingsTest

Same name in this branch
  1. 9 core/modules/ckeditor5/tests/src/Kernel/SmartDefaultSettingsTest.php \Drupal\Tests\ckeditor5\Kernel\SmartDefaultSettingsTest
Same name in other branches
  1. 10 core/modules/ckeditor5/tests/src/Unit/SmartDefaultSettingsTest.php \Drupal\Tests\ckeditor5\Unit\SmartDefaultSettingsTest
  2. 10 core/modules/ckeditor5/tests/src/Kernel/SmartDefaultSettingsTest.php \Drupal\Tests\ckeditor5\Kernel\SmartDefaultSettingsTest
  3. 11.x core/modules/ckeditor5/tests/src/Unit/SmartDefaultSettingsTest.php \Drupal\Tests\ckeditor5\Unit\SmartDefaultSettingsTest
  4. 11.x core/modules/ckeditor5/tests/src/Kernel/SmartDefaultSettingsTest.php \Drupal\Tests\ckeditor5\Kernel\SmartDefaultSettingsTest

@coversDefaultClass \Drupal\ckeditor5\SmartDefaultSettings @group ckeditor5

Hierarchy

Expanded class hierarchy of SmartDefaultSettingsTest

File

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

Namespace

Drupal\Tests\ckeditor5\Unit
View source
class SmartDefaultSettingsTest extends UnitTestCase {
    use PrivateMethodUnitTestTrait;
    
    /**
     * @covers ::computeSurplusScore
     * @dataProvider providerSurplusScore
     */
    public function testSurplusScore(HTMLRestrictions $surplus, HTMLRestrictions $needed, int $expected) : void {
        $method = self::getMethod(SmartDefaultSettings::class, 'computeSurplusScore');
        $this->assertSame($expected, $method->invoke(NULL, $surplus, $needed));
    }
    
    /**
     * Data provider for testing computeSurplusScore().
     *
     * @return \Generator
     */
    public function providerSurplusScore() : \Generator {
        $needed = new HTMLRestrictions([
            'code' => FALSE,
        ]);
        (yield 'surplus: 1 tag, 1 attribute, 1 attribute with wildcard restriction' => [
            HTMLRestrictions::fromString('<pre> <code class="language-*">'),
            $needed,
            1001010,
        ]);
        (yield 'surplus: 1 tag, 1 attribute, 2 allowed attribute values' => [
            HTMLRestrictions::fromString('<code class="language-php language-js">'),
            $needed,
            1002,
        ]);
        (yield 'surplus: 2 attributes, 4 allowed attribute values' => [
            // cSpell:disable-next-line
HTMLRestrictions::fromString('<code class="language-php language-js" data-library="highlightjs something">'),
            $needed,
            2004,
        ]);
        (yield 'surplus: 1 any attribute allowed' => [
            HTMLRestrictions::fromString('<code *>'),
            $needed,
            100000,
        ]);
        (yield 'surplus: 1 attribute, 1 attribute any value allowed' => [
            HTMLRestrictions::fromString('<code class>'),
            $needed,
            1100,
        ]);
        (yield 'surplus: 1 tag, 2 wildcard attributes, 2 attributes, 3 attributes any value allowed, 1 wildcard allowed attribute value ' => [
            HTMLRestrictions::fromString('<pre> <code data-config-* data-options-* data-highlight-library class="language-*">'),
            $needed,
            1022310,
        ]);
    }
    
    /**
     * @covers ::getCandidates
     * @covers ::selectCandidate
     * @dataProvider providerCandidates
     */
    public function testCandidates(HTMLRestrictions $provided, HTMLRestrictions $still_needed, array $disabled_plugin_definitions, array $expected_candidates, array $expected_selection = []) : void {
        $get_candidates = self::getMethod(SmartDefaultSettings::class, 'getCandidates');
        $this->assertSame($expected_candidates, $get_candidates->invoke(NULL, $provided, $still_needed, $disabled_plugin_definitions));
        $select_candidate = self::getMethod(SmartDefaultSettings::class, 'selectCandidate');
        $this->assertSame($expected_selection, $select_candidate->invoke(NULL, $expected_candidates, $still_needed, array_keys($provided->getAllowedElements())));
    }
    
    /**
     * Data provider for testing getCandidates() and ::selectCandidate().
     *
     * @return \Generator
     */
    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,
                        ],
                    ],
                ],
            ],
        ]);
    }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overrides
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
PrivateMethodUnitTestTrait::getMethod protected static function Gets a protected/private method to test.
SmartDefaultSettingsTest::providerCandidates public function Data provider for testing getCandidates() and ::selectCandidate().
SmartDefaultSettingsTest::providerSurplusScore public function Data provider for testing computeSurplusScore().
SmartDefaultSettingsTest::testCandidates public function @covers ::getCandidates
@covers ::selectCandidate
@dataProvider providerCandidates
SmartDefaultSettingsTest::testSurplusScore public function @covers ::computeSurplusScore
@dataProvider providerSurplusScore
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals Deprecated protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
UnitTestCase::setUp protected function 338
UnitTestCase::setUpBeforeClass public static function

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.