5ab548936ccfa5b3208fc825f97c4a886f7a6577
[friendica-addons.git/.git] / s3_storage / vendor / akeeba / s3 / minitest / Test / SmallInlineXMLFiles.php
1 <?php
2 /**
3  * Akeeba Engine
4  *
5  * @package   akeebaengine
6  * @copyright Copyright (c)2006-2023 Nicholas K. Dionysopoulos / Akeeba Ltd
7  * @license   GNU General Public License version 3, or later
8  */
9
10 namespace Akeeba\MiniTest\Test;
11
12
13 use Akeeba\S3\Connector;
14 use Akeeba\S3\Input;
15
16 /**
17  * Upload, download and delete small XML files (under 1MB) using a string source
18  *
19  * @package Akeeba\MiniTest\Test
20  */
21 class SmallInlineXMLFiles extends SmallFiles
22 {
23         public static function upload10KbRoot(Connector $s3, array $options): bool
24         {
25                 return static::upload($s3, $options, AbstractTest::TEN_KB, 'root_10kb.xml');
26         }
27
28         public static function upload10KbRootGreek(Connector $s3, array $options): bool
29         {
30                 return static::upload($s3, $options, AbstractTest::TEN_KB, 'δοκιμή_10kb.xml');
31         }
32
33         public static function upload10KbFolderGreek(Connector $s3, array $options): bool
34         {
35                 return static::upload($s3, $options, AbstractTest::TEN_KB, 'ο_φάκελός_μου/δοκιμή_10kb.xml');
36         }
37
38         public static function upload600KbRoot(Connector $s3, array $options): bool
39         {
40                 return static::upload($s3, $options, AbstractTest::SIX_HUNDRED_KB, 'root_600kb.xml');
41         }
42
43         public static function upload10KbFolder(Connector $s3, array $options): bool
44         {
45                 return static::upload($s3, $options, AbstractTest::TEN_KB, 'my_folder/10kb.xml');
46         }
47
48         public static function upload600KbFolder(Connector $s3, array $options): bool
49         {
50                 return static::upload($s3, $options, AbstractTest::SIX_HUNDRED_KB, 'my_folder/600kb.xml');
51         }
52
53         protected static function upload(Connector $s3, array $options, int $size, string $uri): bool
54         {
55                 // Randomize the name. Required for archive buckets where you cannot overwrite data.
56                 $dotPos = strrpos($uri, '.');
57                 $uri    = substr($uri, 0, $dotPos) . '.' . md5(microtime(false)) . substr($uri, $dotPos);
58
59                 // Create some random data to upload
60                 $sourceData = static::createXMLFile($size);
61
62                 // Upload the data. Throws exception if it fails.
63                 $bucket = $options['bucket'];
64                 $input  = Input::createFromData($sourceData);
65
66                 $s3->putObject($input, $bucket, $uri);
67
68                 // Tentatively accept that this method succeeded.
69                 $result = true;
70
71                 // Should I download the file and compare its contents with my random data?
72                 if (static::$downloadAfter)
73                 {
74                         $downloadedData = $s3->getObject($bucket, $uri);
75
76                         $result = static::areStringsEqual($sourceData, $downloadedData);
77                 }
78
79                 // Should I delete the remotely stored file?
80                 if (static::$deleteRemote)
81                 {
82                         // Delete the remote file. Throws exception if it fails.
83                         $s3->deleteObject($bucket, $uri);
84                 }
85
86                 return $result;
87         }
88
89         private static function createXMLFile(int $size): string
90         {
91                 $out = <<< XML
92 <?xml version="1.0" encoding="utf-8" ?>
93 <root>
94 XML;
95
96                 $chunks = floor(($size - 55) / 1024);
97
98                 for ($i = 1; $i <= $chunks; $i++)
99                 {
100                         $randomBlock = static::genRandomData(1024 - 63);
101                         $out .= <<< XML
102                 <element>
103                         <id>$i</id>
104                         <data><![CDATA[$randomBlock]]></data>
105                 </element>
106 XML;
107
108                 }
109
110
111                 $out .= <<< XML
112 </root>
113 XML;
114
115                 return $out;
116         }
117
118         private static function genRandomData(int $length): string
119         {
120                 $chars     = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890';
121                 $maxLength = strlen($chars) - 1;
122                 $salt      = '';
123
124                 for ($i = 0; $i < $length; $i++)
125                 {
126                         $salt .= substr($chars, random_int(0, $maxLength), 1);
127                 }
128
129                 return $salt;
130         }
131 }