diff --git a/core/includes/bootstrap.inc b/core/includes/bootstrap.inc index ac209926e3f1d4eb2d1e8803eead9cc4e1527944..50dc8f38e4b76f5c4a66ebb22bb3cbc30248399a 100644 --- a/core/includes/bootstrap.inc +++ b/core/includes/bootstrap.inc @@ -5,6 +5,7 @@ */ use Drupal\Component\Utility\Crypt; +use Drupal\Component\Utility\Environment; use Drupal\Component\Utility\NestedArray; use Drupal\Component\Utility\String; use Drupal\Component\Utility\Timer; @@ -158,13 +159,6 @@ */ const DRUPAL_AUTHENTICATED_RID = 'authenticated'; -/** - * The number of bytes in a kilobyte. - * - * For more information, visit http://en.wikipedia.org/wiki/Kilobyte. - */ -const DRUPAL_KILOBYTE = 1024; - /** * The maximum number of characters in a module or theme name. */ @@ -2294,18 +2288,12 @@ function _drupal_shutdown_function() { * @return * TRUE if there is sufficient memory to allow the operation, or FALSE * otherwise. + * + * @deprecated in Drupal 8.x-dev, will be removed before Drupal 8.0. + * Use \Drupal\Component\Utility\Environment::checkMemoryLimit(). */ function drupal_check_memory_limit($required, $memory_limit = NULL) { - if (!isset($memory_limit)) { - $memory_limit = ini_get('memory_limit'); - } - - // There is sufficient memory if: - // - No memory limit is set. - // - The memory limit is set to unlimited (-1). - // - The memory limit is greater than or equal to the memory required for - // the operation. - return ((!$memory_limit) || ($memory_limit == -1) || (parse_size($memory_limit) >= parse_size($required))); + return Environment::checkMemoryLimit($required, $memory_limit); } /** diff --git a/core/includes/common.inc b/core/includes/common.inc index f30bcdf1b8dd6e2adb5ab068bde0e85ef98ea21d..65aef092b1c05b93bea1542736340740e283c331 100644 --- a/core/includes/common.inc +++ b/core/includes/common.inc @@ -11,6 +11,7 @@ use Drupal\Component\Serialization\Json; use Drupal\Component\Serialization\Yaml; use Drupal\Component\Serialization\Exception\InvalidDataTypeException; +use Drupal\Component\Utility\Bytes; use Drupal\Component\Utility\Crypt; use Drupal\Component\Utility\Number; use Drupal\Component\Utility\SortArray; @@ -549,17 +550,12 @@ function format_plural($count, $singular, $plural, array $args = array(), array * * @return * An integer representation of the size in bytes. + * + * @deprecated in Drupal 8.x-dev, will be removed before Drupal 8.0. + * Use \Drupal\Component\Utility\Bytes::toInt(). */ function parse_size($size) { - $unit = preg_replace('/[^bkmgtpezy]/i', '', $size); // Remove the non-unit characters from the size. - $size = preg_replace('/[^0-9\.]/', '', $size); // Remove the non-numeric characters from the size. - if ($unit) { - // Find the position of the unit in the ordered string which is the power of magnitude to multiply a kilobyte by. - return round($size * pow(DRUPAL_KILOBYTE, stripos('bkmgtpezy', $unit[0]))); - } - else { - return round($size); - } + return Bytes::toInt($size); } /** @@ -575,11 +571,11 @@ function parse_size($size) { * A translated string representation of the size. */ function format_size($size, $langcode = NULL) { - if ($size < DRUPAL_KILOBYTE) { + if ($size < Bytes::KILOBYTE) { return format_plural($size, '1 byte', '@count bytes', array(), array('langcode' => $langcode)); } else { - $size = $size / DRUPAL_KILOBYTE; // Convert bytes to kilobytes. + $size = $size / Bytes::KILOBYTE; // Convert bytes to kilobytes. $units = array( t('@size KB', array(), array('langcode' => $langcode)), t('@size MB', array(), array('langcode' => $langcode)), @@ -591,8 +587,8 @@ function format_size($size, $langcode = NULL) { t('@size YB', array(), array('langcode' => $langcode)), ); foreach ($units as $unit) { - if (round($size, 2) >= DRUPAL_KILOBYTE) { - $size = $size / DRUPAL_KILOBYTE; + if (round($size, 2) >= Bytes::KILOBYTE) { + $size = $size / Bytes::KILOBYTE; } else { break; diff --git a/core/includes/file.inc b/core/includes/file.inc index fc0b72a8bc5fabb48984c57c99ccb2c280336c93..02bd1078b16acb9a544c2518eec5b643b153af84 100644 --- a/core/includes/file.inc +++ b/core/includes/file.inc @@ -8,6 +8,7 @@ use Drupal\Component\Utility\UrlHelper; use Drupal\Core\StreamWrapper\LocalStream; use Drupal\Component\PhpStorage\FileStorage; +use Drupal\Component\Utility\Bytes; use Drupal\Component\Utility\String; use Drupal\Core\Site\Settings; use Drupal\Core\StreamWrapper\PublicStream; @@ -1271,11 +1272,11 @@ function file_upload_max_size() { if ($max_size < 0) { // Start with post_max_size. - $max_size = parse_size(ini_get('post_max_size')); + $max_size = Bytes::toInt(ini_get('post_max_size')); // If upload_max_size is less, then reduce. Except if upload_max_size is // zero, which indicates no limit. - $upload_max = parse_size(ini_get('upload_max_filesize')); + $upload_max = Bytes::toInt(ini_get('upload_max_filesize')); if ($upload_max > 0 && $upload_max < $max_size) { $max_size = $upload_max; } diff --git a/core/lib/Drupal/Component/Utility/Bytes.php b/core/lib/Drupal/Component/Utility/Bytes.php new file mode 100644 index 0000000000000000000000000000000000000000..0bcac31ed3f0534490a1be570c6fc492c57aab03 --- /dev/null +++ b/core/lib/Drupal/Component/Utility/Bytes.php @@ -0,0 +1,47 @@ +<?php + +/** + * @file + * Contains \Drupal\Component\Utility\Bytes. + */ + +namespace Drupal\Component\Utility; + +/** + * Provides helper methods for byte conversions. + */ +class Bytes { + + /** + * The number of bytes in a kilobyte. + * + * @see http://en.wikipedia.org/wiki/Kilobyte + */ + const KILOBYTE = 1024; + + /** + * Parses a given byte size. + * + * @param mixed $size + * An integer or string size expressed as a number of bytes with optional SI + * or IEC binary unit prefix (e.g. 2, 3K, 5MB, 10G, 6GiB, 8 bytes, 9mbytes). + * + * @return int + * An integer representation of the size in bytes. + */ + public static function toInt($size) { + // Remove the non-unit characters from the size. + $unit = preg_replace('/[^bkmgtpezy]/i', '', $size); + // Remove the non-numeric characters from the size. + $size = preg_replace('/[^0-9\.]/', '', $size); + if ($unit) { + // Find the position of the unit in the ordered string which is the power + // of magnitude to multiply a kilobyte by. + return round($size * pow(self::KILOBYTE, stripos('bkmgtpezy', $unit[0]))); + } + else { + return round($size); + } + } + +} diff --git a/core/lib/Drupal/Component/Utility/Environment.php b/core/lib/Drupal/Component/Utility/Environment.php new file mode 100644 index 0000000000000000000000000000000000000000..087bcadbd1d49a3d1b29904cbbe43d71052f1bf1 --- /dev/null +++ b/core/lib/Drupal/Component/Utility/Environment.php @@ -0,0 +1,45 @@ +<?php + +/** + * @file + * Contains \Drupal\Component\Utility\Environment. + */ + +namespace Drupal\Component\Utility; + +/** + * Provides PHP environment helper methods. + */ +class Environment { + + /** + * Compares the memory required for an operation to the available memory. + * + * @param string $required + * The memory required for the operation, expressed as a number of bytes with + * optional SI or IEC binary unit prefix (e.g. 2, 3K, 5MB, 10G, 6GiB, 8bytes, + * 9mbytes). + * @param $memory_limit + * (optional) The memory limit for the operation, expressed as a number of + * bytes with optional SI or IEC binary unit prefix (e.g. 2, 3K, 5MB, 10G, + * 6GiB, 8bytes, 9mbytes). If no value is passed, the current PHP + * memory_limit will be used. Defaults to NULL. + * + * @return bool + * TRUE if there is sufficient memory to allow the operation, or FALSE + * otherwise. + */ + public static function checkMemoryLimit($required, $memory_limit = NULL) { + if (!isset($memory_limit)) { + $memory_limit = ini_get('memory_limit'); + } + + // There is sufficient memory if: + // - No memory limit is set. + // - The memory limit is set to unlimited (-1). + // - The memory limit is greater than or equal to the memory required for + // the operation. + return ((!$memory_limit) || ($memory_limit == -1) || (Bytes::toInt($memory_limit) >= Bytes::toInt($required))); + } + +} diff --git a/core/modules/color/color.module b/core/modules/color/color.module index 6ecf036bfdc1169bee8720b88ce7f04c1edfc23c..335e0ade97ed8bee78140674f61d49e1cf023402 100644 --- a/core/modules/color/color.module +++ b/core/modules/color/color.module @@ -5,6 +5,8 @@ */ use Drupal\Core\Asset\CssOptimizer; +use Drupal\Component\Utility\Bytes; +use Drupal\Component\Utility\Environment; use Drupal\Component\Utility\String; use Symfony\Component\HttpFoundation\Request; @@ -378,8 +380,8 @@ function color_scheme_form_submit($form, &$form_state) { // scheme change based on a faulty memory calculation. $usage = memory_get_usage(TRUE); $memory_limit = ini_get('memory_limit'); - $size = parse_size($memory_limit); - if (!drupal_check_memory_limit($usage + $required, $memory_limit)) { + $size = Bytes::toInt($memory_limit); + if (!Environment::checkMemoryLimit($usage + $required, $memory_limit)) { drupal_set_message(t('There is not enough memory available to PHP to change this theme\'s color scheme. You need at least %size more. Check the <a href="@url">PHP documentation</a> for more information.', array('%size' => format_size($usage + $required - $size), '@url' => 'http://www.php.net/manual/ini.core.php#ini.sect.resource-limits')), 'error'); return; } diff --git a/core/modules/editor/lib/Drupal/editor/Form/EditorImageDialog.php b/core/modules/editor/lib/Drupal/editor/Form/EditorImageDialog.php index 7b7a4d16fa4f28a3af2a4106170bb27daf2cdead..ab0708b499d697e515b2e51ade45a2c4a4dc4fc0 100644 --- a/core/modules/editor/lib/Drupal/editor/Form/EditorImageDialog.php +++ b/core/modules/editor/lib/Drupal/editor/Form/EditorImageDialog.php @@ -7,6 +7,7 @@ namespace Drupal\editor\Form; +use Drupal\Component\Utility\Bytes; use Drupal\Core\Form\FormBase; use Drupal\filter\Entity\FilterFormat; use Drupal\Core\Ajax\AjaxResponse; @@ -55,7 +56,7 @@ public function buildForm(array $form, array &$form_state, FilterFormat $filter_ else { $max_dimensions = 0; } - $max_filesize = min(parse_size($image_upload['max_size']), file_upload_max_size()); + $max_filesize = min(Bytes::toInt($image_upload['max_size']), file_upload_max_size()); $existing_file = isset($image_element['data-editor-file-uuid']) ? entity_load_by_uuid('file', $image_element['data-editor-file-uuid']) : NULL; $fid = $existing_file ? $existing_file->id() : NULL; diff --git a/core/modules/file/lib/Drupal/file/Plugin/Field/FieldType/FileItem.php b/core/modules/file/lib/Drupal/file/Plugin/Field/FieldType/FileItem.php index 16ada1e1f58db1c49ab877225049ef427eebb316..27ce2abf4a5742a12ede3743575831a63ebba9c0 100644 --- a/core/modules/file/lib/Drupal/file/Plugin/Field/FieldType/FileItem.php +++ b/core/modules/file/lib/Drupal/file/Plugin/Field/FieldType/FileItem.php @@ -7,6 +7,7 @@ namespace Drupal\file\Plugin\Field\FieldType; +use Drupal\Component\Utility\Bytes; use Drupal\Core\Field\FieldStorageDefinitionInterface; use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem; use Drupal\Core\TypedData\DataDefinition; @@ -240,13 +241,13 @@ public static function validateExtensions($element, &$form_state) { * Form API callback. * * Ensures that a size has been entered and that it can be parsed by - * parse_size(). + * \Drupal\Component\Utility\Bytes::toInt(). * * This function is assigned as an #element_validate callback in * instanceSettingsForm(). */ public static function validateMaxFilesize($element, &$form_state) { - if (!empty($element['#value']) && !is_numeric(parse_size($element['#value']))) { + if (!empty($element['#value']) && !is_numeric(Bytes::toInt($element['#value']))) { form_error($element, $form_state, t('The "!name" option must contain a valid value. You may either leave the text field empty or enter a string like "512" (bytes), "80 KB" (kilobytes) or "50 MB" (megabytes).', array('!name' => t($element['title'])))); } } @@ -284,9 +285,9 @@ public function getUploadValidators() { $settings = $this->getSettings(); // Cap the upload size according to the PHP limit. - $max_filesize = parse_size(file_upload_max_size()); + $max_filesize = Bytes::toInt(file_upload_max_size()); if (!empty($settings['max_filesize'])) { - $max_filesize = min($max_filesize, parse_size($settings['max_filesize'])); + $max_filesize = min($max_filesize, Bytes::toInt($settings['max_filesize'])); } // There is always a file size limit due to the PHP server limit. diff --git a/core/modules/simpletest/simpletest.install b/core/modules/simpletest/simpletest.install index 82e1ebbb2ad5b48f7c031ea7db33673b3326b34d..3cb12172bf3b5a04203c885c8212091c15725213 100644 --- a/core/modules/simpletest/simpletest.install +++ b/core/modules/simpletest/simpletest.install @@ -5,6 +5,8 @@ * Install, update and uninstall functions for the simpletest module. */ +use Drupal\Component\Utility\Environment; + /** * Minimum value of PHP memory_limit for SimpleTest. */ @@ -53,7 +55,7 @@ function simpletest_requirements($phase) { // Check the current memory limit. If it is set too low, SimpleTest will fail // to load all tests and throw a fatal error. $memory_limit = ini_get('memory_limit'); - if (!drupal_check_memory_limit(SIMPLETEST_MINIMUM_PHP_MEMORY_LIMIT, $memory_limit)) { + if (!Environment::checkMemoryLimit(SIMPLETEST_MINIMUM_PHP_MEMORY_LIMIT, $memory_limit)) { $requirements['php_memory_limit']['severity'] = REQUIREMENT_ERROR; $requirements['php_memory_limit']['description'] = t('The testing framework requires the PHP memory limit to be at least %memory_minimum_limit. The current value is %memory_limit. <a href="@url">Follow these steps to continue</a>.', array('%memory_limit' => $memory_limit, '%memory_minimum_limit' => SIMPLETEST_MINIMUM_PHP_MEMORY_LIMIT, '@url' => 'http://drupal.org/node/207036')); } diff --git a/core/modules/system/lib/Drupal/system/Tests/Bootstrap/MiscUnitTest.php b/core/modules/system/lib/Drupal/system/Tests/Bootstrap/MiscUnitTest.php deleted file mode 100644 index f94c419f0f2fd1307388644ebfd80c541b3d17a9..0000000000000000000000000000000000000000 --- a/core/modules/system/lib/Drupal/system/Tests/Bootstrap/MiscUnitTest.php +++ /dev/null @@ -1,46 +0,0 @@ -<?php - -/** - * @file - * Definition of Drupal\system\Tests\Bootstrap\MiscUnitTest. - */ - -namespace Drupal\system\Tests\Bootstrap; - -use Drupal\simpletest\UnitTestBase; - -/** - * Tests miscellaneous functions in bootstrap.inc. - */ -class MiscUnitTest extends UnitTestBase { - - public static function getInfo() { - return array( - 'name' => 'Miscellaneous bootstrap unit tests', - 'description' => 'Test miscellaneous functions in bootstrap.inc.', - 'group' => 'Bootstrap', - ); - } - - /** - * Tests that the drupal_check_memory_limit() function works as expected. - */ - function testCheckMemoryLimit() { - $memory_limit = ini_get('memory_limit'); - // Test that a very reasonable amount of memory is available. - $this->assertTrue(drupal_check_memory_limit('30MB'), '30MB of memory tested available.'); - - // Get the available memory and multiply it by two to make it unreasonably - // high. - $twice_avail_memory = ($memory_limit * 2) . 'MB'; - // The function should always return true if the memory limit is set to -1. - $this->assertTrue(drupal_check_memory_limit($twice_avail_memory, -1), 'drupal_check_memory_limit() returns TRUE when a limit of -1 (none) is supplied'); - - // Test that even though we have 30MB of memory available - the function - // returns FALSE when given an upper limit for how much memory can be used. - $this->assertFalse(drupal_check_memory_limit('30MB', '16MB'), 'drupal_check_memory_limit() returns FALSE with a 16MB upper limit on a 30MB requirement.'); - - // Test that an equal amount of memory to the amount requested returns TRUE. - $this->assertTrue(drupal_check_memory_limit('30MB', '30MB'), 'drupal_check_memory_limit() returns TRUE when requesting 30MB on a 30MB requirement.'); - } -} diff --git a/core/modules/system/lib/Drupal/system/Tests/Common/SizeUnitTest.php b/core/modules/system/lib/Drupal/system/Tests/Common/SizeUnitTest.php index a8a2662672adf5148e61e7ffa984d2e8a883e9f7..c170fc4ec3b0f17c3d493a330d8c16d89f2c5068 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Common/SizeUnitTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Common/SizeUnitTest.php @@ -7,6 +7,7 @@ namespace Drupal\system\Tests\Common; +use Drupal\Component\Utility\Bytes; use Drupal\simpletest\UnitTestBase; /** @@ -25,7 +26,7 @@ public static function getInfo() { } function setUp() { - $kb = DRUPAL_KILOBYTE; + $kb = Bytes::KILOBYTE; $this->exact_test_cases = array( '1 byte' => 1, '1 KB' => $kb, @@ -63,46 +64,13 @@ function testCommonFormatSize() { } /** - * Checks that parse_size() returns the proper byte sizes. - */ - function testCommonParseSize() { - foreach ($this->exact_test_cases as $string => $size) { - $this->assertEqual( - $parsed_size = parse_size($string), - $size, - $size . ' == ' . $parsed_size . ' (' . $string . ')' - ); - } - - // Some custom parsing tests - $string = '23476892 bytes'; - $this->assertEqual( - ($parsed_size = parse_size($string)), - $size = 23476892, - $string . ' == ' . $parsed_size . ' bytes' - ); - $string = '76MRandomStringThatShouldBeIgnoredByParseSize.'; // 76 MB - $this->assertEqual( - $parsed_size = parse_size($string), - $size = 79691776, - $string . ' == ' . $parsed_size . ' bytes' - ); - $string = '76.24 Giggabyte'; // Misspeld text -> 76.24 GB - $this->assertEqual( - $parsed_size = parse_size($string), - $size = 81862076662, - $string . ' == ' . $parsed_size . ' bytes' - ); - } - - /** - * Cross-tests parse_size() and format_size(). + * Cross-tests Bytes::toInt() and format_size(). */ function testCommonParseSizeFormatSize() { foreach ($this->exact_test_cases as $size) { $this->assertEqual( $size, - ($parsed_size = parse_size($string = format_size($size, NULL))), + ($parsed_size = Bytes::toInt($string = format_size($size, NULL))), $size . ' == ' . $parsed_size . ' (' . $string . ')' ); } diff --git a/core/modules/system/system.install b/core/modules/system/system.install index a14cd7b35a0b389e6e43effd61d8f42981ec6681..6161819950d4a821f33d28caa63f7448a180c97b 100644 --- a/core/modules/system/system.install +++ b/core/modules/system/system.install @@ -6,6 +6,7 @@ */ use Drupal\Component\Utility\Crypt; +use Drupal\Component\Utility\Environment; use Drupal\Core\Database\Database; use Drupal\Core\Language\Language; use Drupal\Core\Site\Settings; @@ -183,7 +184,7 @@ function system_requirements($phase) { 'value' => $memory_limit == -1 ? t('-1 (Unlimited)') : $memory_limit, ); - if (!drupal_check_memory_limit(DRUPAL_MINIMUM_PHP_MEMORY_LIMIT, $memory_limit)) { + if (!Environment::checkMemoryLimit(DRUPAL_MINIMUM_PHP_MEMORY_LIMIT, $memory_limit)) { $description = ''; if ($phase == 'install') { $description = t('Consider increasing your PHP memory limit to %memory_minimum_limit to help prevent errors in the installation process.', array('%memory_minimum_limit' => DRUPAL_MINIMUM_PHP_MEMORY_LIMIT)); diff --git a/core/tests/Drupal/Tests/Component/Utility/BytesTest.php b/core/tests/Drupal/Tests/Component/Utility/BytesTest.php new file mode 100644 index 0000000000000000000000000000000000000000..34f0be2f88a60e36659ca9f1713ab84448bd4ef6 --- /dev/null +++ b/core/tests/Drupal/Tests/Component/Utility/BytesTest.php @@ -0,0 +1,75 @@ +<?php + +/** + * @file + * Contains \Drupal\Tests\Component\Utility\BytesTest. + */ + +namespace Drupal\Tests\Component\Utility; + +use Drupal\Component\Utility\Bytes; +use Drupal\Tests\UnitTestCase; + +/** + * Tests bytes size parsing helper methods. + * + * @group Drupal + * @group Utility + * @coversDefaultClass \Drupal\Component\Utility\Bytes + */ +class BytesTest extends UnitTestCase { + + /** + * {@inheritdoc} + */ + public static function getInfo() { + return array( + 'name' => 'Bytes utility helpers', + 'description' => '', + 'group' => 'Utility', + ); + } + + /** + * Tests \Drupal\Component\Utility\Bytes::toInt(). + * + * @param int $size + * The value for the size argument for + * \Drupal\Component\Utility\Bytes::toInt(). + * @param int $expected_int + * The expected return value from + * \Drupal\Component\Utility\Bytes::toInt(). + * + * @dataProvider providerTestToInt + * @covers ::toInt + */ + public function testToInt($size, $expected_int) { + $this->assertEquals($expected_int, Bytes::toInt($size)); + } + + /** + * Provides data for testToInt. + * + * @return array + * An array of arrays, each containing the argument for + * \Drupal\Component\Utility\Bytes::toInt(): size, and the expected return + * value. + */ + public function providerTestToInt() { + return array( + array('1 byte', 1), + array('1 KB' , Bytes::KILOBYTE), + array('1 MB' , pow(Bytes::KILOBYTE, 2)), + array('1 GB' , pow(Bytes::KILOBYTE, 3)), + array('1 TB' , pow(Bytes::KILOBYTE, 4)), + array('1 PB' , pow(Bytes::KILOBYTE, 5)), + array('1 EB' , pow(Bytes::KILOBYTE, 6)), + array('1 ZB' , pow(Bytes::KILOBYTE, 7)), + array('1 YB' , pow(Bytes::KILOBYTE, 8)), + array('23476892 bytes', 23476892), + array('76MRandomStringThatShouldBeIgnoredByParseSize.', 79691776), // 76 MB + array('76.24 Giggabyte', 81862076662), // 76.24 GB (with typo) + ); + } + +} diff --git a/core/tests/Drupal/Tests/Component/Utility/EnvironmentTest.php b/core/tests/Drupal/Tests/Component/Utility/EnvironmentTest.php new file mode 100644 index 0000000000000000000000000000000000000000..158278d787cfc5968d09aa1bc72dfe74db2f340f --- /dev/null +++ b/core/tests/Drupal/Tests/Component/Utility/EnvironmentTest.php @@ -0,0 +1,78 @@ +<?php + +/** + * @file + * Contains \Drupal\Tests\Component\Utility\EnvironmentTest. + */ + +namespace Drupal\Tests\Component\Utility; + +use Drupal\Component\Utility\Environment; +use Drupal\Tests\UnitTestCase; + +/** + * Tests PHP environment helper class. + * + * @group Drupal + * @group Utility + * @coversDefaultClass \Drupal\Component\Utility\Environment + */ +class EnvironmentTest extends UnitTestCase { + + /** + * {@inheritdoc} + */ + public static function getInfo() { + return array( + 'name' => 'PHP environment utility helpers', + 'description' => '', + 'group' => 'Utility', + ); + } + + /** + * Tests \Drupal\Component\Utility\Environment::checkMemoryLimit(). + * + * @param string $required + * The required memory argument for + * \Drupal\Component\Utility\Environment::checkMemoryLimit(). + * @param string $custom_memory_limit + * The custom memory limit argument for + * \Drupal\Component\Utility\Environment::checkMemoryLimit(). + * @param bool $expected + * The expected return value from + * \Drupal\Component\Utility\Environment::checkMemoryLimit(). + * + * @dataProvider providerTestCheckMemoryLimit + * @covers ::checkMemoryLimit + */ + public function testCheckMemoryLimit($required, $custom_memory_limit, $expected) { + $actual = Environment::checkMemoryLimit($required, $custom_memory_limit); + $this->assertEquals($expected, $actual); + } + + /** + * Provides data for testCheckMemoryLimit(). + * + * @return array + * An array of arrays, each containing the arguments for + * \Drupal\Component\Utility\Environment::checkMemoryLimit(): + * required and memory_limit, and the expected return value. + */ + public function providerTestCheckMemoryLimit() { + $memory_limit = ini_get('memory_limit'); + $twice_avail_memory = ($memory_limit * 2) . 'MB'; + + return array( + // Minimal amount of memory should be available. + array('30MB', NULL, TRUE), + // Exceed a custom (unlimited) memory limit. + array($twice_avail_memory, -1, TRUE), + // Exceed a custom memory limit. + array('30MB', '16MB', FALSE), + // Available = required. + array('30MB', '30MB', TRUE), + ); + } + +}