| 36 | |
| 37 | /** |
| 38 | * Test for problems with a long transient base key and a longer |
| 39 | * key that starts with the base key. |
| 40 | * |
| 41 | * @ticket 15058 |
| 42 | **/ |
| 43 | function test_long_and_longer_transient_key() { |
| 44 | $base_key = str_repeat('-', 45); |
| 45 | $base_value = rand_str(); |
| 46 | |
| 47 | foreach ( array( 44, 45, 53, 65, 255, 256, 257 ) as $length ) { |
| 48 | wp_cache_flush(); |
| 49 | set_transient( $base_key, $base_value ); |
| 50 | $key = $base_key . $length; |
| 51 | $key = str_pad( $key, $length, "-", STR_PAD_LEFT ); |
| 52 | $value = rand_str(); |
| 53 | $transient_result = set_transient( $key, $value ); |
| 54 | wp_cache_flush(); |
| 55 | $this->assertEquals( $base_value, get_transient( $base_key ), |
| 56 | "Base key should remain the same at len $length" ); |
| 57 | if ( $transient_result ) { |
| 58 | $this->assertEquals( $value, get_transient( $key ), |
| 59 | "Long key should remain the same at len $length" ); |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Test for problems with two transient keys of equivalent length. |
| 66 | * |
| 67 | * @ticket 15058 |
| 68 | **/ |
| 69 | function test_long_transient_keys() { |
| 70 | foreach ( array( 44, 45, 53, 65, 255, 256, 257 ) as $length ) { |
| 71 | $key1 = rand_str( 32 ) . "X"; |
| 72 | $key1 = str_pad( $key1, $length, "-", STR_PAD_LEFT ); |
| 73 | $key1 = substr( $key1, 0, $length ); |
| 74 | |
| 75 | // Second key: Replace the last character to detect truncation |
| 76 | $key2 = substr( $key1, 0, $length - 1 ) . "Y"; |
| 77 | |
| 78 | $value1 = rand_str(); |
| 79 | $value2 = rand_str(); |
| 80 | |
| 81 | $transient_result = set_transient( $key1, $value1 ); |
| 82 | if ($transient_result) { |
| 83 | $transient_result2 = set_transient( $key2, $value2 ); |
| 84 | if ($transient_result2) { |
| 85 | wp_cache_flush(); |
| 86 | $this->assertEquals( $value1, get_transient( $key1 ), |
| 87 | "Length $length, first transient should work" ); |
| 88 | $this->assertEquals( $value2, get_transient( $key2 ), |
| 89 | "Length $length, second transient should work" ); |
| 90 | } |
| 91 | } |
| 92 | else { |
| 93 | $this->assertFalse( get_transient( $key1 ), |
| 94 | "transient keylen $length set failed, get should fail" ); |
| 95 | } |
| 96 | } |
| 97 | } |