Explorar el Código

wrench: QRData::estimateTotalBitLength(): do not substract from the estimated bit length

smiley hace 1 año
padre
commit
c1c9cdf285
Se han modificado 2 ficheros con 29 adiciones y 3 borrados
  1. 3 3
      src/Data/QRData.php
  2. 26 0
      tests/Data/QRDataTest.php

+ 3 - 3
src/Data/QRData.php

@@ -164,9 +164,9 @@ final class QRData{
 
 
 			// it seems that in some cases the estimated total length is not 100% accurate,
 			// it seems that in some cases the estimated total length is not 100% accurate,
 			// so we substract 4 bits from the total when not in mixed mode
 			// so we substract 4 bits from the total when not in mixed mode
-			if(count($this->dataSegments) <= 1){
-				$length -= 4;
-			}
+#			if(count($this->dataSegments) <= 1){
+#				$length -= 4;
+#			}
 
 
 			// we've got a match!
 			// we've got a match!
 			// or let's see if there's a higher version number available
 			// or let's see if there's a higher version number available

+ 26 - 0
tests/Data/QRDataTest.php

@@ -11,7 +11,9 @@
 namespace chillerlan\QRCodeTest\Data;
 namespace chillerlan\QRCodeTest\Data;
 
 
 use chillerlan\QRCode\Common\BitBuffer;
 use chillerlan\QRCode\Common\BitBuffer;
+use chillerlan\QRCode\Common\EccLevel;
 use chillerlan\QRCode\Common\MaskPattern;
 use chillerlan\QRCode\Common\MaskPattern;
+use chillerlan\QRCode\Data\Byte;
 use chillerlan\QRCode\Data\QRData;
 use chillerlan\QRCode\Data\QRData;
 use chillerlan\QRCode\Output\QRGdImagePNG;
 use chillerlan\QRCode\Output\QRGdImagePNG;
 use chillerlan\QRCode\QRCode;
 use chillerlan\QRCode\QRCode;
@@ -63,4 +65,28 @@ final class QRDataTest extends TestCase{
 		$this::assertSame($decodeResult->data, 'https://www.youtube.com/watch?v=DLzxrzFCyOs&t=43s');
 		$this::assertSame($decodeResult->data, 'https://www.youtube.com/watch?v=DLzxrzFCyOs&t=43s');
 	}
 	}
 
 
+	public function testEstimateTotalBitLength():void{
+
+		$options = new QROptions([
+			'versionMin'          => 10,
+			'quietzoneSize'       => 2,
+			'eccLevel'            => EccLevel::H,
+#			'outputType'          => QROutputInterface::CUSTOM,
+#			'outputInterface'     => PmaQrCodeSVG::class,
+			'outputBase64'        => false,
+			'cssClass'            => 'pma-2fa-qrcode',
+			'drawCircularModules' => true,
+		]);
+
+		// version 10H has a maximum of 976 bits, which is the exact length of the string below
+		// QRData::estimateTotalBitLength() used to substract 4 bits for a hypothetical data mode indicator
+		// we're now going the safe route and do not do that anymore...
+		$str = 'otpauth://totp/user?secret=P2SXMJFJ7DJGHLVEQYBNH2EYM4FH66CR'.
+		       '&issuer=phpMyAdmin%20%28%29&digits=6&algorithm=SHA1&period=30';
+
+		$qrData = new QRData($options, [new Byte($str)]);
+
+		$this::assertSame(980, $qrData->estimateTotalBitLength());
+	}
+
 }
 }