Commit 1ace239d authored by catch's avatar catch

Issue #2157703 by andypost: Deprecate comment_int_to_alphadecimal() &...

Issue #2157703 by andypost: Deprecate comment_int_to_alphadecimal() & comment_alphadecimal_to_int() in favour of methods in Number component.
parent e948f79c
......@@ -57,4 +57,47 @@ public static function validStep($value, $step, $offset = 0.0) {
return $computed_acceptable_error >= $remainder || $remainder >= ($step - $computed_acceptable_error);
}
/**
* Generates a sorting code from an integer.
*
* Consists of a leading character indicating length, followed by N digits
* with a numerical value in base 36 (alphadecimal). These codes can be sorted
* as strings without altering numerical order.
*
* It goes:
* 00, 01, 02, ..., 0y, 0z,
* 110, 111, ... , 1zy, 1zz,
* 2100, 2101, ..., 2zzy, 2zzz,
* 31000, 31001, ...
*
* @param int $i
* The integer value to convert.
*
* @return string
* The alpha decimal value.
*
* @see \Drupal\Component\Utility\Number::alphadecimalToInt
*/
public static function intToAlphadecimal($i = 0) {
$num = base_convert((int) $i, 10, 36);
$length = strlen($num);
return chr($length + ord('0') - 1) . $num;
}
/**
* Decodes a sorting code back to an integer.
*
* @param string $string
* The alpha decimal value to convert
*
* @return int
* The integer value.
*
* @see \Drupal\Component\Utility\Number::intToAlphadecimal
*/
public static function alphadecimalToInt($string = '00') {
return (int) base_convert(substr($string, 1), 36, 10);
}
}
......@@ -10,6 +10,7 @@
* book page, user etc.
*/
use Drupal\Component\Utility\Number;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityChangedInterface;
use Drupal\comment\CommentInterface;
......@@ -1590,21 +1591,20 @@ function _comment_per_page() {
* 110, 111, ... , 1zy, 1zz,
* 2100, 2101, ..., 2zzy, 2zzz,
* 31000, 31001, ...
*
* @deprecated Use \Drupal\Component\Utility\Number::intToAlphadecimal() instead.
*/
function comment_int_to_alphadecimal($i = 0) {
$num = base_convert((int) $i, 10, 36);
$length = strlen($num);
return chr($length + ord('0') - 1) . $num;
return Number::alphadecimalToInt($i);
}
/**
* Decodes a sorting code back to an integer.
*
* @see comment_int_to_alphadecimal()
* @deprecated Use \Drupal\Component\Utility\Number::alphadecimalToInt() instead.
*/
function comment_alphadecimal_to_int($c = '00') {
return base_convert(substr($c, 1), 36, 10);
return Number::alphadecimalToInt($c);
}
/**
......
......@@ -7,6 +7,7 @@
namespace Drupal\comment\Entity;
use Drupal\Component\Utility\Number;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\comment\CommentInterface;
use Drupal\Core\Entity\EntityStorageControllerInterface;
......@@ -245,7 +246,7 @@ public function preSave(EntityStorageControllerInterface $storage_controller) {
$max = rtrim($max, '/');
// We need to get the value at the correct depth.
$parts = explode('.', $max);
$n = comment_alphadecimal_to_int($parts[0]);
$n = Number::alphadecimalToInt($parts[0]);
$prefix = '';
}
else {
......@@ -272,14 +273,14 @@ public function preSave(EntityStorageControllerInterface $storage_controller) {
// Get the value at the correct depth.
$parts = explode('.', $max);
$parent_depth = count(explode('.', $parent->thread->value));
$n = comment_alphadecimal_to_int($parts[$parent_depth]);
$n = Number::alphadecimalToInt($parts[$parent_depth]);
}
}
// Finally, build the thread field for this new comment. To avoid
// race conditions, get a lock on the thread. If another process already
// has the lock, just move to the next integer.
do {
$thread = $prefix . comment_int_to_alphadecimal(++$n) . '/';
$thread = $prefix . Number::intToAlphadecimal(++$n) . '/';
$lock_name = "comment:{$this->entity_id->value}:$thread";
} while (!\Drupal::lock()->acquire($lock_name));
$this->threadLock = $lock_name;
......
......@@ -37,7 +37,7 @@ public function testLocks() {
$container->register('request', 'Symfony\Component\HttpFoundation\Request');
$lock = $this->getMock('Drupal\Core\Lock\LockBackendInterface');
$cid = 2;
$lock_name = "comment:$cid:./";
$lock_name = "comment:$cid:01.00/";
$lock->expects($this->at(0))
->method('acquire')
->with($lock_name, 30)
......@@ -65,9 +65,10 @@ public function testLocks() {
$comment->status->value = 1;
$comment->entity_id->value = $cid;
$comment->uid->target_id = 3;
// Parent comment is the first in thread.
$comment->pid->target_id = 42;
$comment->pid->entity = new \stdClass();
$comment->pid->entity->thread = (object) array('value' => '');
$comment->pid->entity->thread = (object) array('value' => '01/');
$storage_controller = $this->getMock('Drupal\comment\CommentStorageControllerInterface');
$comment->preSave($storage_controller);
$comment->postSave($storage_controller);
......@@ -85,9 +86,6 @@ protected function tearDown() {
}
}
namespace {
if (!function_exists('comment_int_to_alphadecimal')) {
function comment_int_to_alphadecimal() {}
}
if (!function_exists('module_invoke_all')) {
function module_invoke_all() {}
}
......
<?php
/**
* @file
* Contains \Drupal\Tests\Component\Utility\NumberAlphadecimalTest.
*/
namespace Drupal\Tests\Component\Utility;
use Drupal\Component\Utility\Number;
use Drupal\Tests\UnitTestCase;
/**
* Unit tests for the number alphadecimal functions.
*
* @see \Drupal\Component\Utility\Number
*
* @group Drupal
*/
class NumberAlphadecimalTest extends UnitTestCase {
/**
* {@inheritdoc}
*/
public static function getInfo() {
return array(
'name' => 'Number alphadecimal functions',
'description' => 'Tests alphadecimal number conversion functions.',
'group' => 'Common',
);
}
/**
* Tests the alphadecimal conversion functions.
*
* @param int $value
* The integer value.
* @param string $expected
* The expected alphadecimal value.
*
* @dataProvider providerTestConversions
*/
public function testConversions($value, $expected) {
$this->assertSame(Number::intToAlphadecimal($value), $expected);
$this->assertSame($value, Number::alphadecimalToInt($expected));
}
/**
* Data provider for testConversions().
*
* @see testConversions()
*
* @return array
* An array containing:
* - The integer value.
* - The alphadecimal value.
*/
public function providerTestConversions() {
return array(
array(0, '00'),
array(1, '01'),
array(10, '0a'),
array(20, '0k'),
array(35, '0z'),
array(36, '110'),
array(100, '12s'),
);
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment