| 
									
										
										
										
											2020-04-04 08:16:05 +08:00
										 |  |  | <?php namespace Tests\Entity; | 
					
						
							| 
									
										
										
										
											2016-05-14 04:20:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-22 08:17:45 +08:00
										 |  |  | use BookStack\Entities\Models\Book; | 
					
						
							|  |  |  | use BookStack\Entities\Models\Chapter; | 
					
						
							| 
									
										
										
										
											2018-09-25 19:30:50 +08:00
										 |  |  | use BookStack\Actions\Tag; | 
					
						
							| 
									
										
										
										
											2020-11-22 08:17:45 +08:00
										 |  |  | use BookStack\Entities\Models\Entity; | 
					
						
							|  |  |  | use BookStack\Entities\Models\Page; | 
					
						
							| 
									
										
										
										
											2018-09-25 19:30:50 +08:00
										 |  |  | use BookStack\Auth\Permissions\PermissionService; | 
					
						
							| 
									
										
										
										
											2020-04-04 08:16:05 +08:00
										 |  |  | use Tests\BrowserKitTest; | 
					
						
							| 
									
										
										
										
											2016-05-14 04:20:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-26 03:35:40 +08:00
										 |  |  | class TagTest extends BrowserKitTest | 
					
						
							| 
									
										
										
										
											2016-05-14 04:20:21 +08:00
										 |  |  | { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     protected $defaultTagCount = 20; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Get an instance of a page that has many tags. | 
					
						
							| 
									
										
										
										
											2018-09-25 19:30:50 +08:00
										 |  |  |      * @param \BookStack\Actions\Tag[]|bool $tags | 
					
						
							| 
									
										
										
										
											2019-09-20 07:18:28 +08:00
										 |  |  |      * @return Entity | 
					
						
							| 
									
										
										
										
											2016-05-14 04:20:21 +08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2019-09-20 07:18:28 +08:00
										 |  |  |     protected function getEntityWithTags($class, $tags = false): Entity | 
					
						
							| 
									
										
										
										
											2016-05-14 04:20:21 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-03-30 21:09:51 +08:00
										 |  |  |         $entity = $class::first(); | 
					
						
							| 
									
										
										
										
											2016-05-14 04:20:21 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (!$tags) { | 
					
						
							|  |  |  |             $tags = factory(Tag::class, $this->defaultTagCount)->make(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-30 21:09:51 +08:00
										 |  |  |         $entity->tags()->saveMany($tags); | 
					
						
							|  |  |  |         return $entity; | 
					
						
							| 
									
										
										
										
											2016-05-14 04:20:21 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_tag_name_suggestions() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         // Create some tags with similar names to test with
 | 
					
						
							|  |  |  |         $attrs = collect(); | 
					
						
							|  |  |  |         $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'country'])); | 
					
						
							|  |  |  |         $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'color'])); | 
					
						
							|  |  |  |         $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'city'])); | 
					
						
							|  |  |  |         $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'county'])); | 
					
						
							|  |  |  |         $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'planet'])); | 
					
						
							|  |  |  |         $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'plans'])); | 
					
						
							| 
									
										
										
										
											2018-03-30 21:09:51 +08:00
										 |  |  |         $page = $this->getEntityWithTags(Page::class, $attrs); | 
					
						
							| 
									
										
										
										
											2016-05-14 04:20:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-16 03:12:53 +08:00
										 |  |  |         $this->asAdmin()->get('/ajax/tags/suggest/names?search=dog')->seeJsonEquals([]); | 
					
						
							|  |  |  |         $this->get('/ajax/tags/suggest/names?search=co')->seeJsonEquals(['color', 'country', 'county']); | 
					
						
							|  |  |  |         $this->get('/ajax/tags/suggest/names?search=cou')->seeJsonEquals(['country', 'county']); | 
					
						
							|  |  |  |         $this->get('/ajax/tags/suggest/names?search=pla')->seeJsonEquals(['planet', 'plans']); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_tag_value_suggestions() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         // Create some tags with similar values to test with
 | 
					
						
							|  |  |  |         $attrs = collect(); | 
					
						
							|  |  |  |         $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'country', 'value' => 'cats'])); | 
					
						
							|  |  |  |         $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'color', 'value' => 'cattery'])); | 
					
						
							|  |  |  |         $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'city', 'value' => 'castle'])); | 
					
						
							|  |  |  |         $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'county', 'value' => 'dog'])); | 
					
						
							|  |  |  |         $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'planet', 'value' => 'catapult'])); | 
					
						
							|  |  |  |         $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'plans', 'value' => 'dodgy'])); | 
					
						
							| 
									
										
										
										
											2018-03-30 21:09:51 +08:00
										 |  |  |         $page = $this->getEntityWithTags(Page::class, $attrs); | 
					
						
							| 
									
										
										
										
											2016-05-16 03:12:53 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $this->asAdmin()->get('/ajax/tags/suggest/values?search=ora')->seeJsonEquals([]); | 
					
						
							|  |  |  |         $this->get('/ajax/tags/suggest/values?search=cat')->seeJsonEquals(['cats', 'cattery', 'catapult']); | 
					
						
							|  |  |  |         $this->get('/ajax/tags/suggest/values?search=do')->seeJsonEquals(['dog', 'dodgy']); | 
					
						
							|  |  |  |         $this->get('/ajax/tags/suggest/values?search=cas')->seeJsonEquals(['castle']); | 
					
						
							| 
									
										
										
										
											2016-05-14 04:20:21 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_entity_permissions_effect_tag_suggestions() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $permissionService = $this->app->make(PermissionService::class); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Create some tags with similar names to test with and save to a page
 | 
					
						
							|  |  |  |         $attrs = collect(); | 
					
						
							|  |  |  |         $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'country'])); | 
					
						
							|  |  |  |         $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'color'])); | 
					
						
							| 
									
										
										
										
											2018-03-30 21:09:51 +08:00
										 |  |  |         $page = $this->getEntityWithTags(Page::class, $attrs); | 
					
						
							| 
									
										
										
										
											2016-05-14 04:20:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-22 00:16:27 +08:00
										 |  |  |         $this->asAdmin()->get('/ajax/tags/suggest/names?search=co')->seeJsonEquals(['color', 'country']); | 
					
						
							|  |  |  |         $this->asEditor()->get('/ajax/tags/suggest/names?search=co')->seeJsonEquals(['color', 'country']); | 
					
						
							| 
									
										
										
										
											2016-05-14 04:20:21 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // Set restricted permission the page
 | 
					
						
							|  |  |  |         $page->restricted = true; | 
					
						
							|  |  |  |         $page->save(); | 
					
						
							| 
									
										
										
										
											2019-09-20 07:18:28 +08:00
										 |  |  |         $page->rebuildPermissions(); | 
					
						
							| 
									
										
										
										
											2016-05-14 04:20:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-22 00:16:27 +08:00
										 |  |  |         $this->asAdmin()->get('/ajax/tags/suggest/names?search=co')->seeJsonEquals(['color', 'country']); | 
					
						
							|  |  |  |         $this->asEditor()->get('/ajax/tags/suggest/names?search=co')->seeJsonEquals([]); | 
					
						
							| 
									
										
										
										
											2016-05-14 04:20:21 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |