From cf4724bc966468ce740c28d78e9c6f5d11a64b46 Mon Sep 17 00:00:00 2001 From: LifeOrYou Date: Sat, 23 Dec 2017 12:11:25 +0100 Subject: [PATCH 1/5] Fix #11568 --- src/Utility/Text.php | 36 ++++++++++++++++++++++++++++- tests/TestCase/Utility/TextTest.php | 5 ++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/Utility/Text.php b/src/Utility/Text.php index 23770a3d468..264d2c9416b 100644 --- a/src/Utility/Text.php +++ b/src/Utility/Text.php @@ -29,6 +29,16 @@ class Text */ protected static $_defaultTransliteratorId = 'Any-Latin; Latin-ASCII; [\u0080-\u7fff] remove'; + /** + * Default html tags who must not be count for truncate text. + * + * @var array + */ + protected static $_defaultHtmlNoCount = [ + 'style', + 'script' + ]; + /** * Generate a random UUID version 4 * @@ -608,7 +618,10 @@ public static function truncate($text, $length = 100, array $options = []) preg_match_all('/(<\/?([\w+]+)[^>]*>)?([^<>]*)/', $text, $tags, PREG_SET_ORDER); foreach ($tags as $tag) { - $contentLength = self::_strlen($tag[3], $options); + $contentLength = 0; + if (!in_array($tag[2], static::$_defaultHtmlNoCount)) { + $contentLength = self::_strlen($tag[3], $options); + } if ($truncate === '') { if (!preg_match('/img|br|input|hr|area|base|basefont|col|frame|isindex|link|meta|param/i', $tag[2])) { @@ -1120,4 +1133,25 @@ public static function slug($string, $options = []) return $string; } + + /** + * Get default html tags not counted for truncate. + * + * @return array Html tags. + */ + public static function getHtmlNoCount() + { + return static::$_defaultHtmlNoCount; + } + + /** + * Set html tags not counted for truncate. + * + * @param array $htmlNoCount New html tags not counted for truncate. + * @return void + */ + public static function setHtmlNoCount($htmlNoCount) + { + static::$_defaultHtmlNoCount = $htmlNoCount; + } } diff --git a/tests/TestCase/Utility/TextTest.php b/tests/TestCase/Utility/TextTest.php index 98d6ceb2725..83d5ae1a2bf 100644 --- a/tests/TestCase/Utility/TextTest.php +++ b/tests/TestCase/Utility/TextTest.php @@ -592,6 +592,11 @@ public function testTruncate() ]); $expected = '

Iamatestwi...

'; $this->assertEquals($expected, $result); + + $text = '

The quick brown fox jumps over the lazy dog

'; + $expected = '

The qu...

'; + $result = $this->Text->truncate($text, 9, ['html' => true, 'ellipsis' => '...']); + $this->assertSame($expected, $result); } /** From 807db8f228fb8372c8d5698bd06b711e421033be Mon Sep 17 00:00:00 2001 From: dereuromark Date: Mon, 25 Dec 2017 00:26:41 +0100 Subject: [PATCH 2/5] Fix non interactive to only overwrite with designed force flag enabled. --- src/Console/Shell.php | 9 +- tests/TestCase/Console/ShellTest.php | 113 +++++++----------- tests/TestCase/Shell/CommandListShellTest.php | 4 +- tests/TestCase/Shell/CompletionShellTest.php | 2 +- tests/test_app/TestApp/Shell/MergeShell.php | 36 ++++++ .../test_app/TestApp/Shell/ShellTestShell.php | 77 ++++++++++++ 6 files changed, 166 insertions(+), 75 deletions(-) create mode 100644 tests/test_app/TestApp/Shell/MergeShell.php create mode 100644 tests/test_app/TestApp/Shell/ShellTestShell.php diff --git a/src/Console/Shell.php b/src/Console/Shell.php index 4a9d07efcbd..5d6d6513c01 100644 --- a/src/Console/Shell.php +++ b/src/Console/Shell.php @@ -879,7 +879,14 @@ public function createFile($path, $contents) $this->_io->out(); - if (is_file($path) && empty($this->params['force']) && $this->interactive) { + $fileExists = is_file($path); + if ($fileExists && empty($this->params['force']) && !$this->interactive) { + $this->_io->out('File exists, skipping.'); + + return false; + } + + if ($fileExists && $this->interactive && empty($this->params['force'])) { $this->_io->out(sprintf('File `%s` exists', $path)); $key = $this->_io->askChoice('Do you want to overwrite?', ['y', 'n', 'a', 'q'], 'n'); diff --git a/tests/TestCase/Console/ShellTest.php b/tests/TestCase/Console/ShellTest.php index 16ca9ada558..3d3e789872c 100644 --- a/tests/TestCase/Console/ShellTest.php +++ b/tests/TestCase/Console/ShellTest.php @@ -20,77 +20,10 @@ use Cake\Core\Plugin; use Cake\Filesystem\Folder; use Cake\TestSuite\TestCase; +use TestApp\Shell\MergeShell; +use TestApp\Shell\ShellTestShell; use TestApp\Shell\TestingDispatchShell; -/** - * for testing merging vars - */ -class MergeShell extends Shell -{ - - public $tasks = ['DbConfig', 'Fixture']; - - public $modelClass = 'Articles'; -} - -/** - * ShellTestShell class - */ -class ShellTestShell extends Shell -{ - - /** - * name property - * - * @var string - */ - public $name = 'ShellTestShell'; - - /** - * stopped property - * - * @var int - */ - public $stopped; - - /** - * testMessage property - * - * @var string - */ - public $testMessage = 'all your base are belong to us'; - - /** - * stop method - * - * @param int $status - * @return void - */ - protected function _stop($status = Shell::CODE_SUCCESS) - { - $this->stopped = $status; - } - - protected function _secret() - { - } - - //@codingStandardsIgnoreStart - public function doSomething() - { - } - - protected function noAccess() - { - } - - public function logSomething() - { - $this->log($this->testMessage); - } - //@codingStandardsIgnoreEnd -} - /** * TestAppleTask class */ @@ -129,6 +62,14 @@ class ShellTest extends TestCase 'core.users' ]; + /** @var \Cake\Console\Shell */ + protected $Shell; + + /** + * @var \Cake\Console\ConsoleIo|\PHPUnit\Framework\MockObject\MockObject + */ + protected $io; + /** * setUp method * @@ -558,12 +499,38 @@ public function testCreateFileNonInteractive() new Folder($path, true); $contents = "Shell->interactive = false; $result = $this->Shell->createFile($file, $contents); $this->assertTrue($result); $this->assertFileExists($file); $this->assertEquals(file_get_contents($file), $contents); } + /** + * Test that while in non interactive mode it will not overwrite files by default. + * + * @return void + */ + public function testCreateFileNonInteractiveFileExists() + { + $eol = PHP_EOL; + $path = TMP . 'shell_test'; + $file = $path . DS . 'file1.php'; + if (!is_dir($path)) { + mkdir($path, 0770, true); + } + touch($file); + $this->assertFileExists($file); + + new Folder($path, true); + + $contents = "Shell->interactive = false; + $result = $this->Shell->createFile($file, $contents); + $this->assertFalse($result); + } + /** * Test that files are not changed with a 'n' reply. * @@ -619,7 +586,8 @@ public function testCreateFileOverwrite() } /** - * Test that there is no user prompt in non-interactive mode while file already exists. + * Test that there is no user prompt in non-interactive mode while file already exists + * and if force mode is explicitly enabled. * * @return void */ @@ -635,6 +603,7 @@ public function testCreateFileOverwriteNonInteractive() $this->io->expects($this->never())->method('askChoice'); + $this->Shell->params['force'] = true; $this->Shell->interactive = false; $result = $this->Shell->createFile($file, 'My content'); $this->assertTrue($result); @@ -1017,6 +986,7 @@ public function testRunCommandWithMethodInSubcommands() */ public function testRunCommandWithMissingMethodInSubcommands() { + /** @var \Cake\Console\ConsoleOptionParser|\PHPUnit\Framework\MockObject\MockObject $parser */ $parser = $this->getMockBuilder('Cake\Console\ConsoleOptionParser') ->setMethods(['help']) ->setConstructorArgs(['knife']) @@ -1024,6 +994,7 @@ public function testRunCommandWithMissingMethodInSubcommands() $parser->addSubCommand('slice'); $io = $this->getMockBuilder('Cake\Console\ConsoleIo')->getMock(); + /** @var \Cake\Console\Shell|\PHPUnit\Framework\MockObject\MockObject $shell */ $shell = $this->getMockBuilder('Cake\Console\Shell') ->setMethods(['getOptionParser', 'startup']) ->setConstructorArgs([$io]) @@ -1383,7 +1354,7 @@ public function testQuietLog() ->method('setLoggers') ->with(ConsoleIo::QUIET); - $this->Shell = $this->getMockBuilder(__NAMESPACE__ . '\ShellTestShell') + $this->Shell = $this->getMockBuilder(ShellTestShell::class) ->setMethods(['welcome']) ->setConstructorArgs([$io]) ->getMock(); diff --git a/tests/TestCase/Shell/CommandListShellTest.php b/tests/TestCase/Shell/CommandListShellTest.php index 26bae974b6e..89627e33d09 100644 --- a/tests/TestCase/Shell/CommandListShellTest.php +++ b/tests/TestCase/Shell/CommandListShellTest.php @@ -65,7 +65,7 @@ public function testMain() $expected = "/\[.*CORE.*\] cache, help, i18n, orm_cache, plugin, routes, server/"; $this->assertOutputRegExp($expected); - $expected = "/\[.*app.*\] i18m, integration, sample/"; + $expected = "/\[.*app.*\] i18m, integration, merge, sample/"; $this->assertOutputRegExp($expected); $this->assertExitCode(Shell::CODE_SUCCESS); $this->assertErrorEmpty(); @@ -86,7 +86,7 @@ public function testMainAppPriority() $expected = "/\[.*CORE.*\] cache, help, orm_cache, plugin, routes, server/"; $this->assertOutputRegExp($expected); - $expected = "/\[.*app.*\] i18n, integration, sample/"; + $expected = "/\[.*app.*\] i18n, integration, merge, sample/"; $this->assertOutputRegExp($expected); $this->assertExitCode(Shell::CODE_SUCCESS); $this->assertErrorEmpty(); diff --git a/tests/TestCase/Shell/CompletionShellTest.php b/tests/TestCase/Shell/CompletionShellTest.php index 40d460c52d1..764f5c334d0 100644 --- a/tests/TestCase/Shell/CompletionShellTest.php +++ b/tests/TestCase/Shell/CompletionShellTest.php @@ -116,7 +116,7 @@ public function testCommands() $output = $this->out->output; $expected = 'TestPlugin.example TestPlugin.sample TestPluginTwo.example unique welcome ' . - "cache help i18n orm_cache plugin routes server version i18m integration sample testing_dispatch\n"; + "cache help i18n orm_cache plugin routes server version i18m integration merge sample test testing_dispatch\n"; $this->assertTextEquals($expected, $output); } diff --git a/tests/test_app/TestApp/Shell/MergeShell.php b/tests/test_app/TestApp/Shell/MergeShell.php new file mode 100644 index 00000000000..0e2dd26c970 --- /dev/null +++ b/tests/test_app/TestApp/Shell/MergeShell.php @@ -0,0 +1,36 @@ +stopped = $status; + } + + protected function _secret() + { + } + + //@codingStandardsIgnoreStart + public function doSomething() + { + } + + protected function noAccess() + { + } + + public function logSomething() + { + $this->log($this->testMessage); + } + //@codingStandardsIgnoreEnd +} From 28075d82ec61fa3da242cdf2e3c9e9b6984eeed3 Mon Sep 17 00:00:00 2001 From: nojimage Date: Wed, 27 Dec 2017 15:23:37 +0900 Subject: [PATCH 3/5] load phpunit aliases in tests/bootstrap.php --- tests/bootstrap.php | 7 +------ tests/phpunit_aliases.php | 6 +++++- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/tests/bootstrap.php b/tests/bootstrap.php index db12ef987e3..100c15aa1cc 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -133,9 +133,4 @@ ini_set('intl.default_locale', 'en_US'); ini_set('session.gc_divisor', '1'); -if (class_exists('PHPUnit_Runner_Version')) { - class_alias('PHPUnit_Framework_TestResult', 'PHPUnit\Framework\TestResult'); - class_alias('PHPUnit_Framework_Error', 'PHPUnit\Framework\Error\Error'); - class_alias('PHPUnit_Framework_Error_Warning', 'PHPUnit\Framework\Error\Warning'); - class_alias('PHPUnit_Framework_ExpectationFailedException', 'PHPUnit\Framework\ExpectationFailedException'); -} +loadPHPUnitAliases(); diff --git a/tests/phpunit_aliases.php b/tests/phpunit_aliases.php index 87f22c09630..7063d12bbef 100644 --- a/tests/phpunit_aliases.php +++ b/tests/phpunit_aliases.php @@ -1,5 +1,5 @@ Date: Thu, 28 Dec 2017 09:12:52 +0100 Subject: [PATCH 4/5] Remove useless methods and make in_array typehint --- src/Utility/Text.php | 23 +---------------------- 1 file changed, 1 insertion(+), 22 deletions(-) diff --git a/src/Utility/Text.php b/src/Utility/Text.php index 264d2c9416b..ccee6980d6a 100644 --- a/src/Utility/Text.php +++ b/src/Utility/Text.php @@ -619,7 +619,7 @@ public static function truncate($text, $length = 100, array $options = []) preg_match_all('/(<\/?([\w+]+)[^>]*>)?([^<>]*)/', $text, $tags, PREG_SET_ORDER); foreach ($tags as $tag) { $contentLength = 0; - if (!in_array($tag[2], static::$_defaultHtmlNoCount)) { + if (!in_array($tag[2], static::$_defaultHtmlNoCount, true)) { $contentLength = self::_strlen($tag[3], $options); } @@ -1133,25 +1133,4 @@ public static function slug($string, $options = []) return $string; } - - /** - * Get default html tags not counted for truncate. - * - * @return array Html tags. - */ - public static function getHtmlNoCount() - { - return static::$_defaultHtmlNoCount; - } - - /** - * Set html tags not counted for truncate. - * - * @param array $htmlNoCount New html tags not counted for truncate. - * @return void - */ - public static function setHtmlNoCount($htmlNoCount) - { - static::$_defaultHtmlNoCount = $htmlNoCount; - } } From 288a1e2c86319a6e51fdeca0d086b95be1853b11 Mon Sep 17 00:00:00 2001 From: Mark Story Date: Thu, 28 Dec 2017 20:44:26 -0500 Subject: [PATCH 5/5] Update version number to 3.5.10 --- VERSION.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/VERSION.txt b/VERSION.txt index f9a26400023..eafe95472b6 100644 --- a/VERSION.txt +++ b/VERSION.txt @@ -16,4 +16,4 @@ // @license https://opensource.org/licenses/mit-license.php MIT License // +--------------------------------------------------------------------------------------------+ // //////////////////////////////////////////////////////////////////////////////////////////////////// -3.5.9 +3.5.10