Merge "Avoid deprecated LinkCache::singleton()"
[lhc/web/wiklou.git] / tests / phpunit / includes / db / LBFactoryTest.php
index 0395bff..82ca66a 100644 (file)
@@ -393,6 +393,8 @@ class LBFactoryTest extends MediaWikiTestCase {
                $cp->shutdown( null, 'sync', $cpIndex );
 
                $this->assertEquals( null, $cpIndex, "CP write index retained" );
+
+               $this->assertEquals( '45e93a9c215c031d38b7c42d8e4700ca', $cp->getClientId() );
        }
 
        private function newLBFactoryMulti( array $baseOverride = [], array $serverOverride = [] ) {
@@ -419,7 +421,8 @@ class LBFactoryTest extends MediaWikiTestCase {
                                'test-db1' => $wgDBserver,
                        ],
                        'loadMonitorClass' => LoadMonitorNull::class,
-                       'localDomain' => new DatabaseDomain( $wgDBname, null, $wgDBprefix )
+                       'localDomain' => new DatabaseDomain( $wgDBname, null, $wgDBprefix ),
+                       'agent' => 'MW-UNIT-TESTS'
                ] );
        }
 
@@ -609,35 +612,71 @@ class LBFactoryTest extends MediaWikiTestCase {
 
        /**
         * @covers \Wikimedia\Rdbms\LBFactory::makeCookieValueFromCPIndex()
-        * @covers \Wikimedia\Rdbms\LBFactory::getCPIndexFromCookieValue()
+        * @covers \Wikimedia\Rdbms\LBFactory::getCPInfoFromCookieValue()
         */
        public function testCPPosIndexCookieValues() {
-               $this->assertEquals( '3@542', LBFactory::makeCookieValueFromCPIndex( 3, 542 ) );
-
                $time = 1526522031;
+               $agentId = md5( 'Ramsey\'s Loyal Presa Canario' );
+
+               $lbFactory = $this->newLBFactoryMulti();
+               $this->assertEquals(
+                       '3@542#c47dcfb0566e7d7bc110a6128a45c93a',
+                       LBFactory::makeCookieValueFromCPIndex( 3, 542, $agentId )
+               );
+
+               $lbFactory = $this->newLBFactoryMulti();
+               $lbFactory->setRequestInfo( [ 'IPAddress' => '10.64.24.52', 'UserAgent' => 'meow' ] );
+               $this->assertEquals(
+                       '1@542#c47dcfb0566e7d7bc110a6128a45c93a',
+                       LBFactory::makeCookieValueFromCPIndex( 1, 542, $agentId )
+               );
+
+               $this->assertSame(
+                       null,
+                       LBFactory::getCPInfoFromCookieValue( "5#$agentId", $time - 10 )['index'],
+                       'No time set'
+               );
                $this->assertSame(
-                       5,
-                       LBFactory::getCPIndexFromCookieValue( "5", $time - 10 )
+                       null,
+                       LBFactory::getCPInfoFromCookieValue( "5@$time", $time - 10 )['index'],
+                       'No agent set'
                );
                $this->assertSame(
                        null,
-                       LBFactory::getCPIndexFromCookieValue( "0", $time - 10 )
+                       LBFactory::getCPInfoFromCookieValue( "0@$time#$agentId", $time - 10 )['index'],
+                       'Bad index'
                );
+
                $this->assertSame(
                        2,
-                       LBFactory::getCPIndexFromCookieValue( "2@$time", $time - 10 )
+                       LBFactory::getCPInfoFromCookieValue( "2@$time#$agentId", $time - 10 )['index'],
+                       'Fresh'
                );
                $this->assertSame(
                        2,
-                       LBFactory::getCPIndexFromCookieValue( "2@$time", $time + 9 - 10 )
+                       LBFactory::getCPInfoFromCookieValue( "2@$time#$agentId", $time + 9 - 10 )['index'],
+                       'Almost stale'
+               );
+               $this->assertSame(
+                       null,
+                       LBFactory::getCPInfoFromCookieValue( "0@$time#$agentId", $time + 9 - 10 )['index'],
+                       'Almost stale; bad index'
                );
                $this->assertSame(
                        null,
-                       LBFactory::getCPIndexFromCookieValue( "0@$time", $time + 9 - 10 )
+                       LBFactory::getCPInfoFromCookieValue( "2@$time#$agentId", $time + 11 - 10 )['index'],
+                       'Stale'
+               );
+
+               $this->assertSame(
+                       $agentId,
+                       LBFactory::getCPInfoFromCookieValue( "5@$time#$agentId", $time - 10 )['clientId'],
+                       'Live (client ID)'
                );
                $this->assertSame(
                        null,
-                       LBFactory::getCPIndexFromCookieValue( "2@$time", $time + 11 - 10 )
+                       LBFactory::getCPInfoFromCookieValue( "5@$time#$agentId", $time + 11 - 10 )['clientId'],
+                       'Stale (client ID)'
                );
        }
 }