You are here

class TypogrifyClassTest in Typogrify 8

Testing all methods on the Typogrify class and their interaction.

@group typogrify

Hierarchy

Expanded class hierarchy of TypogrifyClassTest

File

tests/src/Unit/TypogrifyClassTest.php, line 13

Namespace

Drupal\Tests\typogrify\Unit
View source
class TypogrifyClassTest extends UnitTestCase {

  /**
   * {@inheritdoc}
   */
  public static $modules = [
    'typogrify',
  ];

  /**
   * Tests ampersand-wrapping.
   */
  public function testAmpersandWrapping() {
    $before = 'John & Robert.';
    $after = 'John <span class="amp">&amp;</span> Robert.';
    $this
      ->assertSame(Typogrify::amp($before), $after, 'Basic ampersand wrapping');
    $before = 'P&T';
    $after = 'P&T';
    $this
      ->assertSame(Typogrify::amp($before), $after, "Don't mess with ampersands in words");
    $before = 'advanced robotics &&nbsp;computing...';
    $after = 'advanced robotics <span class="amp">&amp;</span>&nbsp;computing...';
    $this
      ->assertSame(Typogrify::amp($before), $after, 'One space as HTML entity.');
    $before = 'advanced robotics &amp; computing...';
    $after = 'advanced robotics <span class="amp">&amp;</span> computing...';
    $this
      ->assertSame(Typogrify::amp($before), $after, 'Ampersand as HTML entity.');
    $before = 'advanced robotics&nbsp;&amp;&nbsp;computing...';
    $after = 'advanced robotics&nbsp;<span class="amp">&amp;</span>&nbsp;computing...';
    $this
      ->assertSame(Typogrify::amp($before), $after, 'Both spaces and ampersand as HTML entities.');
    $before = 'P&amp;T had many clients, of which DD&T &amp; Cronhammar, Kronhammer & Hjort were the largest';
    $after = 'P&amp;T had many clients, of which DD&T <span class="amp">&amp;</span> Cronhammar, Kronhammer <span class="amp">&amp;</span> Hjort were the largest';
    $this
      ->assertSame(Typogrify::amp($before), $after, 'Both spaces and ampersand as HTML entities.');
  }

  /**
   * Widont test.
   */
  public function testWidont() {
    $before = 'A very simple test';
    $after = 'A very simple&nbsp;test';
    $this
      ->assertSame(Typogrify::widont($before), $after, 'Basic widont. Once sentence, no HTML.');

    // Single word items shouldn't be changed.
    $before = 'Test';
    $after = 'Test';
    $this
      ->assertSame(Typogrify::widont($before), $after, 'Single word test #1');
    $before = ' Test';
    $after = ' Test';
    $this
      ->assertSame(Typogrify::widont($before), $after, 'Single word test #2');
    $before = '<ul><li>Test</p></li><ul>';
    $after = '<ul><li>Test</p></li><ul>';
    $this
      ->assertSame(Typogrify::widont($before), $after, 'Single word test #3');
    $before = '<ul><li> Test</p></li><ul>';
    $after = '<ul><li> Test</p></li><ul>';
    $this
      ->assertSame(Typogrify::widont($before), $after, 'Single word test #4');
    $before = '<p>In a couple of paragraphs</p><p>paragraph two</p>';
    $after = '<p>In a couple of&nbsp;paragraphs</p><p>paragraph&nbsp;two</p>';
    $this
      ->assertSame(Typogrify::widont($before), $after, 'Two paragraphs');
    $before = '<h1><a href="#">In a link inside a heading</i> </a></h1>';
    $after = '<h1><a href="#">In a link inside a&nbsp;heading</i> </a></h1>';
    $this
      ->assertSame(Typogrify::widont($before), $after, 'In a link inside a heading');
    $before = '<h1><a href="#">In a link</a> followed by other text</h1>';
    $after = '<h1><a href="#">In a link</a> followed by other&nbsp;text</h1>';
    $this
      ->assertSame(Typogrify::widont($before), $after, 'In a link followed by other text');
    $before = '<h1><a href="#"></a></h1>';
    $after = '<h1><a href="#"></a></h1>';
    $this
      ->assertSame(Typogrify::widont($before), $after, "Empty HTML tags shouldn't cause errors");
    $before = '<div>Divs get no love!</div>';
    $after = '<div>Divs get no love!</div>';
    $this
      ->assertSame(Typogrify::widont($before), $after, 'Divs get no love');
    $before = '<pre>Neither do PREs</pre>';
    $after = '<pre>Neither do PREs</pre>';
    $this
      ->assertSame(Typogrify::widont($before), $after, 'Neither do PREs');
    $before = '<div><p>But divs with paragraphs do!</p></div>';
    $after = '<div><p>But divs with paragraphs&nbsp;do!</p></div>';
    $this
      ->assertSame(Typogrify::widont($before), $after, 'But divs with paragraphs do!');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
TypogrifyClassTest::$modules public static property
TypogrifyClassTest::testAmpersandWrapping public function Tests ampersand-wrapping.
TypogrifyClassTest::testWidont public function Widont test.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
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 340