|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Kirby\Query; |
| 4 | + |
| 5 | +use Kirby\Cms\App; |
| 6 | +use Kirby\Cms\Pages; |
| 7 | +use Kirby\Filesystem\Dir; |
| 8 | +use Kirby\Image\QrCode; |
| 9 | +use Kirby\Toolkit\I18n; |
| 10 | + |
| 11 | +class QueryInterpretedDefaultFunctionsTest extends \Kirby\TestCase |
| 12 | +{ |
| 13 | + public const TMP = KIRBY_TMP_DIR . '/Query.QueryDefaultFunctions'; |
| 14 | + |
| 15 | + protected function setUp(): void |
| 16 | + { |
| 17 | + new App([ |
| 18 | + 'options' => [ |
| 19 | + 'query' => [ |
| 20 | + 'runner' => 'interpreted' |
| 21 | + ] |
| 22 | + ] |
| 23 | + ]); |
| 24 | + } |
| 25 | + |
| 26 | + protected function tearDown(): void |
| 27 | + { |
| 28 | + App::destroy(); |
| 29 | + Dir::remove(static::TMP); |
| 30 | + } |
| 31 | + |
| 32 | + public function testKirby() |
| 33 | + { |
| 34 | + $query = new Query('kirby'); |
| 35 | + $this->assertInstanceOf(App::class, $query->resolve()); |
| 36 | + |
| 37 | + $query = new Query('kirby.site'); |
| 38 | + $this->assertIsSite($query->resolve()); |
| 39 | + } |
| 40 | + |
| 41 | + public function testCollection() |
| 42 | + { |
| 43 | + new App([ |
| 44 | + 'site' => [ |
| 45 | + 'children' => [ |
| 46 | + ['slug' => 'test'] |
| 47 | + ] |
| 48 | + ], |
| 49 | + 'collections' => [ |
| 50 | + 'test' => fn ($pages) => $pages |
| 51 | + ] |
| 52 | + ]); |
| 53 | + |
| 54 | + $query = new Query('collection("test")'); |
| 55 | + $collection = $query->resolve(); |
| 56 | + $this->assertInstanceOf(Pages::class, $collection); |
| 57 | + $this->assertCount(1, $collection); |
| 58 | + } |
| 59 | + |
| 60 | + public function testFile() |
| 61 | + { |
| 62 | + new App([ |
| 63 | + 'site' => [ |
| 64 | + 'children' => [ |
| 65 | + [ |
| 66 | + 'slug' => 'a', |
| 67 | + 'files' => [ |
| 68 | + ['filename' => 'test.jpg'] |
| 69 | + ] |
| 70 | + ] |
| 71 | + ], |
| 72 | + 'files' => [ |
| 73 | + ['filename' => 'test.jpg'] |
| 74 | + ] |
| 75 | + ] |
| 76 | + ]); |
| 77 | + |
| 78 | + $query = new Query('file("test.jpg")'); |
| 79 | + $this->assertIsFile($query->resolve()); |
| 80 | + |
| 81 | + $query = new Query('file("a/test.jpg")'); |
| 82 | + $this->assertIsFile($query->resolve()); |
| 83 | + |
| 84 | + $query = new Query('file("b/test.jpg")'); |
| 85 | + $this->assertNull($query->resolve()); |
| 86 | + } |
| 87 | + |
| 88 | + public function testPage() |
| 89 | + { |
| 90 | + $app = new App([ |
| 91 | + 'roots' => [ |
| 92 | + 'index' => static::TMP |
| 93 | + ], |
| 94 | + 'site' => [ |
| 95 | + 'children' => [ |
| 96 | + [ |
| 97 | + 'slug' => 'a', |
| 98 | + ], |
| 99 | + [ |
| 100 | + 'slug' => 'b', |
| 101 | + 'content' => ['uuid' => 'test'] |
| 102 | + ] |
| 103 | + ] |
| 104 | + ] |
| 105 | + ]); |
| 106 | + |
| 107 | + $a = $app->page('a'); |
| 108 | + $b = $app->page('b'); |
| 109 | + |
| 110 | + $query = new Query('page("a")'); |
| 111 | + $this->assertSame($a, $query->resolve()); |
| 112 | + |
| 113 | + $query = new Query('page("a").slug'); |
| 114 | + $this->assertSame('a', $query->resolve(['slug' => 'foo'])); |
| 115 | + |
| 116 | + $query = new Query('page("page://test")'); |
| 117 | + $this->assertSame($b, $query->resolve(['page' => $b])); |
| 118 | + |
| 119 | + $query = new Query('page("c")'); |
| 120 | + $this->assertNull($query->resolve()); |
| 121 | + } |
| 122 | + |
| 123 | + public function testqr() |
| 124 | + { |
| 125 | + $query = new Query('qr("https://getkirby.com")'); |
| 126 | + $qr = $query->resolve(); |
| 127 | + $this->assertInstanceOf(QrCode::class, $qr); |
| 128 | + $this->assertSame('https://getkirby.com', $qr->data); |
| 129 | + } |
| 130 | + |
| 131 | + public function testSite() |
| 132 | + { |
| 133 | + new App([ |
| 134 | + 'site' => [ |
| 135 | + 'children' => [ |
| 136 | + ['slug' => 'a'] |
| 137 | + ] |
| 138 | + ] |
| 139 | + ]); |
| 140 | + |
| 141 | + $query = new Query('site'); |
| 142 | + $this->assertIsSite($query->resolve()); |
| 143 | + |
| 144 | + $query = new Query('site.children.first'); |
| 145 | + $this->assertIsPage($query->resolve()); |
| 146 | + } |
| 147 | + |
| 148 | + public function testT() |
| 149 | + { |
| 150 | + I18n::$translations = [ |
| 151 | + 'en' => ['add' => 'Add'], |
| 152 | + 'de' => ['add' => 'Hinzufügen'] |
| 153 | + ]; |
| 154 | + |
| 155 | + $query = new Query('t("add")'); |
| 156 | + $this->assertSame('Add', $query->resolve()); |
| 157 | + |
| 158 | + $query = new Query('t("notfound", "fallback")'); |
| 159 | + $this->assertSame('fallback', $query->resolve()); |
| 160 | + |
| 161 | + $query = new Query('t("add", null, "de")'); |
| 162 | + $this->assertSame('Hinzufügen', $query->resolve()); |
| 163 | + |
| 164 | + I18n::$translations = []; |
| 165 | + } |
| 166 | + |
| 167 | + public function testUser() |
| 168 | + { |
| 169 | + new App([ |
| 170 | + 'users' => [ |
| 171 | + ['id' => 'user-a', 'email' => 'foo@bar.com'] |
| 172 | + ] |
| 173 | + ]); |
| 174 | + |
| 175 | + $query = new Query('user("user://user-a")'); |
| 176 | + $this->assertIsUser($query->resolve()); |
| 177 | + |
| 178 | + $query = new Query('user("user://user-a").email'); |
| 179 | + $this->assertSame('foo@bar.com', $query->resolve()); |
| 180 | + |
| 181 | + $query = new Query('user("user-b")'); |
| 182 | + $this->assertNull($query->resolve()); |
| 183 | + } |
| 184 | +} |
0 commit comments