|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @group formatting |
| 5 | + * |
| 6 | + * @covers ::wp_truncate_slug |
| 7 | + */ |
| 8 | +class Tests_Formatting_wpTruncateSlug extends WP_UnitTestCase { |
| 9 | + |
| 10 | + /** |
| 11 | + * Tests that wp_truncate_slug() correctly truncates slugs. |
| 12 | + * |
| 13 | + * @ticket 56868 |
| 14 | + * @ticket 46010 |
| 15 | + * |
| 16 | + * @dataProvider data_wp_truncate_slug_should_truncate |
| 17 | + * |
| 18 | + * @param string $slug The slug to truncate. |
| 19 | + * @param int $length Max length of the slug. |
| 20 | + * @param string $expected The expected truncated slug. |
| 21 | + * @param string $message Test feedback message. |
| 22 | + */ |
| 23 | + public function test_wp_truncate_slug_should_truncate( $slug, $length, $expected, $message ) { |
| 24 | + $this->assertSame( $expected, wp_truncate_slug( $slug, $length ), $message ); |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Data provider for test_wp_truncate_slug_should_truncate(). |
| 29 | + * |
| 30 | + * @return array[] |
| 31 | + */ |
| 32 | + public function data_wp_truncate_slug_should_truncate() { |
| 33 | + return array( |
| 34 | + 'a slug that is too long' => array( |
| 35 | + 'slug' => 'truncated slug', |
| 36 | + 'length' => 9, |
| 37 | + 'expected' => 'truncated', |
| 38 | + 'message' => '"truncated slug" should have been truncated to "truncated".', |
| 39 | + ), |
| 40 | + 'a slug that is too long and ends with a dash' => array( |
| 41 | + 'slug' => 'truncated-slug', |
| 42 | + 'length' => 10, |
| 43 | + 'expected' => 'truncated', |
| 44 | + 'message' => '"truncated-slug" should have been truncated to "truncated".', |
| 45 | + ), |
| 46 | + |
| 47 | + // URL-encoded characters. |
| 48 | + 'URL-encoded characters and "length" includes the first URL-encoded character' => array( |
| 49 | + 'slug' => 'myslug%2F', |
| 50 | + 'length' => 7, |
| 51 | + 'expected' => 'myslug', |
| 52 | + 'message' => '"myslug%2F" should have been truncated to "myslug".', |
| 53 | + ), |
| 54 | + 'URL-encoded characters and "length" includes the second URL-encoded character' => array( |
| 55 | + 'slug' => 'myslug%2F', |
| 56 | + 'length' => 8, |
| 57 | + 'expected' => 'myslug', |
| 58 | + 'message' => '"myslug%2F" should have been truncated to "myslug".', |
| 59 | + ), |
| 60 | + 'URL-encoded characters and "length" includes the third URL-encoded character' => array( |
| 61 | + 'slug' => 'myslug%2F', |
| 62 | + 'length' => 9, |
| 63 | + 'expected' => 'myslug%2F', |
| 64 | + 'message' => '"myslug%2F" should have been truncated to "myslug%2F".', |
| 65 | + ), |
| 66 | + |
| 67 | + // URL-encoded accent characters. |
| 68 | + 'URL-encoded accent characters and "length" includes the first URL-encoded character' => array( |
| 69 | + 'slug' => 'myslug%C4%85', |
| 70 | + 'length' => 7, |
| 71 | + 'expected' => 'myslug', |
| 72 | + 'message' => '"myslug%C4%85" should have been truncated to "myslug".', |
| 73 | + ), |
| 74 | + 'URL-encoded accent characters and "length" includes the second URL-encoded character' => array( |
| 75 | + 'slug' => 'myslug%C4%85', |
| 76 | + 'length' => 8, |
| 77 | + 'expected' => 'myslug', |
| 78 | + 'message' => '"myslug%C4%85" should have been truncated to "myslug".', |
| 79 | + ), |
| 80 | + 'URL-encoded accent characters and "length" includes the third URL-encoded character' => array( |
| 81 | + 'slug' => 'myslug%C4%85', |
| 82 | + 'length' => 9, |
| 83 | + 'expected' => 'myslug', |
| 84 | + 'message' => '"myslug%C4%85" should have been truncated to "myslug".', |
| 85 | + ), |
| 86 | + 'URL-encoded accent characters and "length" includes the fourth URL-encoded character' => array( |
| 87 | + 'slug' => 'myslug%C4%85', |
| 88 | + 'length' => 10, |
| 89 | + 'expected' => 'myslug', |
| 90 | + 'message' => '"myslug%C4%85" should have been truncated to "myslug".', |
| 91 | + ), |
| 92 | + 'URL-encoded accent characters and "length" includes the fifth URL-encoded character' => array( |
| 93 | + 'slug' => 'myslug%C4%85', |
| 94 | + 'length' => 11, |
| 95 | + 'expected' => 'myslug', |
| 96 | + 'message' => '"myslug%C4%85" should have been truncated to "myslug".', |
| 97 | + ), |
| 98 | + 'URL-encoded accent characters and "length" includes the sixth URL-encoded character' => array( |
| 99 | + 'slug' => 'myslug%C4%85', |
| 100 | + 'length' => 12, |
| 101 | + 'expected' => 'myslug%C4%85', |
| 102 | + 'message' => '"myslug%C4%85" should have been truncated to "myslug%C4%85".', |
| 103 | + ), |
| 104 | + ); |
| 105 | + } |
| 106 | +} |
0 commit comments