Merge branch 'develop' into github-action-deploy

This commit is contained in:
Jonny Barnes 2021-10-24 18:14:43 +01:00
commit 005d3cdf90
87 changed files with 8023 additions and 11405 deletions

View file

@ -7,7 +7,7 @@ APP_LOG_LEVEL=warning
DB_CONNECTION=pgsql
DB_HOST=127.0.0.1
DB_PORT=5432
DB_DATABASE=jbuktest
DB_DATABASE=jbukdev_testing
DB_USERNAME=postgres
DB_PASSWORD=postgres

View file

@ -1,17 +0,0 @@
APP_ENV=testing
APP_DEBUG=true
APP_KEY=base64:6DJhvZLVjE6dD4Cqrteh+6Z5vZlG+v/soCKcDHLOAH0=
APP_URL=http://jonnybarnes.localhost
APP_LONGURL=jonnybarnes.localhost
APP_SHORTURL=jmb.localhost
DB_CONNECTION=travis
CACHE_DRIVER=array
SESSION_DRIVER=array
QUEUE_DRIVER=sync
SCOUT_DRIVER=pgsql
DISPLAY_NAME='Travis Test'
USER_NAME=travis

View file

@ -11,11 +11,11 @@ jobs:
services:
postgres:
image: postgres:13.1
image: postgres:13.4
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: jbuktest
POSTGRES_DB: jbukdev_testing
ports:
- 5432:5432
@ -36,15 +36,13 @@ jobs:
- name: Copy .env
run: php -r "file_exists('.env') || copy('.env.github', '.env');"
- name: Install dependencies
run: composer install -q --no-ansi --no-interaction --no-progress
run: composer install --quiet --no-ansi --no-interaction --no-progress
- name: Generate key
run: php artisan key:generate
- name: Setup directory permissions
run: chmod -R 777 storage bootstrap/cache
- name: Setup test database
run: |
php artisan migrate
php artisan db:seed
run: php artisan migrate
- name: Execute tests (Unit and Feature tests) via PHPUnit
run: vendor/bin/phpunit
- name: Run phpcs

View file

@ -24,16 +24,6 @@ class ParseCachedWebMentions extends Command
*/
protected $description = 'Re-parse the webmentions cached HTML';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
@ -41,15 +31,15 @@ class ParseCachedWebMentions extends Command
*/
public function handle(FileSystem $filesystem)
{
$HTMLfiles = $filesystem->allFiles(storage_path() . '/HTML');
foreach ($HTMLfiles as $file) {
if ($file->getExtension() != 'backup') { //we dont want to parse.backup files
$htmlFiles = $filesystem->allFiles(storage_path() . '/HTML');
foreach ($htmlFiles as $file) {
if ($file->getExtension() !== 'backup') { //we dont want to parse `.backup` files
$filepath = $file->getPathname();
$this->info('Loading HTML from: ' . $filepath);
$html = $filesystem->get($filepath);
$url = $this->URLFromFilename($filepath);
$microformats = \Mf2\parse($html, $url);
$url = $this->urlFromFilename($filepath);
$webmention = WebMention::where('source', $url)->firstOrFail();
$microformats = \Mf2\parse($html, $url);
$webmention->mf2 = json_encode($microformats);
$webmention->save();
$this->info('Saved the microformats to the database.');
@ -63,12 +53,12 @@ class ParseCachedWebMentions extends Command
* @param string
* @return string
*/
private function URLFromFilename(string $filepath): string
private function urlFromFilename(string $filepath): string
{
$dir = mb_substr($filepath, mb_strlen(storage_path() . '/HTML/'));
$url = str_replace(['http/', 'https/'], ['http://', 'https://'], $dir);
if (mb_substr($url, -10) == 'index.html') {
$url = mb_substr($url, 0, mb_strlen($url) - 10);
if (mb_substr($url, -10) === 'index.html') {
$url = mb_substr($url, 0, -10);
}
return $url;

View file

@ -7,16 +7,10 @@ namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Models\Article;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;
class ArticlesController extends Controller
{
/**
* List the articles that can be edited.
*
* @return \Illuminate\View\View
*/
public function index(): View
{
$posts = Article::select('id', 'title', 'published')->orderBy('id', 'desc')->get();
@ -24,11 +18,6 @@ class ArticlesController extends Controller
return view('admin.articles.index', ['posts' => $posts]);
}
/**
* Show the new article form.
*
* @return \Illuminate\View\View
*/
public function create(): View
{
$message = session('message');
@ -36,11 +25,6 @@ class ArticlesController extends Controller
return view('admin.articles.create', ['message' => $message]);
}
/**
* Process an incoming request for a new article and save it.
*
* @return \Illuminate\Http\RedirectResponse
*/
public function store(): RedirectResponse
{
//if a `.md` is attached use that for the main content.
@ -49,42 +33,21 @@ class ArticlesController extends Controller
$content = $file->fread($file->getSize());
}
$main = $content ?? request()->input('main');
$article = Article::create(
[
'url' => request()->input('url'),
'title' => request()->input('title'),
'main' => $main,
'published' => request()->input('published') ?? 0,
]
);
Article::create([
'url' => request()->input('url'),
'title' => request()->input('title'),
'main' => $main,
'published' => request()->input('published') ?? 0,
]);
return redirect('/admin/blog');
}
/**
* Show the edit form for an existing article.
*
* @param int $articleId
* @return \Illuminate\View\View
*/
public function edit(int $articleId): View
public function edit(Article $article): View
{
$post = Article::select(
'title',
'main',
'url',
'published'
)->where('id', $articleId)->get();
return view('admin.articles.edit', ['id' => $articleId, 'post' => $post]);
return view('admin.articles.edit', ['article' => $article]);
}
/**
* Process an incoming request to edit an article.
*
* @param int $articleId
* @return \Illuminate\Http\RedirectResponse
*/
public function update(int $articleId): RedirectResponse
{
$article = Article::find($articleId);
@ -97,12 +60,6 @@ class ArticlesController extends Controller
return redirect('/admin/blog');
}
/**
* Process a request to delete an aricle.
*
* @param int $articleId
* @return \Illuminate\Http\RedirectResponse
*/
public function destroy(int $articleId): RedirectResponse
{
Article::where('id', $articleId)->delete();

View file

@ -59,18 +59,14 @@ class ArticlesController extends Controller
* We only have the ID, work out post title, year and month
* and redirect to it.
*
* @param int $idFromUrl
* @param string $idFromUrl
* @return RedirectResponse
*/
public function onlyIdInUrl(int $idFromUrl): RedirectResponse
public function onlyIdInUrl(string $idFromUrl): RedirectResponse
{
$realId = resolve(Numbers::class)->b60tonum((string) $idFromUrl);
$realId = resolve(Numbers::class)->b60tonum($idFromUrl);
try {
$article = Article::findOrFail($realId);
} catch (ModelNotFoundException $exception) {
abort(404);
}
$article = Article::findOrFail($realId);
return redirect($article->link);
}

View file

@ -18,19 +18,15 @@ class CSPHeader
{
// headers have to be single-line strings,
// so we concat multiple lines
// phpcs:disable
// phpcs:disable Generic.Files.LineLength.TooLong
return $next($request)
->header(
'Content-Security-Policy',
"default-src 'self'; " .
"script-src 'self' 'unsafe-inline' 'unsafe-eval' https://api.mapbox.com https://api.tiles.mapbox.com blob:; " .
"style-src 'self' 'unsafe-inline' https://api.mapbox.com https://api.tiles.mapbox.com cloud.typography.com jonnybarnes.uk; " .
"img-src 'self' data: blob: https://pbs.twimg.com https://api.mapbox.com https://*.tiles.mapbox.com https://jbuk-media.s3-eu-west-1.amazonaws.com https://jbuk-media-dev.s3-eu-west-1.amazonaws.com https://secure.gravatar.com https://graph.facebook.com *.fbcdn.net https://*.cdninstagram.com https://*.4sqi.net https://upload.wikimedia.org; " .
"style-src 'self' cloud.typography.com jonnybarnes.uk; " .
"img-src 'self' data: blob: https://pbs.twimg.com https://jbuk-media.s3-eu-west-1.amazonaws.com https://jbuk-media-dev.s3-eu-west-1.amazonaws.com https://secure.gravatar.com https://graph.facebook.com *.fbcdn.net https://*.cdninstagram.com https://*.4sqi.net https://upload.wikimedia.org; " .
"font-src 'self' data:; " .
"connect-src 'self' https://api.mapbox.com https://*.tiles.mapbox.com https://events.mapbox.com data: blob:; " .
"worker-src 'self' blob:; " .
"frame-src 'self' https://www.youtube.com blob:; " .
'child-src blob:; ' .
'upgrade-insecure-requests; ' .
'block-all-mixed-content; ' .
'report-to csp-endpoint; ' .
@ -43,6 +39,6 @@ class CSPHeader
"'max-age': 10886400" .
'}'
);
// phpcs:enable
// phpcs:enable Generic.Files.LineLength.TooLong
}
}

View file

@ -41,11 +41,13 @@ class SaveProfileImage implements ShouldQueue
{
try {
$author = $authorship->findAuthor($this->microformats);
} catch (AuthorshipParserException $e) {
return;
} catch (AuthorshipParserException) {
return null;
}
$photo = Arr::get($author, 'properties.photo.0');
$home = Arr::get($author, 'properties.url.0');
//dont save pbs.twimg.com links
if (
$photo
@ -53,16 +55,18 @@ class SaveProfileImage implements ShouldQueue
&& parse_url($photo, PHP_URL_HOST) != 'twitter.com'
) {
$client = resolve(Client::class);
try {
$response = $client->get($photo);
$image = $response->getBody();
} catch (RequestException $e) {
} catch (RequestException) {
// we are opening and reading the default image so that
$default = public_path() . '/assets/profile-images/default-image';
$handle = fopen($default, 'rb');
$image = fread($handle, filesize($default));
fclose($handle);
}
$path = public_path() . '/assets/profile-images/' . parse_url($home, PHP_URL_HOST) . '/image';
$parts = explode('/', $path);
$name = array_pop($parts);

View file

@ -6,7 +6,10 @@ namespace App\Jobs;
use App\Models\Note;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Header;
use GuzzleHttp\Psr7\Uri;
use GuzzleHttp\Psr7\UriResolver;
use GuzzleHttp\Psr7\Utils;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
@ -65,14 +68,14 @@ class SendWebMentions implements ShouldQueue
* @param string $url
* @return string|null
*/
public function discoverWebmentionEndpoint(string $url)
public function discoverWebmentionEndpoint(string $url): ?string
{
//lets not send webmentions to myself
if (parse_url($url, PHP_URL_HOST) == config('app.longurl')) {
return;
if (parse_url($url, PHP_URL_HOST) === config('app.longurl')) {
return null;
}
if (Str::startsWith($url, '/notes/tagged/')) {
return;
return null;
}
$endpoint = null;
@ -80,7 +83,7 @@ class SendWebMentions implements ShouldQueue
$guzzle = resolve(Client::class);
$response = $guzzle->get($url);
//check HTTP Headers for webmention endpoint
$links = \GuzzleHttp\Psr7\parse_header($response->getHeader('Link'));
$links = Header::parse($response->getHeader('Link'));
foreach ($links as $link) {
if (mb_stristr($link['rel'], 'webmention')) {
return $this->resolveUri(trim($link[0], '<>'), $url);
@ -110,7 +113,7 @@ class SendWebMentions implements ShouldQueue
*/
public function getLinks(?string $html): array
{
if ($html == '' || is_null($html)) {
if ($html === '' || is_null($html)) {
return [];
}
@ -136,13 +139,13 @@ class SendWebMentions implements ShouldQueue
*/
public function resolveUri(string $url, string $base): string
{
$endpoint = \GuzzleHttp\Psr7\uri_for($url);
if ($endpoint->getScheme() != '') {
$endpoint = Utils::uriFor($url);
if ($endpoint->getScheme() !== '') {
return (string) $endpoint;
}
return (string) Uri::resolve(
\GuzzleHttp\Psr7\uri_for($base),
return (string) UriResolver::resolve(
Utils::uriFor($base),
$endpoint
);
}

View file

@ -7,6 +7,7 @@ namespace App\Models;
use Cviebrock\EloquentSluggable\Sluggable;
use Eloquent;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Carbon;
@ -58,6 +59,7 @@ use Spatie\CommonMarkHighlighter\IndentedCodeRenderer;
*/
class Article extends Model
{
use HasFactory;
use Sluggable;
use SoftDeletes;

View file

@ -6,6 +6,7 @@ namespace App\Models;
use Eloquent;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Carbon;
@ -35,6 +36,8 @@ use Illuminate\Support\Carbon;
*/
class Contact extends Model
{
use HasFactory;
/**
* The database table used by the model.
*

View file

@ -6,6 +6,7 @@ namespace App\Models;
use Eloquent;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Carbon;
@ -41,6 +42,8 @@ use Illuminate\Support\Str;
*/
class Media extends Model
{
use HasFactory;
/**
* The table associated with the model.
*
@ -62,7 +65,7 @@ class Media extends Model
*/
public function note(): BelongsTo
{
return $this->belongsTo('App\Models\Note');
return $this->belongsTo(Note::class);
}
/**
@ -118,7 +121,7 @@ class Media extends Model
$filenameParts = explode('.', $path);
array_pop($filenameParts);
return ltrim(array_reduce($filenameParts, function ($carry, $item) {
return ltrim(array_reduce($filenameParts, static function ($carry, $item) {
return $carry . '.' . $item;
}, ''), '.');
}

View file

@ -5,14 +5,15 @@ declare(strict_types=1);
namespace App\Models;
use App\Exceptions\TwitterContentException;
use Barryvdh\LaravelIdeHelper\Eloquent;
use Codebird\Codebird;
use Eloquent;
use Exception;
use GuzzleHttp\Client;
use Illuminate\Database\Eloquent\Relations\{BelongsTo, BelongsToMany, HasMany, MorphMany};
use Illuminate\Database\Eloquent\{Builder, Collection, Factories\HasFactory, Model, SoftDeletes};
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Cache;
use JetBrains\PhpStorm\ArrayShape;
use Jonnybarnes\IndieWeb\Numbers;
use Laravel\Scout\Searchable;
use League\CommonMark\Block\Element\{FencedCode, IndentedCode};
@ -20,6 +21,11 @@ use League\CommonMark\Extension\Autolink\AutolinkExtension;
use League\CommonMark\{CommonMarkConverter, Environment};
use Normalizer;
use Spatie\CommonMarkHighlighter\{FencedCodeRenderer, IndentedCodeRenderer};
use App\Models\Tag;
use App\Models\MicropubClient;
use App\Models\WebMention;
use App\Models\Place;
use App\Models\Media;
/**
* App\Models\Note.
@ -102,12 +108,12 @@ class Note extends Model
/**
* This variable is used to keep track of contacts in a note.
*/
protected $contacts;
protected ?array $contacts;
/**
* Set our contacts variable to null.
*
* @param array $attributes
* @param array $attributes
*/
public function __construct(array $attributes = [])
{
@ -145,9 +151,9 @@ class Note extends Model
*
* @return BelongsToMany
*/
public function tags()
public function tags(): BelongsToMany
{
return $this->belongsToMany('App\Models\Tag');
return $this->belongsToMany(Tag::class);
}
/**
@ -155,9 +161,9 @@ class Note extends Model
*
* @return BelongsTo
*/
public function client()
public function client(): BelongsTo
{
return $this->belongsTo('App\Models\MicropubClient', 'client_id', 'client_url');
return $this->belongsTo(MicropubClient::class, 'client_id', 'client_url');
}
/**
@ -165,9 +171,9 @@ class Note extends Model
*
* @return MorphMany
*/
public function webmentions()
public function webmentions(): MorphMany
{
return $this->morphMany('App\Models\WebMention', 'commentable');
return $this->morphMany(WebMention::class, 'commentable');
}
/**
@ -175,9 +181,9 @@ class Note extends Model
*
* @return BelongsTo
*/
public function place()
public function place(): BelongsTo
{
return $this->belongsTo('App\Models\Place');
return $this->belongsTo(Place::class);
}
/**
@ -185,9 +191,9 @@ class Note extends Model
*
* @return HasMany
*/
public function media()
public function media(): HasMany
{
return $this->hasMany('App\Models\Media');
return $this->hasMany(Media::class);
}
/**
@ -195,6 +201,7 @@ class Note extends Model
*
* @return array
*/
#[ArrayShape(['note' => "null|string"])]
public function toSearchableArray(): array
{
return [
@ -207,7 +214,7 @@ class Note extends Model
*
* @param string|null $value
*/
public function setNoteAttribute(?string $value)
public function setNoteAttribute(?string $value): void
{
if ($value !== null) {
$normalized = normalizer_normalize($value, Normalizer::FORM_C);
@ -253,13 +260,13 @@ class Note extends Model
$note = $this->note;
foreach ($this->media as $media) {
if ($media->type == 'image') {
if ($media->type === 'image') {
$note .= '<img src="' . $media->url . '" alt="">';
}
if ($media->type == 'audio') {
if ($media->type === 'audio') {
$note .= '<audio src="' . $media->url . '">';
}
if ($media->type == 'video') {
if ($media->type === 'video') {
$note .= '<video src="' . $media->url . '">';
}
}
@ -398,7 +405,10 @@ class Note extends Model
*/
public function getTwitterAttribute(): ?object
{
if ($this->in_reply_to == null || mb_substr($this->in_reply_to, 0, 20, 'UTF-8') !== 'https://twitter.com/') {
if (
$this->in_reply_to === null ||
!$this->isTwitterLink($this->in_reply_to)
) {
return null;
}
@ -569,7 +579,7 @@ class Note extends Model
public function autoLinkHashtag(string $note): string
{
return preg_replace_callback(
'/#([^\s]*)\b/',
'/#([^\s[:punct:]]+)/',
function ($matches) {
return '<a rel="tag" class="p-category" href="/notes/tagged/'
. Tag::normalize($matches[1]) . '">#'
@ -660,4 +670,9 @@ class Note extends Model
return $address;
});
}
private function isTwitterLink(string $inReplyTo): bool
{
return str_starts_with($inReplyTo, 'https://twitter.com/');
}
}

View file

@ -11,6 +11,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Support\Carbon;
use Illuminate\Support\Str;
/**
* App\Models\Tag.
@ -50,7 +51,7 @@ class Tag extends Model
*/
public function notes()
{
return $this->belongsToMany('App\Models\Note');
return $this->belongsToMany(Note::class);
}
/**
@ -82,13 +83,6 @@ class Tag extends Model
*/
public static function normalize(string $tag): string
{
return mb_strtolower(
preg_replace(
'/&([a-z]{1,2})(acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml|caron);/i',
'$1',
htmlentities($tag)
),
'UTF-8'
);
return Str::slug($tag);
}
}

View file

@ -8,6 +8,7 @@ use App\Traits\FilterHtml;
use Codebird\Codebird;
use Eloquent;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Filesystem\Filesystem;
@ -55,6 +56,7 @@ use Jonnybarnes\WebmentionsParser\Exceptions\AuthorshipParserException;
class WebMention extends Model
{
use FilterHtml;
use HasFactory;
/**
* The database table used by the model.

View file

@ -9,7 +9,7 @@
],
"license": "CC0-1.0",
"require": {
"php": "^7.4|^8.0",
"php": "^8.0",
"ext-intl": "*",
"ext-json": "*",
"ext-dom": "*",
@ -31,7 +31,7 @@
"league/commonmark": "^1.0",
"league/flysystem-aws-s3-v3": "^1.0",
"mf2/mf2": "~0.3",
"pmatseykanets/laravel-scout-postgres": "dev-php8",
"pmatseykanets/laravel-scout-postgres": "^7.3",
"predis/predis": "~1.0",
"spatie/browsershot": "~3.0",
"spatie/commonmark-highlighter": "^2.0",
@ -48,6 +48,7 @@
"nunomaduro/collision": "^5.0",
"phpunit/php-code-coverage": "^9.2",
"phpunit/phpunit": "^9.0",
"spatie/laravel-ray": "^1.12",
"vimeo/psalm": "^4.0"
},
"config": {
@ -91,11 +92,5 @@
"test": [
"vendor/bin/phpunit --stop-on-failure"
]
},
"repositories": [
{
"type": "vcs",
"url": "https://github.com/jonnybarnes/laravel-scout-postgres"
}
]
}
}

3064
composer.lock generated

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,34 @@
<?php
namespace Database\Factories;
use App\Models\Article;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Carbon;
class ArticleFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Article::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'titleurl' => $this->faker->slug(3),
'title' => $this->faker->words(3, true),
'main' => $this->faker->paragraphs(4, true),
'published' => 1,
'created_at' => Carbon::now()->toDateTimeString(),
'updated_at' => Carbon::now()->toDateTimeString(),
];
}
}

View file

@ -0,0 +1,35 @@
<?php
namespace Database\Factories;
use App\Models\Contact;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Carbon;
class ContactFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Contact::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'nick' => mb_strtolower($this->faker->firstName),
'name' => $this->faker->name(),
'homepage' => $this->faker->url,
'twitter' => mb_strtolower($this->faker->firstName),
'facebook' => $this->faker->randomNumber(5),
'created_at' => Carbon::now()->toDateTimeString(),
'updated_at' => Carbon::now()->toDateTimeString(),
];
}
}

View file

@ -0,0 +1,32 @@
<?php
namespace Database\Factories;
use App\Models\Media;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Carbon;
class MediaFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Media::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'path' => 'media/' . $this->faker->uuid . '.jpg',
'type' => 'image',
'created_at' => Carbon::now()->toDateTimeString(),
'updated_at' => Carbon::now()->toDateTimeString(),
];
}
}

View file

@ -3,6 +3,7 @@
namespace Database\Factories;
use App\Models\Note;
use Exception;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Carbon;
@ -19,10 +20,11 @@ class NoteFactory extends Factory
* Define the model's default state.
*
* @return array
* @throws Exception
*/
public function definition()
{
$now = Carbon::now()->subDays(rand(5, 15));
$now = Carbon::now()->subDays(random_int(5, 15));
return [
'note' => $this->faker->paragraph,

View file

@ -0,0 +1,31 @@
<?php
namespace Database\Factories;
use App\Models\WebMention;
use Illuminate\Database\Eloquent\Factories\Factory;
class WebMentionFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = WebMention::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'source' => $this->faker->url,
'target' => url('notes/1'),
'type' => 'reply',
'content' => $this->faker->paragraph,
];
}
}

13390
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -6,7 +6,7 @@
"license": "CC0-1.0",
"dependencies": {
"normalize.css": "^8.0.1",
"puppeteer": "^5.3.1",
"puppeteer": "^10.2.0",
"stylelint-a11y": "^1.2.3"
},
"devDependencies": {
@ -15,23 +15,21 @@
"autoprefixer": "^10.2.4",
"babel-loader": "^8.2.1",
"browserlist": "^1.0.1",
"compression-webpack-plugin": "^7.1.2",
"css-loader": "^5.0.0",
"cssnano": "^4.1.10",
"compression-webpack-plugin": "^8.0.1",
"css-loader": "^6.2.0",
"cssnano": "^5.0.2",
"eslint": "^7.13.0",
"eslint-webpack-plugin": "^2.3.0",
"husky": "^4.3.0",
"lint-staged": "^10.4.0",
"mini-css-extract-plugin": "^1.0.0",
"eslint-webpack-plugin": "^3.0.1",
"mini-css-extract-plugin": "^2.2.1",
"postcss": "^8.1.6",
"postcss-combine-duplicated-selectors": "^10.0.2",
"postcss-combine-media-query": "^1.0.1",
"postcss-import": "^14.0.0",
"postcss-loader": "^4.0.4",
"postcss-loader": "^6.1.1",
"pre-commit": "^1.1.3",
"stylelint": "^13.7.2",
"stylelint-config-standard": "^20.0.0",
"stylelint-webpack-plugin": "^2.1.1",
"stylelint-config-standard": "^22.0.0",
"stylelint-webpack-plugin": "^3.0.1",
"webpack": "^5.3.2",
"webpack-cli": "^4.0.0"
},
@ -46,19 +44,6 @@
"make:js": "npm run lint:es6 && npm run webpack && npm run uglifyjs",
"webpack": "webpack"
},
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"lint-staged": {
"./resources/es6/*.js": [
"eslint"
],
"*.scss": [
"stylelint --syntax=scss"
]
},
"browserslist": [
"last 2 versions",
"> 1%",

View file

@ -1,13 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
printerClass="NunoMaduro\Collision\Adapters\Phpunit\Printer"
processIsolation="false"
stopOnFailure="true"
>
<testsuites>
@ -21,6 +16,7 @@
</testsuites>
<php>
<env name="APP_ENV" value="testing"/>
<env name="DB_DATABASE" value="jbukdev_testing"/>
<env name="BCRYPT_ROUNDS" value="4"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="MAIL_DRIVER" value="array"/>

View file

@ -1,3 +1,6 @@
:root{--font-stack-body:"Whitney SSm A","Whitney SSm B",sans-serif;--font-stack-headings:"Quarto A","Quarto B",serif;--font-stack-monospace:"Operator Mono SSm A","Operator Mono SSm B",monospace;--color-background:#004643;--color-headline:#fffffe;--color-paragraph:#abd1c6;--color-button:#f9bc60;--color-button-text:#001e1d;--color-stroke:#001e1d;--color-main:#e8e4e6;--color-highlight:#f9bc60;--color-secondary:#abd1c6;--color-tertiary:#e16162}body{font-family:var(--font-stack-body);font-style:normal;font-weight:400;font-size:2rem;background-color:var(--color-background);color:var(--color-paragraph)}h1,h2,h3,h4,h5,h6{font-family:var(--font-stack-headings);font-style:normal;font-weight:800}code,pre{font-family:var(--font-stack-monospace);font-style:normal;font-weight:400}a{color:var(--color-highlight);text-decoration:none}.h-feed>.h-entry,.h-feed>.note{margin-top:4rem}body,main{display:-webkit-box;display:-ms-flexbox;display:flex;-webkit-box-orient:vertical;-webkit-box-direction:normal;-ms-flex-direction:column;flex-direction:column}main{margin:auto}main img{max-width:100%}main .h-entry:first-child>.bookmark-link{padding-top:2rem}.note{display:-webkit-box;display:-ms-flexbox;display:flex;-webkit-box-orient:vertical;-webkit-box-direction:normal;-ms-flex-direction:column;flex-direction:column}.note-metadata{display:-webkit-box;display:-ms-flexbox;display:flex;-webkit-box-orient:horizontal;-webkit-box-direction:normal;-ms-flex-direction:row;flex-direction:row;-webkit-box-pack:justify;-ms-flex-pack:justify;justify-content:space-between}.note .client{word-break:break-all}.note .syndication-links svg{height:1em;width:1em}.note>.e-content>.naked-link .u-photo{margin:2rem 0}article header>h1{margin-bottom:0}.post-info{font-size:1.4rem}.pagination{display:-webkit-box;display:-ms-flexbox;display:flex;-webkit-box-orient:horizontal;-webkit-box-direction:normal;-ms-flex-direction:row;flex-direction:row;-ms-flex-wrap:wrap;flex-wrap:wrap;-webkit-box-pack:space-evenly;-ms-flex-pack:space-evenly;justify-content:space-evenly;max-width:90vw;list-style-type:none}.personal-bio{padding:0 2rem}footer{display:-webkit-box;display:-ms-flexbox;display:flex;-webkit-box-orient:vertical;-webkit-box-direction:normal;-ms-flex-direction:column;flex-direction:column;-webkit-box-align:center;-ms-flex-align:center;align-items:center;margin-top:1.5rem}.iwc-logo{max-width:100%}#top-header{display:-webkit-box;display:-ms-flexbox;display:flex;-webkit-box-orient:vertical;-webkit-box-direction:normal;-ms-flex-direction:column;flex-direction:column;-webkit-box-pack:center;-ms-flex-pack:center;justify-content:center}#top-header h1{width:100%;text-align:center}nav{display:-webkit-box;display:-ms-flexbox;display:flex;-webkit-box-pack:center;-ms-flex-pack:center;justify-content:center;-ms-flex-wrap:wrap;flex-wrap:wrap}nav a{margin:0 .5rem}.post-info a,.syndication-links .u-syndication{text-decoration:none}.p-bridgy-facebook-content,.p-bridgy-twitter-content{display:none}@media screen and (max-width:699px){main{margin-left:5px;margin-right:5px}input{max-width:95vw}footer{margin-left:5px;margin-right:5px}}@media screen and (min-width:700px){main{max-width:700px}main>.h-entry,main>.note{padding:0 1rem}}
/*!***************************************************************************************************************************************************************************!*\
!*** css ./node_modules/css-loader/dist/cjs.js??ruleSet[1].rules[0].use[1]!./node_modules/postcss-loader/dist/cjs.js??ruleSet[1].rules[0].use[2]!./resources/css/app.css ***!
\***************************************************************************************************************************************************************************/
:root{--font-stack-body:"Whitney SSm A","Whitney SSm B",sans-serif;--font-stack-headings:"Quarto A","Quarto B",serif;--font-stack-monospace:"Operator Mono SSm A","Operator Mono SSm B",monospace;--color-background:#004643;--color-headline:#fffffe;--color-paragraph:#abd1c6;--color-button:#f9bc60;--color-button-text:#001e1d;--color-stroke:#001e1d;--color-main:#e8e4e6;--color-highlight:#f9bc60;--color-secondary:#abd1c6;--color-tertiary:#e16162}body{-webkit-box-orient:vertical;-webkit-box-direction:normal;background-color:var(--color-background);color:var(--color-paragraph);display:-webkit-box;display:-ms-flexbox;display:flex;-ms-flex-direction:column;flex-direction:column;font-family:var(--font-stack-body);font-size:2rem;font-style:normal;font-weight:400}h1,h2,h3,h4,h5,h6{font-family:var(--font-stack-headings);font-style:normal;font-weight:800}code,pre{font-family:var(--font-stack-monospace);font-style:normal;font-weight:400}a{color:var(--color-highlight);text-decoration:none}.h-feed>.h-entry,.h-feed>.note{margin-top:4rem}main{-webkit-box-orient:vertical;-webkit-box-direction:normal;display:-webkit-box;display:-ms-flexbox;display:flex;-ms-flex-direction:column;flex-direction:column;margin:auto}main img{max-width:100%}main .h-entry:first-child>.bookmark-link{padding-top:2rem}.note{-webkit-box-orient:vertical;-ms-flex-direction:column;flex-direction:column}.note,.note-metadata{-webkit-box-direction:normal;display:-webkit-box;display:-ms-flexbox;display:flex}.note-metadata{-webkit-box-orient:horizontal;-webkit-box-pack:justify;-ms-flex-pack:justify;-ms-flex-direction:row;flex-direction:row;justify-content:space-between}.note .client{word-break:break-all}.note .syndication-links svg{height:1em;width:1em}.note>.e-content>.naked-link .u-photo{margin:2rem 0}article header>h1{margin-bottom:0}.post-info{font-size:1.4rem}.pagination{-webkit-box-orient:horizontal;-webkit-box-direction:normal;-webkit-box-pack:space-evenly;-ms-flex-pack:space-evenly;display:-webkit-box;display:-ms-flexbox;display:flex;-ms-flex-direction:row;flex-direction:row;-ms-flex-wrap:wrap;flex-wrap:wrap;justify-content:space-evenly;list-style-type:none;max-width:90vw}.personal-bio{padding:0 2rem}footer{-webkit-box-orient:vertical;-webkit-box-direction:normal;-webkit-box-align:center;-ms-flex-align:center;align-items:center;display:-webkit-box;display:-ms-flexbox;display:flex;-ms-flex-direction:column;flex-direction:column;margin-top:1.5rem}.iwc-logo{max-width:100%}#top-header{-webkit-box-orient:vertical;-webkit-box-direction:normal;-webkit-box-pack:center;-ms-flex-pack:center;display:-webkit-box;display:-ms-flexbox;display:flex;-ms-flex-direction:column;flex-direction:column;justify-content:center}#top-header h1{text-align:center;width:100%}nav{-webkit-box-pack:center;-ms-flex-pack:center;display:-webkit-box;display:-ms-flexbox;display:flex;-ms-flex-wrap:wrap;flex-wrap:wrap;justify-content:center}nav a{margin:0 .5rem}.post-info a,.syndication-links .u-syndication{text-decoration:none}.p-bridgy-facebook-content,.p-bridgy-twitter-content{display:none}@media screen and (max-width:699px){main{margin-left:5px;margin-right:5px}input{max-width:95vw}footer{margin-left:5px;margin-right:5px}}@media screen and (min-width:700px){main{max-width:700px}main>.h-entry,main>.note{padding:0 1rem}}
/*# sourceMappingURL=app.css.map*/

Binary file not shown.

31
public/assets/app.js vendored
View file

@ -2,18 +2,6 @@
/******/ "use strict";
/******/ var __webpack_modules__ = ({
/***/ "./resources/js/app.js":
/*!*****************************!*\
!*** ./resources/js/app.js ***!
\*****************************/
/***/ (function(__unused_webpack_module, __webpack_exports__, __webpack_require__) {
__webpack_require__.r(__webpack_exports__);
/* harmony import */ var _css_app_css__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../css/app.css */ "./resources/css/app.css");
/***/ }),
/***/ "./resources/css/app.css":
/*!*******************************!*\
!*** ./resources/css/app.css ***!
@ -34,8 +22,9 @@ __webpack_require__.r(__webpack_exports__);
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/ // Check if module is in cache
/******/ if(__webpack_module_cache__[moduleId]) {
/******/ return __webpack_module_cache__[moduleId].exports;
/******/ var cachedModule = __webpack_module_cache__[moduleId];
/******/ if (cachedModule !== undefined) {
/******/ return cachedModule.exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = __webpack_module_cache__[moduleId] = {
@ -64,10 +53,16 @@ __webpack_require__.r(__webpack_exports__);
/******/ }();
/******/
/************************************************************************/
/******/ // startup
/******/ // Load entry module
/******/ __webpack_require__("./resources/js/app.js");
/******/ // This entry module used 'exports' so it can't be inlined
var __webpack_exports__ = {};
// This entry need to be wrapped in an IIFE because it need to be isolated against other modules in the chunk.
!function() {
/*!*****************************!*\
!*** ./resources/js/app.js ***!
\*****************************/
__webpack_require__.r(__webpack_exports__);
/* harmony import */ var _css_app_css__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../css/app.css */ "./resources/css/app.css");
}();
/******/ })()
;
//# sourceMappingURL=app.js.map

Binary file not shown.

View file

@ -3,28 +3,28 @@
@section('title')Edit Article « Admin CP « @stop
@section('content')
<form action="/admin/blog/{{ $id }}" method="post" accept-charset="utf-8" class="admin-form form">
<form action="/admin/blog/{{ $article->id }}" method="post" accept-charset="utf-8" class="admin-form form">
{{ csrf_field() }}
{{ method_field('PUT') }}
<div>
<label for="title">Title (URL):</label>
<input type="text" name="title" id="title" value="{!! $post['0']['title'] !!}">
<input type="url" name="url" id="url" value="{!! $post['0']['url'] !!}">
<input type="text" name="title" id="title" value="{!! $article->title !!}">
<input type="url" name="url" id="url" value="{!! $article->url !!}">
</div>
<div>
<label for="main">Main:</label>
<textarea name="main" id="main">{{ $post['0']['main'] }}</textarea>
<textarea name="main" id="main">{!! $article->main !!}</textarea>
</div>
<div class="form-row">
<label for="published">Published:</label>
<input type="checkbox" name="published" value="1"@if($post['0']['published'] == '1') checked="checked"@endif>
<input type="checkbox" name="published" value="1"@if($article->published == '1') checked="checked"@endif>
</div>
<div>
<button type="submit" name="save">Save</button>
</div>
</form>
<hr>
<form action="/admin/blog/{{ $id }}" method="post" class="admin-form form">
<form action="/admin/blog/{{ $article->id }}" method="post" class="admin-form form">
{{ csrf_field() }}
{{ method_field('DELETE') }}
<div>

View file

@ -65,7 +65,7 @@ Route::group(['domain' => config('url.longurl')], function () {
Route::get('/', [AdminArticlesController::class, 'index']);
Route::get('/create', [AdminArticlesController::class, 'create']);
Route::post('/', [AdminArticlesController::class, 'store']);
Route::get('/{id}/edit', [AdminArticlesController::class, 'edit']);
Route::get('/{article}/edit', [AdminArticlesController::class, 'edit']);
Route::put('/{id}', [AdminArticlesController::class, 'update']);
Route::delete('/{id}', [AdminArticlesController::class, 'destroy']);
});

View file

@ -1,18 +1,19 @@
<?php
declare(strict_types=1);
namespace Tests\Feature;
use Tests\TestCase;
use App\Models\Note;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class ActivityStreamTest extends TestCase
{
/**
* Test request to homepage returns data for site owner.
*
* @return void
*/
public function test_homepage_returns_data_for_site_owner()
use RefreshDatabase;
/** @test */
public function homepageRequestReturnsDataForSiteOwner(): void
{
$response = $this->get('/', ['Accept' => 'application/activity+json']);
$response->assertHeader('Content-Type', 'application/activity+json');
@ -24,15 +25,11 @@ class ActivityStreamTest extends TestCase
]);
}
/**
* Test request to a single note returns AS2.0 data.
*
* @return void
*/
public function test_single_note_returns_as_data()
/** @test */
public function requestForNoteIncludesActivityStreamData(): void
{
$note = \App\Models\Note::find(11);
$response = $this->get('/notes/B', ['Accept' => 'application/activity+json']);
$note = Note::factory()->create();
$response = $this->get($note->longurl, ['Accept' => 'application/activity+json']);
$response->assertHeader('Content-Type', 'application/activity+json');
$response->assertJson([
'@context' => 'https://www.w3.org/ns/activitystreams',

View file

@ -1,16 +1,19 @@
<?php
declare(strict_types=1);
namespace Tests\Feature\Admin;
use Tests\TestCase;
use App\Models\User;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class AdminHomeControllerTest extends TestCase
{
use DatabaseTransactions;
use RefreshDatabase;
public function test_admin_homepage()
/** @test */
public function adminHomepageLoads(): void
{
$user = User::factory()->make();

View file

@ -1,24 +1,29 @@
<?php
declare(strict_types=1);
namespace Tests\Feature\Admin;
use Tests\TestCase;
class AdminTest extends TestCase
{
public function test_admin_page_redirects_to_login()
/** @test */
public function adminPageRedirectsUnauthedUsersToLoginPage(): void
{
$response = $this->get('/admin');
$response->assertRedirect('/login');
}
public function test_login_page()
/** @test */
public function loginPageLoads(): void
{
$response = $this->get('/login');
$response->assertViewIs('login');
}
public function test_attempt_login_with_bad_credentials()
/** @test */
public function loginAttemptWithBadCredentialsFails(): void
{
$response = $this->post('/login', [
'username' => 'bad',

View file

@ -1,17 +1,22 @@
<?php
declare(strict_types=1);
namespace Tests\Feature\Admin;
use Tests\TestCase;
use App\Models\Article;
use App\Models\User;
use Faker\Factory;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\TestCase;
class ArticlesTest extends TestCase
{
use DatabaseTransactions;
use RefreshDatabase;
public function test_index_page()
/** @test */
public function adminArticlesPageLoads(): void
{
$user = User::factory()->make();
@ -20,7 +25,8 @@ class ArticlesTest extends TestCase
$response->assertSeeText('Select article to edit:');
}
public function test_create_page()
/** @test */
public function adminCanLoadFormToCreateArticle(): void
{
$user = User::factory()->make();
@ -29,7 +35,8 @@ class ArticlesTest extends TestCase
$response->assertSeeText('Title (URL)');
}
public function test_create_new_article()
/** @test */
public function admiNCanCreateNewArticle(): void
{
$user = User::factory()->make();
@ -41,10 +48,11 @@ class ArticlesTest extends TestCase
$this->assertDatabaseHas('articles', ['title' => 'Test Title']);
}
public function test_create_new_article_with_upload()
/** @test */
public function adminCanCreateNewArticleWithFile(): void
{
$user = User::factory()->make();
$faker = \Faker\Factory::create();
$faker = Factory::create();
$text = $faker->text;
if ($fh = fopen(sys_get_temp_dir() . '/article.md', 'w')) {
fwrite($fh, $text);
@ -65,21 +73,27 @@ class ArticlesTest extends TestCase
]);
}
public function test_see_edit_form()
/** @test */
public function articleCanLoadFormToEditArticle(): void
{
$user = User::factory()->make();
$article = Article::factory()->create([
'main' => 'This is *my* new blog. It uses `Markdown`.',
]);
$response = $this->actingAs($user)
->get('/admin/blog/1/edit');
->get('/admin/blog/' . $article->id . '/edit');
$response->assertSeeText('This is *my* new blog. It uses `Markdown`.');
}
public function test_edit_article()
/** @test */
public function adminCanEditArticle(): void
{
$user = User::factory()->make();
$article = Article::factory()->create();
$this->actingAs($user)
->post('/admin/blog/1', [
->post('/admin/blog/' . $article->id, [
'_method' => 'PUT',
'title' => 'My New Blog',
'main' => 'This article has been edited',
@ -90,16 +104,18 @@ class ArticlesTest extends TestCase
]);
}
public function test_delete_article()
/** @test */
public function adminCanDeleteArticle(): void
{
$user = User::factory()->make();
$article = Article::factory()->create();
$this->actingAs($user)
->post('/admin/blog/1', [
->post('/admin/blog/' . $article->id, [
'_method' => 'DELETE',
]);
$this->assertSoftDeleted('articles', [
'title' => 'My New Blog',
'title' => $article->title,
]);
}
}

View file

@ -1,16 +1,20 @@
<?php
declare(strict_types=1);
namespace Tests\Feature\Admin;
use Tests\TestCase;
use App\Models\MicropubClient;
use App\Models\User;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class ClientsTest extends TestCase
{
use DatabaseTransactions;
use RefreshDatabase;
public function test_index_page()
/** @test */
public function clientsPageLoads(): void
{
$user = User::factory()->make();
@ -19,7 +23,8 @@ class ClientsTest extends TestCase
$response->assertSeeText('Clients');
}
public function test_create_page()
/** @test */
public function adminCanLoadFormToCreateClient(): void
{
$user = User::factory()->make();
@ -28,7 +33,8 @@ class ClientsTest extends TestCase
$response->assertSeeText('New Client');
}
public function test_create_new_client()
/** @test */
public function adminCanCreateNewClient(): void
{
$user = User::factory()->make();
@ -43,21 +49,27 @@ class ClientsTest extends TestCase
]);
}
public function test_see_edit_form()
/** @test */
public function adminCanLoadEditFormForClient(): void
{
$user = User::factory()->make();
$client = MicropubClient::factory()->create([
'client_url' => 'https://jbl5.dev/notes/new',
]);
$response = $this->actingAs($user)
->get('/admin/clients/1/edit');
->get('/admin/clients/' . $client->id . '/edit');
$response->assertSee('https://jbl5.dev/notes/new');
}
public function test_edit_client()
/** @test */
public function adminCanEditClient(): void
{
$user = User::factory()->make();
$client = MicropubClient::factory()->create();
$this->actingAs($user)
->post('/admin/clients/1', [
->post('/admin/clients/' . $client->id, [
'_method' => 'PUT',
'client_url' => 'https://jbl5.dev/notes/new',
'client_name' => 'JBL5dev',
@ -68,12 +80,16 @@ class ClientsTest extends TestCase
]);
}
public function test_delete_client()
/** @test */
public function adminCanDeleteClient(): void
{
$user = User::factory()->make();
$client = MicropubClient::factory()->create([
'client_url' => 'https://jbl5.dev/notes/new',
]);
$this->actingAs($user)
->post('/admin/clients/1', [
->post('/admin/clients/' . $client->id, [
'_method' => 'DELETE',
]);
$this->assertDatabaseMissing('clients', [

View file

@ -1,20 +1,22 @@
<?php
declare(strict_types=1);
namespace Tests\Feature\Admin;
use Tests\TestCase;
use App\Models\User;
use GuzzleHttp\Client;
use App\Models\Contact;
use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use GuzzleHttp\Handler\MockHandler;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\TestCase;
class ContactsTest extends TestCase
{
use DatabaseTransactions;
use RefreshDatabase;
protected function tearDown(): void
{
@ -25,7 +27,8 @@ class ContactsTest extends TestCase
parent::tearDown();
}
public function test_index_page()
/** @test */
public function contactIndexPageLoads(): void
{
$user = User::factory()->make();
@ -33,7 +36,8 @@ class ContactsTest extends TestCase
$response->assertViewIs('admin.contacts.index');
}
public function test_create_page()
/** @test */
public function contactCreatePageLoads(): void
{
$user = User::factory()->make();
@ -41,7 +45,8 @@ class ContactsTest extends TestCase
$response->assertViewIs('admin.contacts.create');
}
public function test_create_new_contact()
/** @test */
public function adminCanCreateNewContact(): void
{
$user = User::factory()->make();
@ -57,19 +62,23 @@ class ContactsTest extends TestCase
]);
}
public function test_see_edit_form()
/** @test */
public function adminCanSeeFormToEditContact(): void
{
$user = User::factory()->make();
$contact = Contact::factory()->create();
$response = $this->actingAs($user)->get('/admin/contacts/1/edit');
$response = $this->actingAs($user)->get('/admin/contacts/' . $contact->id . '/edit');
$response->assertViewIs('admin.contacts.edit');
}
public function test_update_contact_no_uploaded_avatar()
/** @test */
public function adminCanUpdateContact(): void
{
$user = User::factory()->make();
$contact = Contact::factory()->create();
$this->actingAs($user)->post('/admin/contacts/1', [
$this->actingAs($user)->post('/admin/contacts/' . $contact->id, [
'_method' => 'PUT',
'name' => 'Tantek Celik',
'nick' => 'tantek',
@ -82,14 +91,16 @@ class ContactsTest extends TestCase
]);
}
public function test_edit_contact_with_uploaded_avatar()
/** @test */
public function adminCanEditContactAndUploadAvatar(): void
{
copy(__DIR__ . '/../../aaron.png', sys_get_temp_dir() . '/tantek.png');
$path = sys_get_temp_dir() . '/tantek.png';
$file = new UploadedFile($path, 'tantek.png', 'image/png', null, true);
$user = User::factory()->make();
$contact = Contact::factory()->create();
$this->actingAs($user)->post('/admin/contacts/1', [
$this->actingAs($user)->post('/admin/contacts/' . $contact->id, [
'_method' => 'PUT',
'name' => 'Tantek Celik',
'nick' => 'tantek',
@ -103,11 +114,17 @@ class ContactsTest extends TestCase
);
}
public function test_delete_contact()
/** @test */
public function adminCanDeleteContact(): void
{
$user = User::factory()->make();
$contact = Contact::factory()->create(['nick' => 'tantek']);
$this->actingAs($user)->post('/admin/contacts/1', [
$this->assertDatabaseHas('contacts', [
'nick' => 'tantek',
]);
$this->actingAs($user)->post('/admin/contacts/' . $contact->id, [
'_method' => 'DELETE',
]);
$this->assertDatabaseMissing('contacts', [
@ -115,14 +132,15 @@ class ContactsTest extends TestCase
]);
}
public function test_get_avatar_method()
/** @test */
public function adminCanTriggerRetrievalOfRemoteAvatar(): void
{
$html = <<<HTML
<div class="h-card">
<img class="u-photo" src="http://tantek.com/tantek.png">
</div>
HTML;
$file = fopen(__DIR__ . '/../../aaron.png', 'r');
<div class="h-card">
<img class="u-photo" alt="" src="http://tantek.com/tantek.png">
</div>
HTML;
$file = fopen(__DIR__ . '/../../aaron.png', 'rb');
$mock = new MockHandler([
new Response(200, ['Content-Type' => 'text/html'], $html),
new Response(200, ['Content-Type' => 'iamge/png'], $file),
@ -131,8 +149,11 @@ HTML;
$client = new Client(['handler' => $handler]);
$this->app->instance(Client::class, $client);
$user = User::factory()->make();
$contact = Contact::factory()->create([
'homepage' => 'https://tantek.com',
]);
$this->actingAs($user)->get('/admin/contacts/1/getavatar');
$this->actingAs($user)->get('/admin/contacts/' . $contact->id . '/getavatar');
$this->assertFileEquals(
__DIR__ . '/../../aaron.png',
@ -140,7 +161,8 @@ HTML;
);
}
public function test_get_avatar_method_redirects_with_failed_homepage()
/** @test */
public function gettingRemoteAvatarFailsGracefullyWithRemoteNotFound(): void
{
$mock = new MockHandler([
new Response(404),
@ -149,19 +171,21 @@ HTML;
$client = new Client(['handler' => $handler]);
$this->app->instance(Client::class, $client);
$user = User::factory()->make();
$contact = Contact::factory()->create();
$response = $this->actingAs($user)->get('/admin/contacts/1/getavatar');
$response = $this->actingAs($user)->get('/admin/contacts/' . $contact->id . '/getavatar');
$response->assertRedirect('/admin/contacts/1/edit');
$response->assertRedirect('/admin/contacts/' . $contact->id . '/edit');
}
public function test_get_avatar_method_redirects_with_failed_avatar_download()
/** @test */
public function gettingRemoteAvatarFailsGracefullyWithRemoteError(): void
{
$html = <<<HTML
<div class="h-card">
<img class="u-photo" src="http://tantek.com/tantek.png">
</div>
HTML;
<div class="h-card">
<img class="u-photo" src="http://tantek.com/tantek.png">
</div>
HTML;
$mock = new MockHandler([
new Response(200, ['Content-Type' => 'text/html'], $html),
new Response(404),
@ -170,13 +194,15 @@ HTML;
$client = new Client(['handler' => $handler]);
$this->app->instance(Client::class, $client);
$user = User::factory()->make();
$contact = Contact::factory()->create();
$response = $this->actingAs($user)->get('/admin/contacts/1/getavatar');
$response = $this->actingAs($user)->get('/admin/contacts/' . $contact->id . '/getavatar');
$response->assertRedirect('/admin/contacts/1/edit');
$response->assertRedirect('/admin/contacts/' . $contact->id . '/edit');
}
public function test_get_avatar_for_contact_with_no_homepage()
/** @test */
public function gettingRemoteAvatarFailsGracefullyForContactWithNoHompage(): void
{
$contact = Contact::create([
'nick' => 'fred',

View file

@ -1,19 +1,22 @@
<?php
declare(strict_types=1);
namespace Tests\Feature\Admin;
use App\Models\User;
use Tests\TestCase;
use App\Models\Like;
use App\Jobs\ProcessLike;
use App\Models\Like;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Queue;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\TestCase;
class LikesTest extends TestCase
{
use DatabaseTransactions;
use RefreshDatabase;
public function test_index_page()
/** @test */
public function likesPageLoads(): void
{
$user = User::factory()->make();
@ -22,7 +25,8 @@ class LikesTest extends TestCase
$response->assertSeeText('Likes');
}
public function test_create_page()
/** @test */
public function likeCreateFormLoads(): void
{
$user = User::factory()->make();
@ -31,7 +35,8 @@ class LikesTest extends TestCase
$response->assertSeeText('New Like');
}
public function test_create_new_like()
/** @test */
public function adminCanCreateLike(): void
{
Queue::fake();
$user = User::factory()->make();
@ -46,22 +51,26 @@ class LikesTest extends TestCase
Queue::assertPushed(ProcessLike::class);
}
public function test_see_edit_form()
/** @test */
public function likeEditFormLoads(): void
{
$user = User::factory()->make();
$like = Like::factory()->create();
$response = $this->actingAs($user)
->get('/admin/likes/1/edit');
->get('/admin/likes/' . $like->id . '/edit');
$response->assertSee('Edit Like');
}
public function test_edit_like()
/** @test */
public function adminCanEditLike(): void
{
Queue::fake();
$user = User::factory()->make();
$like = Like::factory()->create();
$this->actingAs($user)
->post('/admin/likes/1', [
->post('/admin/likes/' . $like->id, [
'_method' => 'PUT',
'like_url' => 'https://example.com',
]);
@ -71,14 +80,15 @@ class LikesTest extends TestCase
Queue::assertPushed(ProcessLike::class);
}
public function test_delete_like()
/** @test */
public function adminCanDeleteLike(): void
{
$like = Like::find(1);
$like = Like::factory()->create();
$url = $like->url;
$user = User::factory()->make();
$this->actingAs($user)
->post('/admin/likes/1', [
->post('/admin/likes/' . $like->id, [
'_method' => 'DELETE',
]);
$this->assertDatabaseMissing('likes', [

View file

@ -1,18 +1,22 @@
<?php
declare(strict_types=1);
namespace Tests\Feature\Admin;
use App\Models\User;
use Tests\TestCase;
use App\Jobs\SendWebMentions;
use App\Models\Note;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Queue;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\TestCase;
class NotesTest extends TestCase
{
use DatabaseTransactions;
use RefreshDatabase;
public function test_index_page()
/** @test */
public function notesPageLoads(): void
{
$user = User::factory()->make();
@ -20,7 +24,8 @@ class NotesTest extends TestCase
$response->assertViewIs('admin.notes.index');
}
public function test_create_page()
/** @test */
public function noteCreatePageLoads(): void
{
$user = User::factory()->make();
@ -28,7 +33,8 @@ class NotesTest extends TestCase
$response->assertViewIs('admin.notes.create');
}
public function test_create_a_new_note()
/** @test */
public function adminCanCreateNewNote(): void
{
$user = User::factory()->make();
@ -40,20 +46,24 @@ class NotesTest extends TestCase
]);
}
public function test_edit_page()
/** @test */
public function noteEditFormLoads(): void
{
$user = User::factory()->make();
$note = Note::factory()->create();
$response = $this->actingAs($user)->get('/admin/notes/1/edit');
$response = $this->actingAs($user)->get('/admin/notes/' . $note->id . '/edit');
$response->assertViewIs('admin.notes.edit');
}
public function test_edit_a_note()
/** @test */
public function adminCanEditNote(): void
{
Queue::fake();
$user = User::factory()->make();
$note = Note::factory()->create();
$this->actingAs($user)->post('/admin/notes/1', [
$this->actingAs($user)->post('/admin/notes/' . $note->id, [
'_method' => 'PUT',
'content' => 'An edited note',
'webmentions' => true,
@ -65,15 +75,17 @@ class NotesTest extends TestCase
Queue::assertPushed(SendWebMentions::class);
}
public function test_delete_note()
/** @test */
public function adminCanDeleteNote(): void
{
$user = User::factory()->make();
$note = Note::factory()->create();
$this->actingAs($user)->post('/admin/notes/1', [
$this->actingAs($user)->post('/admin/notes/' . $note->id, [
'_method' => 'DELETE',
]);
$this->assertSoftDeleted('notes', [
'id' => '1',
'id' => $note->id,
]);
}
}

View file

@ -1,16 +1,20 @@
<?php
declare(strict_types=1);
namespace Tests\Feature\Admin;
use App\Models\Place;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class PlacesTest extends TestCase
{
use DatabaseTransactions;
use RefreshDatabase;
public function test_index_page()
/** @test */
public function placesPageLoads(): void
{
$user = User::factory()->make();
@ -18,7 +22,8 @@ class PlacesTest extends TestCase
$response->assertViewIs('admin.places.index');
}
public function test_create_page()
/** @test */
public function createPlacePageLoads(): void
{
$user = User::factory()->make();
@ -26,7 +31,8 @@ class PlacesTest extends TestCase
$response->assertViewIs('admin.places.create');
}
public function test_create_new_place()
/** @test */
public function adminCanCreateNewPlace(): void
{
$user = User::factory()->make();
@ -42,19 +48,25 @@ class PlacesTest extends TestCase
]);
}
public function test_edit_page()
/** @test */
public function editPlacePageLoads(): void
{
$user = User::factory()->make();
$place = Place::factory()->create();
$response = $this->actingAs($user)->get('/admin/places/1/edit');
$response = $this->actingAs($user)->get('/admin/places/' . $place->id . '/edit');
$response->assertViewIs('admin.places.edit');
}
public function test_updating_a_place()
/** @test */
public function adminCanUpdatePlace(): void
{
$user = User::factory()->make();
$place = Place::factory()->create([
'name' => 'The Bridgewater Pub',
]);
$this->actingAs($user)->post('/admin/places/1', [
$this->actingAs($user)->post('/admin/places/' . $place->id, [
'_method' => 'PUT',
'name' => 'The Bridgewater',
'description' => 'Who uses “Pub” anyway',

View file

@ -1,45 +1,59 @@
<?php
declare(strict_types=1);
namespace Tests\Feature;
use Tests\TestCase;
use App\Models\Article;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Jonnybarnes\IndieWeb\Numbers;
use Tests\TestCase;
class ArticlesTest extends TestCase
{
public function test_articles_page()
use RefreshDatabase;
/** @test */
public function articlesPageLoads(): void
{
$response = $this->get('/blog');
$response->assertViewIs('articles.index');
}
public function test_single_article()
/** @test */
public function singleArticlePageLoads()
{
$response = $this->get('/blog/' . date('Y') . '/' . date('m') . '/some-code-i-did');
$article = Article::factory()->create();
$response = $this->get($article->link);
$response->assertViewIs('articles.show');
}
public function test_wrong_date_redirects()
/** @test */
public function wrongDateInUrlRedirectsToCorrectDate()
{
$response = $this->get('/blog/1900/01/some-code-i-did');
$response->assertRedirect('/blog/' . date('Y') . '/' . date('m') . '/some-code-i-did');
$article = Article::factory()->create();
$response = $this->get('/blog/1900/01/' . $article->titleurl);
$response->assertRedirect('/blog/' . date('Y') . '/' . date('m') . '/' . $article->titleurl);
}
public function test_redirect_for_id()
/** @test */
public function oldUrlsWithIdAreRedirected()
{
$response = $this->get('/blog/s/2');
$response->assertRedirect('/blog/' . date('Y') . '/' . date('m') . '/some-code-i-did');
$article = Article::factory()->create();
$num60Id = resolve(Numbers::class)->numto60($article->id);
$response = $this->get('/blog/s/' . $num60Id);
$response->assertRedirect($article->link);
}
/** @test */
public function unknownSlugGives404()
public function unknownSlugGetsNotFoundResponse()
{
$response = $this->get('/blog/' . date('Y') . '/' . date('m') . '/unknown-slug');
$response->assertNotFound();
}
/** @test */
public function unknownArticleIdGives404()
public function unknownArticleIdGetsNotFoundResponse()
{
$response = $this->get('/blog/s/22');
$response->assertNotFound();

View file

@ -1,31 +1,38 @@
<?php
declare(strict_types=1);
namespace Tests\Feature;
use App\Jobs\ProcessBookmark;
use App\Jobs\SyndicateBookmarkToTwitter;
use App\Models\Bookmark;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Queue;
use Tests\TestCase;
use Tests\TestToken;
use App\Jobs\ProcessBookmark;
use Illuminate\Support\Facades\Queue;
use App\Jobs\SyndicateBookmarkToTwitter;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class BookmarksTest extends TestCase
{
use DatabaseTransactions, TestToken;
use RefreshDatabase, TestToken;
public function test_bookmarks_page()
/** @test */
public function bookmarksPageLoadsWithoutError(): void
{
$response = $this->get('/bookmarks');
$response->assertViewIs('bookmarks.index');
}
public function test_single_bookmark_page()
/** @test */
public function singleBookmarkPageLoadsWithoutError(): void
{
$response = $this->get('/bookmarks/1');
$bookmark = Bookmark::factory()->create();
$response = $this->get('/bookmarks/' . $bookmark->id);
$response->assertViewIs('bookmarks.show');
}
public function test_browsershot_job_dispatches_when_bookmark_added_http_post_syntax()
/** @test */
public function whenBookmarkIsAddedUsingHttpSyntaxCheckJobToTakeScreenshotIsInvoked(): void
{
Queue::fake();
@ -46,7 +53,8 @@ class BookmarksTest extends TestCase
$this->assertDatabaseHas('bookmarks', ['url' => 'https://example.org/blog-post']);
}
public function test_browsershot_job_dispatches_when_bookmark_added_json_syntax()
/** @test */
public function whenBookmarkIsAddedUsingJsonSyntaxCheckJobToTakeScreenshotIsInvoked(): void
{
Queue::fake();
@ -69,7 +77,8 @@ class BookmarksTest extends TestCase
$this->assertDatabaseHas('bookmarks', ['url' => 'https://example.org/blog-post']);
}
public function test_single_twitter_syndication_target_causes_job_dispatch_http_post_syntax()
/** @test */
public function whenTheBookmarkIsMarkedForPostingToTwitterCheckWeInvokeTheCorrectJob(): void
{
Queue::fake();
@ -88,7 +97,8 @@ class BookmarksTest extends TestCase
$this->assertDatabaseHas('bookmarks', ['url' => 'https://example.org/blog-post']);
}
public function test_tags_created_with_new_bookmark()
/** @test */
public function whenTheBookmarkIsCreatedCheckNecessaryTagsAreAlsoCreated(): void
{
Queue::fake();

View file

@ -1,16 +1,30 @@
<?php
declare(strict_types=1);
namespace Tests\Feature;
use App\Models\Contact;
use App\Models\Note;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class BridgyPosseTest extends TestCase
{
public function test_bridgy_twitter_content()
use RefreshDatabase;
/** @test */
public function notesWeWantCopiedToTwitterShouldHaveNecessaryMarkup(): void
{
$response = $this->get('/notes/4');
Contact::factory()->create([
'nick' => 'joe',
'twitter' => 'joe__',
]);
$note = Note::factory()->create(['note' => 'Hi @joe']);
$response = $this->get($note->longurl);
$html = $response->content();
$this->assertTrue(is_string(mb_stristr($html, 'p-bridgy-twitter-content')));
$this->assertStringContainsString('p-bridgy-twitter-content', $html);
}
}

View file

@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace Tests\Feature;
use Tests\TestCase;
@ -7,9 +9,9 @@ use Tests\TestCase;
class CSPHeadersTest extends TestCase
{
/** @test */
public function check_csp_headers_test()
public function checkCspHeadersArePresent(): void
{
$response = $this->get('/');
$response = $this->get('/blog');
$response->assertHeader('Content-Security-Policy');
$response->assertHeader('Report-To');
}

View file

@ -1,17 +1,23 @@
<?php
declare(strict_types=1);
namespace Tests\Feature;
use App\Models\Contact;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class ContactsTest extends TestCase
{
use RefreshDatabase;
/**
* Check the `/contacts` page gives a good response.
*
* @return void
* @test
*/
public function test_contacts_page()
public function contactsPageLoadsWithoutError(): void
{
$response = $this->get('/contacts');
$response->assertStatus(200);
@ -20,10 +26,13 @@ class ContactsTest extends TestCase
/**
* Test an individual contact page with default profile image.
*
* @return void
* @test
*/
public function test_contact_page_with_default_pic()
public function contactPageShouldFallbackToDefaultProfilePic(): void
{
Contact::factory()->create([
'nick' => 'tantek',
]);
$response = $this->get('/contacts/tantek');
$response->assertViewHas('image', '/assets/profile-images/default-image');
}
@ -31,16 +40,20 @@ class ContactsTest extends TestCase
/**
* Test an individual contact page with a specific profile image.
*
* @return void
* @test
*/
public function test_contact_page_with_specific_pic()
public function contactPageShouldUseSpecificProfilePicIfPresent(): void
{
Contact::factory()->create([
'nick' => 'aaron',
'homepage' => 'https://aaronparecki.com',
]);
$response = $this->get('/contacts/aaron');
$response->assertViewHas('image', '/assets/profile-images/aaronparecki.com/image');
}
/** @test */
public function unknownContactGives404()
public function unknownContactReturnsNotFoundResponse(): void
{
$response = $this->get('/contacts/unknown');
$response->assertNotFound();

View file

@ -1,18 +1,18 @@
<?php
declare(strict_types=1);
namespace Tests\Feature;
use Tests\TestCase;
use Tests\TestToken;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
class CorsHeadersTest extends TestCase
{
use TestToken;
/** @test */
public function check_cors_headers_on_media_endpoint_options_request()
public function checkCorsHeadersOnMediaEndpoint(): void
{
$response = $this->call(
'OPTIONS',
@ -26,9 +26,9 @@ class CorsHeadersTest extends TestCase
}
/** @test */
public function check_missing_on_other_route()
public function checkForNoCorsHeaderOnNonMediaEndpointLinks(): void
{
$response = $this->get('/');
$response = $this->get('/blog');
$response->assertHeaderMissing('Access-Control-Allow-Origin');
}
}

View file

@ -1,47 +1,61 @@
<?php
declare(strict_types=1);
namespace Tests\Feature;
use App\Models\Article;
use App\Models\Note;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class FeedsTest extends TestCase
{
use RefreshDatabase;
/**
* Test the blog RSS feed.
*
* @return void
* @test
*/
public function test_blog_rss_feed()
public function blogRssFeedIsPresent(): void
{
Article::factory()->count(3)->create();
$response = $this->get('/blog/feed.rss');
$response->assertHeader('Content-Type', 'application/rss+xml; charset=utf-8');
$response->assertOk();
}
/**
* Test the notes RSS feed.
*
* @return void
* @test
*/
public function test_notes_rss_feed()
public function notesRssFeedIsPresent(): void
{
Note::factory()->count(3)->create();
$response = $this->get('/notes/feed.rss');
$response->assertHeader('Content-Type', 'application/rss+xml; charset=utf-8');
$response->assertOk();
}
/**
* Test the blog RSS feed.
*
* @return void
* @test
*/
public function test_blog_atom_feed()
public function blogAtomFeedIsPresent(): void
{
Article::factory()->count(3)->create();
$response = $this->get('/blog/feed.atom');
$response->assertHeader('Content-Type', 'application/atom+xml; charset=utf-8');
$response->assertOk();
}
/** @test */
public function blog_jf2_feed()
public function blogJf2FeedIsPresent(): void
{
Article::factory()->count(3)->create();
$response = $this->get('/blog/feed.jf2');
$response->assertHeader('Content-Type', 'application/jf2feed+json');
$response->assertJson([
@ -63,39 +77,46 @@ class FeedsTest extends TestCase
/**
* Test the notes RSS feed.
*
* @return void
* @test
*/
public function test_notes_atom_feed()
public function notesAtomFeedIsPresent(): void
{
Note::factory()->count(3)->create();
$response = $this->get('/notes/feed.atom');
$response->assertHeader('Content-Type', 'application/atom+xml; charset=utf-8');
$response->assertOk();
}
/**
* Test the blog JSON feed.
*
* @return void
* @test
*/
public function test_blog_json_feed()
public function blogJsonFeedIsPresent(): void
{
Article::factory()->count(3)->create();
$response = $this->get('/blog/feed.json');
$response->assertHeader('Content-Type', 'application/json');
$response->assertOk();
}
/**
* Test the notes JSON feed.
*
* @return void
* @test
*/
public function test_notes_json_feed()
public function notesJsonFeedIsPresent(): void
{
Note::factory()->count(3)->create();
$response = $this->get('/notes/feed.json');
$response->assertHeader('Content-Type', 'application/json');
$response->assertOk();
}
/** @test */
public function notes_jf2_feed()
public function notesJf2FeedIsPresent(): void
{
Note::factory()->count(3)->create();
$response = $this->get('/notes/feed.jf2');
$response->assertHeader('Content-Type', 'application/jf2feed+json');
$response->assertJson([
@ -118,10 +139,11 @@ class FeedsTest extends TestCase
* Each JSON feed item must have one of `content_text` or `content_html`,
* and whichever one they have cant be `null`.
*
* @return void
* @test
*/
public function test_json_feed_has_one_content_attribute_and_it_isnt_null()
public function jsonFeedsHaveRequiredAttributes(): void
{
Note::factory()->count(3)->create();
$response = $this->get('/notes/feed.json');
$data = json_decode($response->content());
foreach ($data->items as $item) {

View file

@ -1,40 +1,44 @@
<?php
declare(strict_types=1);
namespace Tests\Feature;
use Codebird\Codebird;
use Queue;
use stdClass;
use Tests\TestCase;
use App\Models\Like;
use Tests\TestToken;
use GuzzleHttp\Client;
use App\Jobs\ProcessLike;
use Lcobucci\JWT\Builder;
use App\Models\Like;
use Codebird\Codebird;
use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Handler\MockHandler;
use Lcobucci\JWT\Signer\Hmac\Sha256;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Queue;
use Jonnybarnes\WebmentionsParser\Authorship;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\TestCase;
use Tests\TestToken;
class LikesTest extends TestCase
{
use DatabaseTransactions, TestToken;
use RefreshDatabase;
use TestToken;
public function test_likes_page()
/** @test */
public function likesPageHasCorrectView(): void
{
$response = $this->get('/likes');
$response->assertViewIs('likes.index');
}
public function test_single_like_page()
/** @test */
public function singleLikePageHasCorrectView(): void
{
$response = $this->get('/likes/1');
$like = Like::factory()->create();
$response = $this->get('/likes/' . $like->id);
$response->assertViewIs('likes.show');
}
public function test_like_micropub_json_request()
/** @test */
public function checkLikeCreatedFromMicropubApiRequests(): void
{
Queue::fake();
@ -53,7 +57,8 @@ class LikesTest extends TestCase
$this->assertDatabaseHas('likes', ['url' => 'https://example.org/blog-post']);
}
public function test_like_micropub_form_request()
/** @test */
public function checkLikeCreatedFromMicropubWebRequests(): void
{
Queue::fake();
@ -70,7 +75,8 @@ class LikesTest extends TestCase
$this->assertDatabaseHas('likes', ['url' => 'https://example.org/blog-post']);
}
public function test_process_like_job_with_simple_author()
/** @test */
public function likeWithSimpleAuthor(): void
{
$like = new Like();
$like->url = 'http://example.org/note/id';
@ -80,17 +86,18 @@ class LikesTest extends TestCase
$job = new ProcessLike($like);
$content = <<<END
<html>
<body>
<div class="h-entry">
<div class="e-content">
A post that I like.
</div>
by <span class="p-author">Fred Bloggs</span>
</div>
</body>
</html>
END;
<html>
<body>
<div class="h-entry">
<div class="e-content">
A post that I like.
</div>
by <span class="p-author">Fred Bloggs</span>
</div>
</body>
</html>
END;
$mock = new MockHandler([
new Response(200, [], $content),
new Response(200, [], $content),
@ -107,7 +114,8 @@ END;
$this->assertEquals('Fred Bloggs', Like::find($id)->author_name);
}
public function test_process_like_job_with_h_card()
/** @test */
public function likeWithHCard(): void
{
$like = new Like();
$like->url = 'http://example.org/note/id';
@ -117,21 +125,22 @@ END;
$job = new ProcessLike($like);
$content = <<<END
<html>
<body>
<div class="h-entry">
<div class="e-content">
A post that I like.
</div>
by
<div class="p-author h-card">
<span class="p-name">Fred Bloggs</span>
<a class="u-url" href="https://fredd.blog/gs"></a>
</div>
</div>
</body>
</html>
END;
<html>
<body>
<div class="h-entry">
<div class="e-content">
A post that I like.
</div>
by
<div class="p-author h-card">
<span class="p-name">Fred Bloggs</span>
<a class="u-url" href="https://fredd.blog/gs"></a>
</div>
</div>
</body>
</html>
END;
$mock = new MockHandler([
new Response(200, [], $content),
new Response(200, [], $content),
@ -148,7 +157,8 @@ END;
$this->assertEquals('Fred Bloggs', Like::find($id)->author_name);
}
public function test_process_like_job_without_mf2()
/** @test */
public function likeWithoutMicroformats(): void
{
$like = new Like();
$like->url = 'http://example.org/note/id';
@ -158,14 +168,15 @@ END;
$job = new ProcessLike($like);
$content = <<<END
<html>
<body>
<div>
I liked a post
</div>
</body>
</html>
END;
<html>
<body>
<div>
I liked a post
</div>
</body>
</html>
END;
$mock = new MockHandler([
new Response(200, [], $content),
new Response(200, [], $content),
@ -182,7 +193,8 @@ END;
$this->assertNull(Like::find($id)->author_name);
}
public function test_process_like_that_is_tweet()
/** @test */
public function likeThatIsATweet(): void
{
$like = new Like();
$like->url = 'https://twitter.com/jonnybarnes/status/1050823255123251200';
@ -202,10 +214,11 @@ END;
return $client;
});
$info = new stdClass();
$info->author_name = 'Jonny Barnes';
$info->author_url = 'https://twitter.com/jonnybarnes';
$info->html = '<div>HTML of the tweet embed</div>';
$info = (object) [
'author_name' => 'Jonny Barnes',
'author_url' => 'https://twitter.com/jonnybarnes',
'html' => '<div>HTML of the tweet embed</div>',
];
$codebirdMock = $this->getMockBuilder(Codebird::class)
->addMethods(['statuses_oembed'])
->getMock();
@ -221,7 +234,7 @@ END;
}
/** @test */
public function unknownLikeGives404()
public function unknownLikeGivesNotFoundResponse(): void
{
$response = $this->get('/likes/202');
$response->assertNotFound();

View file

@ -1,41 +1,32 @@
<?php
declare(strict_types=1);
namespace Tests\Feature;
use Faker\Factory;
use Illuminate\Foundation\Testing\RefreshDatabase;
use App\Jobs\{SendWebMentions, SyndicateNoteToTwitter};
use App\Models\{Media, Note, Place};
use Carbon\Carbon;
use Tests\TestCase;
use Tests\TestToken;
use App\Jobs\SendWebMentions;
use App\Models\{Media, Place};
use App\Jobs\SyndicateNoteToTwitter;
use Illuminate\Support\Facades\Queue;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\{TestCase, TestToken};
class MicropubControllerTest extends TestCase
{
use DatabaseTransactions;
use RefreshDatabase;
use TestToken;
/**
* Test a GET request for the micropub endpoint without a token gives a
* 400 response. Also check the error message.
*
* @return void
*/
public function test_micropub_get_request_without_token_returns_401_response()
/** @test */
public function micropubGetRequestWithoutTokenReturnsErrorResponse(): void
{
$response = $this->get('/api/post');
$response->assertStatus(401);
$response->assertJsonFragment(['error_description' => 'No access token was provided in the request']);
}
/**
* Test a GET request for the micropub endpoint without a valid token gives
* a 400 response. Also check the error message.
*
* @return void
*/
public function test_micropub_get_request_without_valid_token_returns_400_response()
/** @test */
public function micropubGetRequestWithoutValidTokenReturnsErrorResponse(): void
{
$response = $this->get('/api/post', ['HTTP_Authorization' => 'Bearer abc123']);
$response->assertStatus(400);
@ -44,80 +35,64 @@ class MicropubControllerTest extends TestCase
/**
* Test a GET request for the micropub endpoint with a valid token gives a
* 200 response. Check token information is returned in the response.
* 200 response. Check token information is also returned in the response.
*
* @return void
* @test
*/
public function test_micropub_get_request_with_valid_token_returns_200_response()
public function micropubGetRequestWithValidTokenReturnsOkResponse(): void
{
$response = $this->get('/api/post', ['HTTP_Authorization' => 'Bearer ' . $this->getToken()]);
$response->assertStatus(200);
$response->assertJsonFragment(['response' => 'token']);
}
/**
* Test a GET request for syndication targets.
*
* @return void
*/
public function test_micropub_get_request_for_syndication_targets()
/** @test */
public function micropubClientsCanRequestSyndicationTargets(): void
{
$response = $this->get('/api/post?q=syndicate-to', ['HTTP_Authorization' => 'Bearer ' . $this->getToken()]);
$response->assertJsonFragment(['uid' => 'https://twitter.com/jonnybarnes']);
}
/**
* Test a request for places.
*
* @return void
*/
public function test_micropub_get_request_for_nearby_places()
{
$response = $this->get('/api/post?q=geo:53.5,-2.38', ['HTTP_Authorization' => 'Bearer ' . $this->getToken()]);
$response->assertJson(['places' => [['slug' =>'the-bridgewater-pub']]]);
}
/**
* Test a request for places, this time with an uncertainty parameter.
*
* @return void
*/
public function test_micropub_get_request_for_nearby_places_with_uncertainty_parameter()
/** @test */
public function micropubClientsCanRequestKnownNearbyPlaces(): void
{
Place::factory()->create([
'name' => 'The Bridgewater Pub',
'latitude' => '53.5',
'longitude' => '-2.38',
]);
$response = $this->get('/api/post?q=geo:53.5,-2.38', ['HTTP_Authorization' => 'Bearer ' . $this->getToken()]);
$response->assertJson(['places' => [['slug' => 'the-bridgewater-pub']]]);
}
/**
* Test a request for places, where there will be an “empty” response.
* @test
* @todo Add uncertainty parameter
*
* @return void
*/
public function test_micropub_get_request_for_nearby_places_where_non_exist()
public function micropubClientsCanRequestKnownNearbyPlacesWithUncertaintyParameter(): void
{
$response = $this->get('/api/post?q=geo:53.5,-2.38', ['HTTP_Authorization' => 'Bearer ' . $this->getToken()]);
$response->assertJson(['places' => [['slug' => 'the-bridgewater-pub']]]);
}*/
/** @test */
public function returnEmptyResultWhenMicropubClientRequestsKnownNearbyPlaces(): void
{
$response = $this->get('/api/post?q=geo:1.23,4.56', ['HTTP_Authorization' => 'Bearer ' . $this->getToken()]);
$response->assertJson(['places' => []]);
}
/**
* Test a request for the micropub config.
*
* @return void
*/
public function test_micropub_get_request_for_config()
/** @test */
public function micropubClientCanRequestEndpointConfig(): void
{
$response = $this->get('/api/post?q=config', ['HTTP_Authorization' => 'Bearer ' . $this->getToken()]);
$response->assertJsonFragment(['uid' => 'https://twitter.com/jonnybarnes']);
}
/**
* Test a valid micropub requests creates a new note.
*
* @return void
*/
public function test_micropub_post_request_creates_new_note()
/** @test */
public function micropubClientCanCreateNewNote(): void
{
$faker = \Faker\Factory::create();
$faker = Factory::create();
$note = $faker->text;
$response = $this->post(
'/api/post',
@ -133,15 +108,11 @@ class MicropubControllerTest extends TestCase
$this->assertDatabaseHas('notes', ['note' => $note]);
}
/**
* Test a valid micropub requests creates a new note and syndicates to Twitter.
*
* @return void
*/
public function test_micropub_post_request_creates_new_note_sends_to_twitter()
/** @test */
public function micropubClientCanRequestTheNewNoteIsSyndicatedToTwitter(): void
{
Queue::fake();
$faker = \Faker\Factory::create();
$faker = Factory::create();
$note = $faker->text;
$response = $this->post(
'/api/post',
@ -157,12 +128,8 @@ class MicropubControllerTest extends TestCase
Queue::assertPushed(SyndicateNoteToTwitter::class);
}
/**
* Test a valid micropub requests creates a new place.
*
* @return void
*/
public function test_micropub_post_request_creates_new_place()
/** @test */
public function micropubClientsCanCreateNewPlaces(): void
{
$response = $this->post(
'/api/post',
@ -177,13 +144,8 @@ class MicropubControllerTest extends TestCase
$this->assertDatabaseHas('places', ['slug' => 'the-barton-arms']);
}
/**
* Test a valid micropub requests creates a new place with latitude
* and longitude values defined separately.
*
* @return void
*/
public function test_micropub_post_request_creates_new_place_with_latlng()
/** @test */
public function micropubClientsCanCreateNewPlacesWithOldLocationSyntax(): void
{
$response = $this->post(
'/api/post',
@ -199,7 +161,8 @@ class MicropubControllerTest extends TestCase
$this->assertDatabaseHas('places', ['slug' => 'the-barton-arms']);
}
public function test_micropub_post_request_with_invalid_token_returns_expected_error_response()
/** @test */
public function micropubClientWebRequestWithInvalidTokenReturnsErrorResponse(): void
{
$response = $this->post(
'/api/post',
@ -213,7 +176,8 @@ class MicropubControllerTest extends TestCase
$response->assertJson(['error' => 'invalid_token']);
}
public function test_micropub_post_request_with_scopeless_token_returns_expected_error_response()
/** @test */
public function micropubClientWebRequestWithTokenWithoutAnyScopesReturnsErrorResponse(): void
{
$response = $this->post(
'/api/post',
@ -227,7 +191,8 @@ class MicropubControllerTest extends TestCase
$response->assertJson(['error_description' => 'The provided token has no scopes']);
}
public function test_micropub_post_request_for_place_without_create_scope_errors()
/** @test */
public function micropubClientWebRequestWithTokenWithoutCreateScopesReturnsErrorResponse(): void
{
$response = $this->post(
'/api/post',
@ -245,16 +210,16 @@ class MicropubControllerTest extends TestCase
/**
* Test a valid micropub requests using JSON syntax creates a new note.
*
* @return void
* @test
*/
public function test_micropub_post_request_with_json_syntax_creates_new_note()
public function micropubClientApiRequestCreatesNewNote(): void
{
Queue::fake();
Media::create([
'path' => 'test-photo.jpg',
'type' => 'image',
]);
$faker = \Faker\Factory::create();
$faker = Factory::create();
$note = $faker->text;
$response = $this->postJson(
'/api/post',
@ -282,16 +247,16 @@ class MicropubControllerTest extends TestCase
* Test a valid micropub requests using JSON syntax creates a new note with
* existing self-created place.
*
* @return void
* @test
*/
public function test_micropub_post_request_with_json_syntax_creates_new_note_with_existing_place_in_location()
public function micropubClientApiRequestCreatesNewNoteWithExistingPlaceInLocationData(): void
{
$place = new Place();
$place->name = 'Test Place';
$place->latitude = 1.23;
$place->longitude = 4.56;
$place->save();
$faker = \Faker\Factory::create();
$faker = Factory::create();
$note = $faker->text;
$response = $this->postJson(
'/api/post',
@ -313,11 +278,11 @@ class MicropubControllerTest extends TestCase
* Test a valid micropub requests using JSON syntax creates a new note with
* a new place defined in the location block.
*
* @return void
* @test
*/
public function test_micropub_post_request_with_json_syntax_creates_new_note_with_new_place_in_location()
public function micropubClientApiRequestCreatesNewNoteWithNewPlaceInLocationData(): void
{
$faker = \Faker\Factory::create();
$faker = Factory::create();
$note = $faker->text;
$response = $this->postJson(
'/api/post',
@ -349,11 +314,11 @@ class MicropubControllerTest extends TestCase
* Test a valid micropub requests using JSON syntax creates a new note without
* a new place defined in the location block if there is missing data.
*
* @return void
* @test
*/
public function test_micropub_post_request_with_json_syntax_creates_new_note_without_new_place_in_location()
public function micropubClientApiRequestCreatesNewNoteWithoutNewPlaceInLocationData(): void
{
$faker = \Faker\Factory::create();
$faker = Factory::create();
$note = $faker->text;
$response = $this->postJson(
'/api/post',
@ -383,11 +348,11 @@ class MicropubControllerTest extends TestCase
* Test a micropub requests using JSON syntax without a token returns an
* error. Also check the message.
*
* @return void
* @test
*/
public function test_micropub_post_request_with_json_syntax_without_token_returns_error()
public function micropubClientApiRequestWithoutTokenReturnsError(): void
{
$faker = \Faker\Factory::create();
$faker = Factory::create();
$note = $faker->text;
$response = $this->postJson(
'/api/post',
@ -410,11 +375,11 @@ class MicropubControllerTest extends TestCase
* Test a micropub requests using JSON syntax without a valid token returns
* an error. Also check the message.
*
* @return void
* @test
*/
public function test_micropub_post_request_with_json_syntax_with_insufficient_token_returns_error()
public function micropubClientApiRequestWithTokenWithInsufficientPermissionReturnsError(): void
{
$faker = \Faker\Factory::create();
$faker = Factory::create();
$note = $faker->text;
$response = $this->postJson(
'/api/post',
@ -434,7 +399,8 @@ class MicropubControllerTest extends TestCase
->assertStatus(401);
}
public function test_micropub_post_request_with_json_syntax_for_unsupported_type_returns_error()
/** @test */
public function micropubClientApiRequestForUnsupportedPostTypeReturnsError(): void
{
$response = $this->postJson(
'/api/post',
@ -454,9 +420,10 @@ class MicropubControllerTest extends TestCase
->assertStatus(500);
}
public function test_micropub_post_request_with_json_syntax_creates_new_place()
/** @test */
public function micropubClientApiRequestCreatesNewPlace(): void
{
$faker = \Faker\Factory::create();
$faker = Factory::create();
$response = $this->postJson(
'/api/post',
[
@ -473,9 +440,10 @@ class MicropubControllerTest extends TestCase
->assertStatus(201);
}
public function test_micropub_post_request_with_json_syntax_and_uncertainty_parameter_creates_new_place()
/** @test */
public function micropubClientApiRequestCreatesNewPlaceWithUncertaintyParameter(): void
{
$faker = \Faker\Factory::create();
$faker = Factory::create();
$response = $this->postJson(
'/api/post',
[
@ -492,13 +460,15 @@ class MicropubControllerTest extends TestCase
->assertStatus(201);
}
public function test_micropub_post_request_with_json_syntax_update_replace_post()
/** @test */
public function micropubClientApiRequestUpdatesExistingNote(): void
{
$note = Note::factory()->create();
$response = $this->postJson(
'/api/post',
[
'action' => 'update',
'url' => config('app.url') . '/notes/A',
'url' => $note->longurl,
'replace' => [
'content' => ['replaced content'],
],
@ -510,13 +480,15 @@ class MicropubControllerTest extends TestCase
->assertStatus(200);
}
public function test_micropub_post_request_with_json_syntax_update_add_post()
/** @test */
public function micropubClientApiRequestUpdatesNoteSyndicationLinks(): void
{
$note = Note::factory()->create();
$response = $this->postJson(
'/api/post',
[
'action' => 'update',
'url' => config('app.url') . '/notes/A',
'url' => $note->longurl,
'add' => [
'syndication' => [
'https://www.swarmapp.com/checkin/123',
@ -535,13 +507,15 @@ class MicropubControllerTest extends TestCase
]);
}
public function test_micropub_post_request_with_json_syntax_update_add_image_to_post()
/** @test */
public function micropubClientApiRequestAddsImageToNote(): void
{
$note = Note::factory()->create();
$response = $this->postJson(
'/api/post',
[
'action' => 'update',
'url' => config('app.url') . '/notes/A',
'url' => $note->longurl,
'add' => [
'photo' => ['https://example.org/photo.jpg'],
],
@ -556,7 +530,8 @@ class MicropubControllerTest extends TestCase
]);
}
public function test_micropub_post_request_with_json_syntax_update_add_post_errors_for_non_note()
/** @test */
public function micropubClientApiRequestReturnsErrorTryingToUpdateNonNoteModel(): void
{
$response = $this->postJson(
'/api/post',
@ -574,7 +549,8 @@ class MicropubControllerTest extends TestCase
->assertStatus(500);
}
public function test_micropub_post_request_with_json_syntax_update_add_post_errors_for_note_not_found()
/** @test */
public function micropubClientApiRequestReturnsErrorTryingToUpdateNonExistingNote(): void
{
$response = $this->postJson(
'/api/post',
@ -592,13 +568,15 @@ class MicropubControllerTest extends TestCase
->assertStatus(404);
}
public function test_micropub_post_request_with_json_syntax_update_add_post_errors_for_unsupported_request()
/** @test */
public function micropubClientApiRequestReturnsErrorWhenTryingToUpdateUnsupportedProperty(): void
{
$note = Note::factory()->create();
$response = $this->postJson(
'/api/post',
[
'action' => 'update',
'url' => config('app.url') . '/notes/A',
'url' => $note->longurl,
'morph' => [ // or any other unsupported update type
'syndication' => ['https://www.swarmapp.com/checkin/123'],
],
@ -610,7 +588,8 @@ class MicropubControllerTest extends TestCase
->assertStatus(500);
}
public function test_micropub_post_request_with_json_syntax_update_errors_for_insufficient_scope()
/** @test */
public function micropubClientApiRequestWithTokenWithInsufficientScopeReturnsError(): void
{
$response = $this->postJson(
'/api/post',
@ -628,13 +607,15 @@ class MicropubControllerTest extends TestCase
->assertJson(['error' => 'insufficient_scope']);
}
public function test_micropub_post_request_with_json_syntax_update_replace_post_syndication()
/** @test */
public function micropubClientApiRequestCanReplaceNoteSyndicationTargets(): void
{
$note = Note::factory()->create();
$response = $this->postJson(
'/api/post',
[
'action' => 'update',
'url' => config('app.url') . '/notes/B',
'url' => $note->longurl,
'replace' => [
'syndication' => [
'https://www.swarmapp.com/checkin/the-id',
@ -653,9 +634,10 @@ class MicropubControllerTest extends TestCase
]);
}
public function test_access_token_form_encoded()
/** @test */
public function micropubClientWebReauestCanEncodeTokenWithinTheForm(): void
{
$faker = \Faker\Factory::create();
$faker = Factory::create();
$note = $faker->text;
$response = $this->post(
'/api/post',

View file

@ -1,10 +1,12 @@
<?php
declare(strict_types=1);
namespace Tests\Feature;
use App\Jobs\ProcessMedia;
use App\Models\Media;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Queue;
use Illuminate\Support\Facades\Storage;
@ -13,11 +15,11 @@ use Tests\TestToken;
class MicropubMediaTest extends TestCase
{
use DatabaseTransactions;
use RefreshDatabase;
use TestToken;
/** @test */
public function emptyResponseForLastUploadWhenNoneFound()
public function emptyResponseForLastUploadWhenNoneFound(): void
{
// Make sure theres no media
Media::all()->each(function ($media) {
@ -33,7 +35,7 @@ class MicropubMediaTest extends TestCase
}
/** @test */
public function clientCanListLastUpload()
public function clientCanListLastUpload(): void
{
Queue::fake();
Storage::fake('s3');
@ -61,7 +63,7 @@ class MicropubMediaTest extends TestCase
}
/** @test */
public function clientCanSourceUploads()
public function clientCanSourceUploads(): void
{
Queue::fake();
Storage::fake('s3');
@ -91,7 +93,7 @@ class MicropubMediaTest extends TestCase
}
/** @test */
public function clientCanSourceUploadsWithLimit()
public function clientCanSourceUploadsWithLimit(): void
{
Queue::fake();
Storage::fake('s3');
@ -123,7 +125,7 @@ class MicropubMediaTest extends TestCase
}
/** @test */
public function errorResponseForUnknownQValue()
public function errorResponseForUnknownQValue(): void
{
$response = $this->get(
'/api/media?q=unknown',
@ -134,7 +136,7 @@ class MicropubMediaTest extends TestCase
}
/** @test */
public function optionsRequestReturnsCorsResponse()
public function optionsRequestReturnsCorsResponse(): void
{
$response = $this->options('/api/media');
@ -143,7 +145,7 @@ class MicropubMediaTest extends TestCase
}
/** @test */
public function mediaEndpointRequestWithInvalidTokenReturns400Response()
public function mediaEndpointRequestWithInvalidTokenReturns400Response(): void
{
$response = $this->post(
'/api/media',
@ -155,7 +157,7 @@ class MicropubMediaTest extends TestCase
}
/** @test */
public function mediaEndpointRequestWithTokenWithNoScopeReturns400Response()
public function mediaEndpointRequestWithTokenWithNoScopeReturns400Response(): void
{
$response = $this->post(
'/api/media',
@ -167,7 +169,7 @@ class MicropubMediaTest extends TestCase
}
/** @test */
public function mediaEndpointRequestWithInsufficientTokenScopesReturns401Response()
public function mediaEndpointRequestWithInsufficientTokenScopesReturns401Response(): void
{
$response = $this->post(
'/api/media',
@ -181,7 +183,7 @@ class MicropubMediaTest extends TestCase
}
/** @test */
public function mediaEndpointUploadFile()
public function mediaEndpointUploadFile(): void
{
Queue::fake();
Storage::fake('s3');
@ -204,7 +206,7 @@ class MicropubMediaTest extends TestCase
}
/** @test */
public function mediaEndpointUploadAudioFile()
public function mediaEndpointUploadAudioFile(): void
{
Queue::fake();
Storage::fake('s3');
@ -227,7 +229,7 @@ class MicropubMediaTest extends TestCase
}
/** @test */
public function mediaEndpointUploadVideoFile()
public function mediaEndpointUploadVideoFile(): void
{
Queue::fake();
Storage::fake('s3');
@ -250,7 +252,7 @@ class MicropubMediaTest extends TestCase
}
/** @test */
public function mediaEndpointUploadDocumentFile()
public function mediaEndpointUploadDocumentFile(): void
{
Queue::fake();
Storage::fake('s3');
@ -272,7 +274,7 @@ class MicropubMediaTest extends TestCase
}
/** @test */
public function mediaEndpointUploadInvalidFileReturnsError()
public function mediaEndpointUploadInvalidFileReturnsError(): void
{
Queue::fake();
Storage::fake('local');

View file

@ -1,19 +1,24 @@
<?php
declare(strict_types=1);
namespace Tests\Feature;
use Tests\TestCase;
use App\Models\Note;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class NotesControllerTest extends TestCase
{
use RefreshDatabase;
/**
* Test the `/notes` page returns 200, this should
* mean the database is being hit.
*
* @return void
* @test
*/
public function test_notes_page()
public function notesPageLoads(): void
{
$response = $this->get('/notes');
$response->assertStatus(200);
@ -22,37 +27,41 @@ class NotesControllerTest extends TestCase
/**
* Test a specific note.
*
* @return void
* @test
*/
public function test_specific_note()
public function specificNotePageLoads(): void
{
$response = $this->get('/notes/D');
$note = Note::factory()->create();
$response = $this->get($note->longurl);
$response->assertViewHas('note');
}
public function test_note_replying_to_tweet()
/** @todo */
/* @test *
public function noteReplyingToTweet(): void
{
$response = $this->get('/notes/B');
$response->assertViewHas('note');
}
}*/
/**
* Test that `/note/{decID}` redirects to `/notes/{nb60id}`.
*
* @return void
* @test
*/
public function test_dec_id_redirect()
public function oldNoteUrlsRedirect(): void
{
$response = $this->get('/note/11');
$response->assertRedirect(config('app.url') . '/notes/B');
$note = Note::factory()->create();
$response = $this->get('/note/' . $note->id);
$response->assertRedirect($note->longurl);
}
/**
* Visit the tagged page and check the tag view data.
*
* @return void
* @test
*/
public function test_tagged_notes_page()
public function taggedNotesPageLoads(): void
{
$response = $this->get('/notes/tagged/beer');
$response->assertViewHas('tag', 'beer');

View file

@ -1,18 +1,21 @@
<?php
declare(strict_types=1);
namespace Tests\Feature;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Carbon;
use Tests\TestCase;
use Tests\TestToken;
use Lcobucci\JWT\Builder;
use Lcobucci\JWT\Signer\Hmac\Sha256;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class OwnYourGramTest extends TestCase
{
use DatabaseTransactions, TestToken;
use RefreshDatabase;
use TestToken;
public function test_ownyourgram_post()
/** @test */
public function postingInstagramUrlSavesMediaPath(): void
{
$response = $this->json(
'POST',
@ -21,10 +24,13 @@ class OwnYourGramTest extends TestCase
'type' => ['h-entry'],
'properties' => [
'content' => ['How beautiful are the plates and chopsticks'],
'published' => [\Carbon\Carbon::now()->toIso8601String()],
'published' => [Carbon::now()->toIso8601String()],
'location' => ['geo:53.802419075834,-1.5431942917637'],
'syndication' => ['https://www.instagram.com/p/BVC_nVTBFfi/'],
'photo' => ['https://scontent-sjc2-1.cdninstagram.com/t51.2885-15/e35/18888604_425332491185600_326487281944756224_n.jpg'],
'photo' => [
// phpcs:ignore Generic.Files.LineLength.TooLong
'https://scontent-sjc2-1.cdninstagram.com/t51.2885-15/e35/18888604_425332491185600_326487281944756224_n.jpg'
],
],
],
['HTTP_Authorization' => 'Bearer ' . $this->getToken()]
@ -34,6 +40,7 @@ class OwnYourGramTest extends TestCase
'response' => 'created'
]);
$this->assertDatabaseHas('media_endpoint', [
// phpcs:ignore Generic.Files.LineLength.TooLong
'path' => 'https://scontent-sjc2-1.cdninstagram.com/t51.2885-15/e35/18888604_425332491185600_326487281944756224_n.jpg'
]);
$this->assertDatabaseHas('notes', [

View file

@ -1,16 +1,19 @@
<?php
declare(strict_types=1);
namespace Tests\Feature;
use Tests\TestCase;
use App\Models\WebMention;
use Illuminate\FileSystem\FileSystem;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\TestCase;
class ParseCachedWebMentionsTest extends TestCase
{
use DatabaseTransactions;
use RefreshDatabase;
protected function setUp(): void
{
@ -18,12 +21,23 @@ class ParseCachedWebMentionsTest extends TestCase
mkdir(storage_path('HTML') . '/https/aaronpk.localhost/reply', 0777, true);
mkdir(storage_path('HTML') . '/http/tantek.com', 0777, true);
copy(__DIR__.'/../aaron.html', storage_path('HTML') . '/https/aaronpk.localhost/reply/1');
copy(__DIR__.'/../tantek.html', storage_path('HTML') . '/http/tantek.com/index.html');
copy(__DIR__ . '/../aaron.html', storage_path('HTML') . '/https/aaronpk.localhost/reply/1');
copy(__DIR__ . '/../tantek.html', storage_path('HTML') . '/http/tantek.com/index.html');
}
public function test_parsing_html()
/** @test */
public function parseWebmentionHtml(): void
{
$webmentionAaron = WebMention::factory()->create([
'source' => 'https://aaronpk.localhost/reply/1',
'created_at' => Carbon::now()->subDays(5),
'updated_at' => Carbon::now()->subDays(5),
]);
$webmentionTantek = WebMention::factory()->create([
'source' => 'http://tantek.com/',
'created_at' => Carbon::now()->subDays(5),
'updated_at' => Carbon::now()->subDays(5),
]);
$this->assertFileExists(storage_path('HTML') . '/https/aaronpk.localhost/reply/1');
$this->assertFileExists(storage_path('HTML') . '/http/tantek.com/index.html');
$htmlAaron = file_get_contents(storage_path('HTML') . '/https/aaronpk.localhost/reply/1');
@ -37,8 +51,9 @@ class ParseCachedWebMentionsTest extends TestCase
Artisan::call('webmentions:parsecached');
$webmentionAaron = WebMention::find(1);
$webmentionTantek = WebMention::find(2);
$webmentionAaron = WebMention::find($webmentionAaron->id);
$webmentionTantek = WebMention::find($webmentionTantek->id);
$this->assertTrue($webmentionAaron->updated_at->gt($webmentionAaron->created_at));
$this->assertTrue($webmentionTantek->updated_at->gt($webmentionTantek->created_at));
}

View file

@ -1,18 +1,23 @@
<?php
declare(strict_types=1);
namespace Tests\Feature;
use Tests\TestCase;
use App\Models\Place;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class PlacesTest extends TestCase
{
use RefreshDatabase;
/**
* Test the `/places` page for OK response.
*
* @return void
* @test
*/
public function test_places_page()
public function placesPageLoads(): void
{
$response = $this->get('/places');
$response->assertStatus(200);
@ -21,12 +26,12 @@ class PlacesTest extends TestCase
/**
* Test a specific place.
*
* @return void
* @test
*/
public function test_single_place()
public function singlePlacePageLoads(): void
{
$place = Place::where('slug', 'the-bridgewater-pub')->first();
$response = $this->get('/places/the-bridgewater-pub');
$place = Place::factory()->create();
$response = $this->get($place->longurl);
$response->assertViewHas('place', $place);
}

View file

@ -1,18 +1,27 @@
<?php
declare(strict_types=1);
namespace Tests\Feature;
use Tests\TestCase;
use App\Jobs\DownloadWebMention;
use Illuminate\Support\Facades\Queue;
use App\Models\WebMention;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Queue;
use Tests\TestCase;
class ReDownloadWebMentionsTest extends TestCase
{
public function test_jobs_are_dispatched()
use RefreshDatabase;
/** @test */
public function downloadJobGetsQueued(): void
{
Queue::fake();
WebMention::factory()->create();
Artisan::call('webmentions:redownload');
Queue::assertPushed(DownloadWebMention::class);

View file

@ -1,14 +1,23 @@
<?php
declare(strict_types=1);
namespace Tests\Feature;
use Tests\TestCase;
use App\Models\Note;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class SearchControllerTest extends TestCase
{
public function test_search()
use RefreshDatabase;
/** @test */
public function searchPageReturnsResult(): void
{
Note::factory()->create([
'note' => 'I love [duckduckgo.com](https://duckduckgo.com)',
]);
$response = $this->get('/search?terms=love');
$response->assertSee('duckduckgo.com');
}

View file

@ -1,33 +1,38 @@
<?php
declare(strict_types=1);
namespace Tests\Feature;
use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;
class ShortURLsControllerTest extends TestCase
{
public function test_short_domain_redirects_to_long_domain()
/** @test */
public function shortDomainRedirectsToLongDomain(): void
{
$response = $this->get('http://' . config('app.shorturl'));
$response = $this->get('https://' . config('app.shorturl'));
$response->assertRedirect(config('app.url'));
}
public function test_short_domain_slashat_redirects_to_twitter()
/** @test */
public function shortDomainSlashAtRedirectsToTwitter(): void
{
$response = $this->get('http://' . config('app.shorturl') . '/@');
$response = $this->get('https://' . config('app.shorturl') . '/@');
$response->assertRedirect('https://twitter.com/jonnybarnes');
}
public function test_short_domain_slasht_redirects_to_long_domain_slash_notes()
/** @test */
public function shortDomainSlashTRedirectsToLongDomainSlashNotes(): void
{
$response = $this->get('http://' . config('app.shorturl') . '/t/E');
$response = $this->get('https://' . config('app.shorturl') . '/t/E');
$response->assertRedirect(config('app.url') . '/notes/E');
}
public function test_short_domain_slashb_redirects_to_long_domain_slash_blog()
/** @test */
public function shortDomainSlashBRedirectsToLongDomainSlashBlog(): void
{
$response = $this->get('http://' . config('app.shorturl') . '/b/1');
$response = $this->get('https://' . config('app.shorturl') . '/b/1');
$response->assertRedirect(config('app.url') . '/blog/s/1');
}
}

View file

@ -1,26 +1,37 @@
<?php
declare(strict_types=1);
namespace Tests\Feature;
use App\Jobs\SendWebMentions;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Queue;
use Tests\TestCase;
use Tests\TestToken;
use Lcobucci\JWT\Builder;
use Lcobucci\JWT\Signer\Hmac\Sha256;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class SwarmTest extends TestCase
{
use DatabaseTransactions, TestToken;
use RefreshDatabase;
use TestToken;
public function test_faked_ownyourswarm_request_with_foursquare()
/**
* Given a check in to Foursquare, this is the content Ownyourswarm will post to us.
*
* @test
*/
public function mockedOwnyourswarmRequestWithFoursquare(): void
{
Queue::fake();
$response = $this->json(
'POST',
'api/post',
[
'type' => ['h-entry'],
'properties' => [
'published' => [\Carbon\Carbon::now()->toDateTimeString()],
'published' => [Carbon::now()->toDateTimeString()],
'syndication' => ['https://www.swarmapp.com/checkin/abc'],
'content' => [[
'value' => 'My first #checkin using Example Product',
@ -48,18 +59,27 @@ class SwarmTest extends TestCase
$this->assertDatabaseHas('places', [
'external_urls' => '{"foursquare": "https://foursquare.com/v/123456"}'
]);
Queue::assertPushed(SendWebMentions::class);
}
// this request would actually come from another client than OwnYourSwarm
public function test_faked_ownyourswarm_request_with_osm()
/**
* This request would actually come from another client than OwnYourSwarm, but were testing
* OpenStreetMap data.
*
* @test
*/
public function mockedOwnyourswarmRequestWithOsm(): void
{
Queue::fake();
$response = $this->json(
'POST',
'api/post',
[
'type' => ['h-entry'],
'properties' => [
'published' => [\Carbon\Carbon::now()->toDateTimeString()],
'published' => [Carbon::now()->toDateTimeString()],
'content' => [[
'value' => 'My first #checkin using Example Product',
'html' => 'My first #checkin using <a href="http://example.org">Example Product</a>',
@ -83,18 +103,26 @@ class SwarmTest extends TestCase
$this->assertDatabaseHas('places', [
'external_urls' => '{"osm": "https://www.openstreetmap.org/way/123456"}'
]);
Queue::assertPushed(SendWebMentions::class);
}
// this request would actually come from another client than OwnYourSwarm
public function test_faked_ownyourswarm_request_without_known_external_url()
/**
* This request would actually come from another client than OwnYourSwarm, as that would include a Foursquare URL
*
* @test
*/
public function mockedOwnyourswarmRequestWithoutKnownExternalUrl(): void
{
Queue::fake();
$response = $this->json(
'POST',
'api/post',
[
'type' => ['h-entry'],
'properties' => [
'published' => [\Carbon\Carbon::now()->toDateTimeString()],
'published' => [Carbon::now()->toDateTimeString()],
'content' => [[
'value' => 'My first #checkin using Example Product',
'html' => 'My first #checkin using <a href="http://example.org">Example Product</a>',
@ -118,9 +146,12 @@ class SwarmTest extends TestCase
$this->assertDatabaseHas('places', [
'external_urls' => '{"default": "https://www.example.org/way/123456"}'
]);
Queue::assertPushed(SendWebMentions::class);
}
public function test_faked_ownyourswarm_request_with_no_text_content()
/** @test */
public function mockedOwnyourswarmRequestWithNoTextContent(): void
{
$response = $this->json(
'POST',
@ -128,7 +159,7 @@ class SwarmTest extends TestCase
[
'type' => ['h-entry'],
'properties' => [
'published' => [\Carbon\Carbon::now()->toDateTimeString()],
'published' => [Carbon::now()->toDateTimeString()],
'syndication' => ['https://www.swarmapp.com/checkin/def'],
],
'checkin' => [
@ -152,18 +183,22 @@ class SwarmTest extends TestCase
$this->assertDatabaseHas('notes', [
'swarm_url' => 'https://www.swarmapp.com/checkin/def'
]);
// Check the default text content for the note was saved
$this->get($response->__get('headers')->get('location'))->assertSee('📍');
}
public function test_faked_ownyourswarm_request_saves_just_post_when_error_in_checkin_data()
/** @test */
public function mockedOwnyourswarmRequestSavesJustThePostWhenAnErrorOccursInTheCheckinData(): void
{
Queue::fake();
$response = $this->json(
'POST',
'api/post',
[
'type' => ['h-entry'],
'properties' => [
'published' => [\Carbon\Carbon::now()->toDateTimeString()],
'published' => [Carbon::now()->toDateTimeString()],
'syndication' => ['https://www.swarmapp.com/checkin/abc'],
'content' => [[
'value' => 'My first #checkin using Example Product',
@ -186,17 +221,22 @@ class SwarmTest extends TestCase
$this->assertDatabaseMissing('places', [
'name' => 'Awesome Venue',
]);
Queue::assertPushed(SendWebMentions::class);
}
public function test_ownyourswarm_request_with_hadr_location()
/** @test */
public function mockedOwnyourswarmRequestWithHAdrLocation(): void
{
Queue::fake();
$response = $this->json(
'POST',
'api/post',
[
'type' => ['h-entry'],
'properties' => [
'published' => [\Carbon\Carbon::now()->toDateTimeString()],
'published' => [Carbon::now()->toDateTimeString()],
'syndication' => ['https://www.swarmapp.com/checkin/abc'],
'content' => [[
'value' => 'My first #checkin using Example Product',
@ -227,18 +267,20 @@ class SwarmTest extends TestCase
$this->assertDatabaseMissing('places', [
'name' => 'Awesome Venue',
]);
Queue::assertPushed(SendWebMentions::class);
}
/** @test */
public function a_real_ownyourswarm_checkin()
public function ownyourswarmCheckinTestUsingRealData(): void
{
$response = $this->json(
'POST',
'api/post',
[
'type' => ['h-entry'],
'properties' =>[
'published' => [\Carbon\Carbon::now()->toDateTimeString()]
'properties' => [
'published' => [Carbon::now()->toDateTimeString()]
],
'syndication' => [
'https://www.swarmapp.com/user/199841/checkin/5c4b1ac56dcf04002c0a4f58'

View file

@ -1,15 +1,17 @@
<?php
declare(strict_types=1);
namespace Tests\Feature;
use IndieAuth\Client;
use Mockery;
use Tests\TestCase;
use IndieAuth\Client;
use Illuminate\Http\JsonResponse;
class TokenEndpointTest extends TestCase
{
public function test_token_endpoint_issues_token()
/** @test */
public function tokenEndpointIssuesToken(): void
{
$mockClient = Mockery::mock(Client::class);
$mockClient->shouldReceive('discoverAuthorizationEndpoint')
@ -35,7 +37,8 @@ class TokenEndpointTest extends TestCase
]);
}
public function test_token_endpoint_returns_error_when_auth_endpoint_lacks_me_data()
/** @test */
public function tokenEndpointReturnsErrorWhenAuthEndpointLacksMeData(): void
{
$mockClient = Mockery::mock(Client::class);
$mockClient->shouldReceive('discoverAuthorizationEndpoint')
@ -60,7 +63,8 @@ class TokenEndpointTest extends TestCase
]);
}
public function test_token_endpoint_returns_error_when_no_auth_endpoint_found()
/** @test */
public function tokenEndpointReturnsErrorWhenNoAuthEndpointFound(): void
{
$mockClient = Mockery::mock(Client::class);
$mockClient->shouldReceive('discoverAuthorizationEndpoint')

View file

@ -1,13 +1,15 @@
<?php
declare(strict_types=1);
namespace Tests\Feature;
use App\Services\TokenService;
use DateTimeImmutable;
use Lcobucci\JWT\Configuration;
use Lcobucci\JWT\Signer\Key\InMemory;
use Lcobucci\JWT\Validation\RequiredConstraintsViolated;
use Tests\TestCase;
use App\Services\TokenService;
class TokenServiceTest extends TestCase
{
@ -15,9 +17,9 @@ class TokenServiceTest extends TestCase
* Given the token is dependent on a random nonce, the time of creation and
* the APP_KEY, to test, we shall create a token, and then verify it.
*
* @return void
* @test
*/
public function test_token_creation_and_validation()
public function tokenserviceCreatesAndValidatesTokens(): void
{
$tokenService = new TokenService();
$data = [
@ -35,7 +37,8 @@ class TokenServiceTest extends TestCase
$this->assertSame($data, $validData);
}
public function test_token_with_different_signing_key_throws_exception()
/** @test */
public function tokensWithDifferentSigningKeyThrowsException(): void
{
$this->expectException(RequiredConstraintsViolated::class);

View file

@ -1,14 +1,21 @@
<?php
declare(strict_types=1);
namespace Tests\Feature;
use Tests\TestCase;
use App\Jobs\ProcessWebMention;
use App\Models\Note;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Queue;
use Tests\TestCase;
class WebMentionsControllerTest extends TestCase
{
public function test_get_endpoint()
use RefreshDatabase;
/** @test */
public function webmentionEndpointCanServeBrowserRequest(): void
{
$response = $this->get('/webmention');
$response->assertViewIs('webmention-endpoint');
@ -17,20 +24,22 @@ class WebMentionsControllerTest extends TestCase
/**
* Test webmentions without source and target are rejected.
*
* @return void
* @test
*/
public function test_webmentions_without_source_and_target_are_rejected()
public function webmentionsWithoutSourceAndTargetAreRejected(): void
{
$response = $this->call('POST', '/webmention', ['source' => 'https://example.org/post/123']);
$response->assertStatus(400);
}
/**
* Test invalid target gets a 400 response.
* Test invalid target gives a 400 response.
*
* @return void
* In this case an invalid target is a URL that doesnt exist on our domain.
*
* @test
*/
public function test_invalid_target_returns_400_response()
public function invalidTargetReturnsErrorResponse(): void
{
$response = $this->call('POST', '/webmention', [
'source' => 'https://example.org/post/123',
@ -40,11 +49,11 @@ class WebMentionsControllerTest extends TestCase
}
/**
* Test blog target gets a 501 response due to me not supporting it.
* Test blog target gets a 501 response due to our not supporting it.
*
* @return void
* @test
*/
public function test_blog_target_returns_501_response()
public function blogTargetReturns501Response(): void
{
$response = $this->call('POST', '/webmention', [
'source' => 'https://example.org/post/123',
@ -54,11 +63,11 @@ class WebMentionsControllerTest extends TestCase
}
/**
* Test that a non-existant note gives a 400 response.
* Test that a non-existent note gives a 400 response.
*
* @return void
* @test
*/
public function test_nonexistant_note_returns_400_response()
public function nonexistentNoteReturnsErrorResponse(): void
{
$response = $this->call('POST', '/webmention', [
'source' => 'https://example.org/post/123',
@ -67,18 +76,16 @@ class WebMentionsControllerTest extends TestCase
$response->assertStatus(400);
}
/**
* Test a legit webmention triggers the ProcessWebMention job.
*
* @return void
*/
public function test_legitimate_webmnetion_triggers_processwebmention_job()
/** @test */
public function legitimateWebmentionTriggersProcessWebmentionJob(): void
{
Queue::fake();
$note = Note::factory()->create();
$response = $this->call('POST', '/webmention', [
'source' => 'https://example.org/post/123',
'target' => config('app.url') . '/notes/B'
'target' => $note->longurl,
]);
$response->assertStatus(202);

View file

@ -1,16 +1,20 @@
<?php
declare(strict_types=1);
namespace Tests\Unit;
use Tests\TestCase;
use App\Models\Article;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Carbon;
use Tests\TestCase;
class ArticlesTest extends TestCase
{
use DatabaseTransactions;
use RefreshDatabase;
public function test_sluggable_method()
/** @test */
public function titleSlugIsGeneratedAutomatically(): void
{
$article = new Article();
$article->title = 'My Title';
@ -20,15 +24,17 @@ class ArticlesTest extends TestCase
$this->assertEquals('my-title', $article->titleurl);
}
public function test_markdown_conversion()
/** @test */
public function markdownContentIsConverted(): void
{
$article = new Article();
$article->main = 'Some *markdown*';
$this->assertEquals('<p>Some <em>markdown</em></p>'.PHP_EOL, $article->html);
$this->assertEquals('<p>Some <em>markdown</em></p>' . PHP_EOL, $article->html);
}
public function test_time_attributes()
/** @test */
public function weGenerateTheDifferentTimeAttributes(): void
{
$article = Article::create([
'title' => 'Test',
@ -41,7 +47,8 @@ class ArticlesTest extends TestCase
$this->assertEquals($article->pubdate, $article->updated_at->toRSSString());
}
public function test_link_accessor()
/** @test */
public function weGenerateTheArticleLinkFromTheSlug(): void
{
$article = Article::create([
'title' => 'Test',
@ -55,8 +62,15 @@ class ArticlesTest extends TestCase
);
}
public function test_date_scope()
/** @test */
public function dateScopeReturnsExpectedArticles(): void
{
Article::factory()->create([
'created_at' => Carbon::now()->subYear()->toDateTimeString(),
'updated_at' => Carbon::now()->subYear()->toDateTimeString(),
]);
Article::factory()->create();
$yearAndMonth = Article::date(date('Y'), date('m'))->get();
$this->assertTrue(count($yearAndMonth) === 1);

View file

@ -1,27 +1,31 @@
<?php
declare(strict_types=1);
namespace Tests\Unit;
use Tests\TestCase;
use App\Exceptions\InternetArchiveException;
use App\Services\BookmarkService;
use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use App\Services\BookmarkService;
use GuzzleHttp\Handler\MockHandler;
use App\Exceptions\InternetArchiveException;
use Tests\TestCase;
class BookmarksTest extends TestCase
{
/**
* @test
* @group puppeteer
*/
public function test_screenshot_of_google()
*
public function takeScreenshotOfDuckDuckGo()
{
$uuid = (new BookmarkService())->saveScreenshot('https://www.google.co.uk');
$uuid = (new BookmarkService())->saveScreenshot('https://duckduckgo.com');
$this->assertTrue(file_exists(public_path() . '/assets/img/bookmarks/' . $uuid . '.png'));
}
}*/
public function test_archive_link_method()
/** @test */
public function archiveLinkMethodCallsArchiveService(): void
{
$mock = new MockHandler([
new Response(200, ['Content-Location' => '/web/1234/example.org']),
@ -33,7 +37,8 @@ class BookmarksTest extends TestCase
$this->assertEquals('/web/1234/example.org', $url);
}
public function test_archive_link_method_archive_site_error_exception()
/** @test */
public function archiveLinkMethodThrowsAnExceptionOnError(): void
{
$this->expectException(InternetArchiveException::class);
@ -43,10 +48,11 @@ class BookmarksTest extends TestCase
$handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]);
$this->app->instance(Client::class, $client);
$url = (new BookmarkService())->getArchiveLink('https://example.org');
(new BookmarkService())->getArchiveLink('https://example.org');
}
public function test_archive_link_method_archive_site_no_location_exception()
/** @test */
public function archiveLinkMethodThrowsAnExceptionIfNoLocationReturned(): void
{
$this->expectException(InternetArchiveException::class);
@ -56,6 +62,6 @@ class BookmarksTest extends TestCase
$handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]);
$this->app->instance(Client::class, $client);
$url = (new BookmarkService())->getArchiveLink('https://example.org');
(new BookmarkService())->getArchiveLink('https://example.org');
}
}

View file

@ -1,18 +1,72 @@
<?php
declare(strict_types=1);
namespace Tests\Unit;
use Tests\TestCase;
class HelpersTest extends TestCase
{
public function test_normalize_url_is_idempotent()
/** @test */
public function normalizeUrlIsIdempotent(): void
{
$input = 'http://example.org:80/index.php?foo=bar&baz=1';
$this->assertEquals(normalize_url(normalize_url($input)), normalize_url($input));
}
public function urlProvider()
/**
* @test
* @dataProvider urlProvider
* @param string $input
* @param string $output
*/
public function normalizeUrlOnDataProvider(string $input, string $output): void
{
$this->assertEquals($output, normalize_url($input));
}
/** @test */
public function prettyPrintJson(): void
{
// phpcs:disable Generic.Files.LineLength.TooLong
$json = <<<JSON
{"glossary": {"title": "example glossary", "GlossDiv": {"title": "S", "GlossList": {"GlossEntry": {"ID": "SGML", "SortAs": "SGML", "GlossTerm": "Standard Generalized Markup Language", "Acronym": "SGML", "Abbrev": "ISO 8879:1986", "GlossDef": {"para": "A meta-markup language, used to create markup languages such as DocBook.", "GlossSeeAlso": ["GML", "XML"]}, "GlossSee": "markup"}}}}}
JSON;
// phpcs:enable Generic.Files.LineLength.TooLong
$expected = <<<EXPECTED
{
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"GlossList": {
"GlossEntry": {
"ID": "SGML",
"SortAs": "SGML",
"GlossTerm": "Standard Generalized Markup Language",
"Acronym": "SGML",
"Abbrev": "ISO 8879:1986",
"GlossDef": {
"para": "A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso": [
"GML",
"XML"
]
},
"GlossSee": "markup"
}
}
}
}
}
EXPECTED;
$this->assertEquals($expected, prettyPrintJson($json));
}
public function urlProvider(): array
{
return [
['https://example.org/', 'https://example.org'],
@ -25,48 +79,4 @@ class HelpersTest extends TestCase
],
];
}
/**
* @dataProvider urlProvider
* @group temp
*/
public function test_normalize_url($input, $output)
{
$this->assertEquals($output, normalize_url($input));
}
public function test_pretty_print_json()
{
$json = <<<JSON
{"glossary": {"title": "example glossary", "GlossDiv": {"title": "S", "GlossList": {"GlossEntry": {"ID": "SGML", "SortAs": "SGML", "GlossTerm": "Standard Generalized Markup Language", "Acronym": "SGML", "Abbrev": "ISO 8879:1986", "GlossDef": {"para": "A meta-markup language, used to create markup languages such as DocBook.", "GlossSeeAlso": ["GML", "XML"]}, "GlossSee": "markup"}}}}}
JSON;
$expected = <<<EXPECTED
{
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"GlossList": {
"GlossEntry": {
"ID": "SGML",
"SortAs": "SGML",
"GlossTerm": "Standard Generalized Markup Language",
"Acronym": "SGML",
"Abbrev": "ISO 8879:1986",
"GlossDef": {
"para": "A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso": [
"GML",
"XML"
]
},
"GlossSee": "markup"
}
}
}
}
}
EXPECTED;
$this->assertEquals($expected, prettyPrintJson($json));
}
}

View file

@ -1,16 +1,19 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Jobs;
use Tests\TestCase;
use App\Jobs\AddClientToDatabase;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class AddClientToDatabaseJobTest extends TestCase
{
use DatabaseTransactions;
use RefreshDatabase;
public function test_job_adds_client()
/** @test */
public function clientIsAddedToDatabaseByJob(): void
{
$job = new AddClientToDatabase('https://example.org/client');
$job->handle();

View file

@ -1,14 +1,16 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Jobs;
use Tests\TestCase;
use App\Jobs\DownloadWebMention;
use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use App\Jobs\DownloadWebMention;
use GuzzleHttp\Handler\MockHandler;
use Illuminate\FileSystem\FileSystem;
use Tests\TestCase;
class DownloadWebMentionJobTest extends TestCase
{
@ -21,15 +23,16 @@ class DownloadWebMentionJobTest extends TestCase
parent::tearDown();
}
public function test_the_job_saves_html()
/** @test */
public function htmlIsSavedByJob(): void
{
$this->assertFileDoesNotExist(storage_path('HTML/https'));
$source = 'https://example.org/reply/1';
$html = <<<HTML
<div class="h-entry">
<a class="u-like-of" href=""></a>
</div>
HTML;
<div class="h-entry">
<a class="u-like-of" href=""></a>
</div>
HTML;
$html = str_replace('href=""', 'href="' . config('app.url') . '/notes/A"', $html);
$mock = new MockHandler([
new Response(200, ['X-Foo' => 'Bar'], $html),
@ -48,21 +51,22 @@ HTML;
$this->assertFileDoesNotExist(storage_path('HTML/https/example.org/reply') . '/1.' . date('Y-m-d') . '.backup');
}
public function test_the_job_saves_html_and_backup()
/** @test */
public function htmlAndBackupSavedByJob(): void
{
$this->assertFileDoesNotExist(storage_path('HTML/https'));
$source = 'https://example.org/reply/1';
$html = <<<HTML
<div class="h-entry">
<a class="u-like-of" href=""></a>
</div>
HTML;
<div class="h-entry">
<a class="u-like-of" href=""></a>
</div>
HTML;
$html2 = <<<HTML
<div class="h-entry">
<a class="u-like-of" href=""></a>
<a class="u-repost-of" href=""></a>
</div>
HTML;
<div class="h-entry">
<a class="u-like-of" href=""></a>
<a class="u-repost-of" href=""></a>
</div>
HTML;
$html = str_replace('href=""', 'href="' . config('app.url') . '/notes/A"', $html);
$html2 = str_replace('href=""', 'href="' . config('app.url') . '/notes/A"', $html2);
$mock = new MockHandler([
@ -82,15 +86,16 @@ HTML;
$this->assertFileExists(storage_path('HTML/https/example.org/reply') . '/1.' . date('Y-m-d') . '.backup');
}
public function test_an_index_html_file()
/** @test */
public function indexHtmlFileIsSavedByJobForUrlsEndingWithSlash(): void
{
$this->assertFileDoesNotExist(storage_path('HTML/https'));
$source = 'https://example.org/reply-one/';
$html = <<<HTML
<div class="h-entry">
<a class="u-like-of" href=""></a>
</div>
HTML;
<div class="h-entry">
<a class="u-like-of" href=""></a>
</div>
HTML;
$html = str_replace('href=""', 'href="' . config('app.url') . '/notes/A"', $html);
$mock = new MockHandler([
new Response(200, ['X-Foo' => 'Bar'], $html),

View file

@ -1,26 +1,25 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Jobs;
use Tests\TestCase;
use Ramsey\Uuid\Uuid;
use GuzzleHttp\Client;
use App\Models\Bookmark;
use GuzzleHttp\HandlerStack;
use App\Jobs\ProcessBookmark;
use GuzzleHttp\Psr7\Response;
use App\Services\BookmarkService;
use GuzzleHttp\Handler\MockHandler;
use App\Exceptions\InternetArchiveException;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use App\Jobs\ProcessBookmark;
use App\Models\Bookmark;
use App\Services\BookmarkService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Ramsey\Uuid\Uuid;
use Tests\TestCase;
class ProcessBookmarkJobTest extends TestCase
{
use DatabaseTransactions;
use RefreshDatabase;
public function test_screenshot_and_archive_link_are_saved()
/** @test */
public function screenshotAndArchiveLinkAreSavedByJob(): void
{
$bookmark = Bookmark::find(1);
$bookmark = Bookmark::factory()->create();
$uuid = Uuid::uuid4();
$service = $this->createMock(BookmarkService::class);
$service->method('saveScreenshot')
@ -38,15 +37,16 @@ class ProcessBookmarkJobTest extends TestCase
]);
}
public function test_exception_casesu_null_value_for_archive_link()
/** @test */
public function archiveLinkSavedAsNullWhenExceptionThrown(): void
{
$bookmark = Bookmark::find(1);
$bookmark = Bookmark::factory()->create();
$uuid = Uuid::uuid4();
$service = $this->createMock(BookmarkService::class);
$service->method('saveScreenshot')
->willReturn($uuid->toString());
$service->method('getArchiveLink')
->will($this->throwException(new InternetArchiveException));
->will($this->throwException(new InternetArchiveException()));
$this->app->instance(BookmarkService::class, $service);
$job = new ProcessBookmark($bookmark);

View file

@ -1,15 +1,18 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Jobs;
use Storage;
use Tests\TestCase;
use App\Jobs\ProcessMedia;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\ImageManager;
use Tests\TestCase;
class ProcessMediaJobTest extends TestCase
{
public function test_job_does_nothing_to_non_image()
/** @test */
public function nonMediaFilesAreNotSaved(): void
{
Storage::fake('s3');
$manager = app()->make(ImageManager::class);
@ -20,18 +23,20 @@ class ProcessMediaJobTest extends TestCase
$this->assertFalse(file_exists(storage_path('app') . '/file.txt'));
}
public function test_job_does_nothing_to_small_images()
/** @test */
public function smallImagesAreNotResized(): void
{
Storage::fake('s3');
$manager = app()->make(ImageManager::class);
Storage::disk('local')->put('aaron.png', file_get_contents(__DIR__.'/../../aaron.png'));
Storage::disk('local')->put('aaron.png', file_get_contents(__DIR__ . '/../../aaron.png'));
$job = new ProcessMedia('aaron.png');
$job->handle($manager);
$this->assertFalse(file_exists(storage_path('app') . '/aaron.png'));
}
public function test_large_images_have_smaller_files_created()
/** @test */
public function largeImagesHaveSmallerImagesCreated(): void
{
$manager = app()->make(ImageManager::class);
Storage::disk('local')->put('test-image.jpg', file_get_contents(__DIR__.'/../../test-image.jpg'));

View file

@ -1,25 +1,27 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Jobs;
use Tests\TestCase;
use App\Exceptions\RemoteContentNotFoundException;
use App\Jobs\ProcessWebMention;
use App\Jobs\SaveProfileImage;
use App\Models\Note;
use GuzzleHttp\Client;
use App\Models\WebMention;
use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use App\Jobs\SaveProfileImage;
use App\Jobs\ProcessWebMention;
use GuzzleHttp\Handler\MockHandler;
use Illuminate\FileSystem\FileSystem;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Queue;
use Jonnybarnes\WebmentionsParser\Parser;
use App\Exceptions\RemoteContentNotFoundException;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\TestCase;
class ProcessWebMentionJobTest extends TestCase
{
use DatabaseTransactions;
use RefreshDatabase;
protected function tearDown(): void
{
@ -30,7 +32,8 @@ class ProcessWebMentionJobTest extends TestCase
parent::tearDown();
}
public function test_for_exception_from_failure_to_get_webmention()
/** @test */
public function failureGettingWebmentionThrowsAnException(): void
{
$this->expectException(RemoteContentNotFoundException::class);
@ -41,24 +44,25 @@ class ProcessWebMentionJobTest extends TestCase
$handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]);
$note = Note::find(1);
$note = Note::factory()->create();
$source = 'https://example.org/mention/1/';
$job = new ProcessWebMention($note, $source);
$job->handle($parser, $client);
}
public function test_a_new_webmention_gets_saved()
/** @test */
public function newWebmentionGetsSavedByJob(): void
{
Queue::fake();
$parser = new Parser();
$html = <<<HTML
<div class="h-entry">
I liked <a class="u-like-of" href="/notes/1">a note</a>.
</div>
HTML;
<div class="h-entry">
I liked <a class="u-like-of" href="/notes/1">a note</a>.
</div>
HTML;
$html = str_replace('href="', 'href="' . config('app.url'), $html);
$mock = new MockHandler([
new Response(200, [], $html),
@ -66,7 +70,7 @@ HTML;
$handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]);
$note = Note::find(1);
$note = Note::factory()->create();
$source = 'https://example.org/mention/1/';
$job = new ProcessWebMention($note, $source);
@ -79,18 +83,19 @@ HTML;
]);
}
public function test_existing_webmention_gets_updated()
/** @test */
public function existingWebmentionGetsUpdatedByJob(): void
{
Queue::fake();
$parser = new Parser();
$html = <<<HTML
<div class="h-entry">
<p>In reply to <a class="u-in-reply-to" href="/notes/E">a note</a></p>
<div class="e-content">Updated reply</div>
</div>
HTML;
<div class="h-entry">
<p>In reply to <a class="u-in-reply-to" href="/notes/E">a note</a></p>
<div class="e-content">Updated reply</div>
</div>
HTML;
$html = str_replace('href="', 'href="' . config('app.url'), $html);
$mock = new MockHandler([
new Response(200, [], $html),
@ -98,7 +103,7 @@ HTML;
$handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]);
$note = Note::find(14);
$note = Note::factory()->create();
$source = 'https://aaronpk.localhost/reply/1';
$job = new ProcessWebMention($note, $source);
@ -108,27 +113,29 @@ HTML;
$this->assertDatabaseHas('webmentions', [
'source' => $source,
'type' => 'in-reply-to',
// phpcs:ignore Generic.Files.LineLength.TooLong
'mf2' => '{"rels": [], "items": [{"type": ["h-entry"], "properties": {"content": [{"html": "Updated reply", "value": "Updated reply"}], "in-reply-to": ["' . config('app.url') . '/notes/E"]}}], "rel-urls": []}',
]);
}
public function test_webmention_reply_gets_deleted()
/** @test */
public function webmentionReplyGetsDeletedWhenReplyToValueChanges(): void
{
$parser = new Parser();
$html = <<<HTML
<div class="h-entry">
<p>In reply to <a class="u-in-reply-to" href="https://other.com/notes/E">a note</a></p>
<div class="e-content">Replying to someone else</div>
</div>
HTML;
<div class="h-entry">
<p>In reply to <a class="u-in-reply-to" href="https://other.com/notes/E">a note</a></p>
<div class="e-content">Replying to someone else</div>
</div>
HTML;
$mock = new MockHandler([
new Response(200, [], $html),
]);
$handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]);
$note = Note::find(14);
$note = Note::factory()->create();
$source = 'https://example.org/reply/1';
$webmention = new WebMention();
$webmention->source = $source;
@ -148,23 +155,24 @@ HTML;
]);
}
public function test_webmention_like_gets_deleted()
/** @test */
public function webmentionLikeGetsDeletedWhenLikeOfValueChanges(): void
{
$parser = new Parser();
$html = <<<HTML
<div class="h-entry">
<p>In reply to <a class="u-like-of" href="https://other.com/notes/E">a note</a></p>
<div class="e-content">I like someone else now</div>
</div>
HTML;
<div class="h-entry">
<p>In reply to <a class="u-like-of" href="https://other.com/notes/E">a note</a></p>
<div class="e-content">I like someone else now</div>
</div>
HTML;
$mock = new MockHandler([
new Response(200, [], $html),
]);
$handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]);
$note = Note::find(14);
$note = Note::factory()->create();
$source = 'https://example.org/reply/1';
$webmention = new WebMention();
$webmention->source = $source;
@ -184,23 +192,24 @@ HTML;
]);
}
public function test_webmention_repost_gets_deleted()
/** @test */
public function webmentionRepostGetsDeletedWhenRepostOfValueChanges(): void
{
$parser = new Parser();
$html = <<<HTML
<div class="h-entry">
<p>In reply to <a class="u-repost-of" href="https://other.com/notes/E">a note</a></p>
<div class="e-content">Reposting someone else</div>
</div>
HTML;
<div class="h-entry">
<p>In reply to <a class="u-repost-of" href="https://other.com/notes/E">a note</a></p>
<div class="e-content">Reposting someone else</div>
</div>
HTML;
$mock = new MockHandler([
new Response(200, [], $html),
]);
$handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]);
$note = Note::find(14);
$note = Note::factory()->create();
$source = 'https://example.org/reply/1';
$webmention = new WebMention();
$webmention->source = $source;

View file

@ -1,16 +1,17 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Jobs;
use Tests\TestCase;
use App\Jobs\SaveProfileImage;
use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use App\Jobs\SaveProfileImage;
use GuzzleHttp\Handler\MockHandler;
use Jonnybarnes\WebmentionsParser\Authorship;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Jonnybarnes\WebmentionsParser\Exceptions\AuthorshipParserException;
use Tests\TestCase;
class SaveProfileImageJobTest extends TestCase
{
@ -22,18 +23,21 @@ class SaveProfileImageJobTest extends TestCase
}
parent::tearDown();
}
public function test_authorship_algo_exception_return_null()
/** @test */
public function authorshipAlgorithmReturnsNullOnException(): void
{
$mf = ['items' => []];
$authorship = $this->createMock(Authorship::class);
$authorship->method('findAuthor')
->will($this->throwException(new AuthorshipParserException));
->will($this->throwException(new AuthorshipParserException()));
$job = new SaveProfileImage($mf);
$this->assertNull($job->handle($authorship));
}
public function test_we_dont_process_twitter_images()
/** @test */
public function weDoNotProcessTwitterImages(): void
{
$mf = ['items' => []];
$author = [
@ -50,7 +54,8 @@ class SaveProfileImageJobTest extends TestCase
$this->assertNull($job->handle($authorship));
}
public function test_saving_of_remote_image()
/** @test */
public function remoteAuthorImagesAreSavedLocally(): void
{
$mock = new MockHandler([
new Response(200, ['Content-Type' => 'image/jpeg'], 'fake jpeg image'),
@ -74,7 +79,8 @@ class SaveProfileImageJobTest extends TestCase
$this->assertFileExists(public_path() . '/assets/profile-images/example.org/image');
}
public function test_copying_of_local_image()
/** @test */
public function localDefaultAuthorImageIsUsedAsFallback(): void
{
$mock = new MockHandler([
new Response(404),

View file

@ -1,19 +1,21 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Jobs;
use Tests\TestCase;
use App\Jobs\SendWebMentions;
use App\Models\Note;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use App\Jobs\SendWebMentions;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Handler\MockHandler;
use Illuminate\Foundation\Testing\RefreshDatabase;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use Tests\TestCase;
class SendWebMentionJobTest extends TestCase
{
public function test_dicover_endoint_method_on_self()
/** @test */
public function discoverWebmentionEndpointOnOwnDomain(): void
{
$note = new Note();
$job = new SendWebMentions($note);
@ -21,7 +23,8 @@ class SendWebMentionJobTest extends TestCase
$this->assertNull($job->discoverWebmentionEndpoint('/notes/tagged/test'));
}
public function test_discover_endpoint_gets_link_from_headers()
/** @test */
public function discoverWebmentionEndpointFromHeaderLinks(): void
{
$url = 'https://example.org/webmention';
$mock = new MockHandler([
@ -35,7 +38,8 @@ class SendWebMentionJobTest extends TestCase
$this->assertEquals($url, $job->discoverWebmentionEndpoint('https://example.org'));
}
public function test_discover_endpoint_correctly_parses_html()
/** @test */
public function discoverWebmentionEndpointFromHtmlLinkTags(): void
{
$html = '<link rel="webmention" href="https://example.org/webmention">';
$mock = new MockHandler([
@ -52,7 +56,8 @@ class SendWebMentionJobTest extends TestCase
);
}
public function test_discover_endpoint_correctly_parses_html_legacy()
/** @test */
public function discoverWebmentionEndpointFromLegacyHtmlMarkup(): void
{
$html = '<link rel="http://webmention.org/" href="https://example.org/webmention">';
$mock = new MockHandler([
@ -69,13 +74,15 @@ class SendWebMentionJobTest extends TestCase
);
}
public function test_empty_note_does_nothing()
/** @test */
public function ensureEmptyNoteDoesNotTriggerAnyActions(): void
{
$job = new SendWebMentions(new Note());
$this->assertNull($job->handle());
}
public function test_resolve_uri()
/** @test */
public function weResolveRelativeUris(): void
{
$uri = '/blog/post';
$base = 'https://example.org/';
@ -83,7 +90,8 @@ class SendWebMentionJobTest extends TestCase
$this->assertEquals('https://example.org/blog/post', $job->resolveUri($uri, $base));
}
public function test_the_job()
/** @test */
public function weSendAWebmentionForANote(): void
{
$html = '<link rel="http://webmention.org/" href="https://example.org/webmention">';
$mock = new MockHandler([
@ -98,6 +106,7 @@ class SendWebMentionJobTest extends TestCase
$note->note = 'Hi [Aaron](https://aaronparecki.com)';
$note->save();
$job = new SendWebMentions($note);
$this->assertNull($job->handle());
$job->handle();
$this->assertTrue(true);
}
}

View file

@ -1,24 +1,29 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Jobs;
use Tests\TestCase;
use GuzzleHttp\Client;
use App\Jobs\SyndicateBookmarkToTwitter;
use App\Models\Bookmark;
use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Handler\MockHandler;
use App\Jobs\SyndicateBookmarkToTwitter;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class SyndicateBookmarkToTwitterJobTest extends TestCase
{
use DatabaseTransactions;
use RefreshDatabase;
public function test_the_job()
/** @test */
public function weSendBookmarksToTwitter(): void
{
$faker = \Faker\Factory::create();
$randomNumber = $faker->randomNumber();
$json = json_encode([
'url' => 'https://twitter.com/123'
'url' => 'https://twitter.com/' . $randomNumber
]);
$mock = new MockHandler([
new Response(201, ['Content-Type' => 'application/json'], $json),
@ -26,13 +31,12 @@ class SyndicateBookmarkToTwitterJobTest extends TestCase
$handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]);
$bookmark = Bookmark::find(1);
$bookmark = Bookmark::factory()->create();
$job = new SyndicateBookmarkToTwitter($bookmark);
$job->handle($client);
$this->assertDatabaseHas('bookmarks', [
'id' => 1,
'syndicates' => '{"twitter": "https://twitter.com/123"}',
'syndicates' => '{"twitter": "https://twitter.com/' . $randomNumber . '"}',
]);
}
}

View file

@ -2,23 +2,26 @@
namespace Tests\Unit\Jobs;
use Tests\TestCase;
use App\Jobs\SyndicateNoteToTwitter;
use App\Models\Note;
use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Handler\MockHandler;
use App\Jobs\SyndicateNoteToTwitter;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class SyndicateNoteToTwitterJobTest extends TestCase
{
use DatabaseTransactions;
use RefreshDatabase;
public function test_the_job()
/** @test */
public function weSyndicateNotesToTwitter(): void
{
$faker = \Faker\Factory::create();
$randomNumber = $faker->randomNumber();
$json = json_encode([
'url' => 'https://twitter.com/i/web/status/123'
'url' => 'https://twitter.com/i/web/status/' . $randomNumber,
]);
$mock = new MockHandler([
new Response(201, ['Content-Type' => 'application/json'], $json),
@ -26,13 +29,12 @@ class SyndicateNoteToTwitterJobTest extends TestCase
$handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]);
$note = Note::find(1);
$note = Note::factory()->create();
$job = new SyndicateNoteToTwitter($note);
$job->handle($client);
$this->assertDatabaseHas('notes', [
'id' => 1,
'tweet_id' => '123',
'tweet_id' => $randomNumber,
]);
}
}

View file

@ -1,23 +1,27 @@
<?php
declare(strict_types=1);
namespace Tests\Unit;
use Tests\TestCase;
use App\Models\Like;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class LikesTest extends TestCase
{
use DatabaseTransactions;
use RefreshDatabase;
public function test_setting_author_url()
/** @test */
public function weCanSetTheAuthorUrl(): void
{
$like = new Like();
$like->author_url = 'https://joe.bloggs/';
$this->assertEquals('https://joe.bloggs', $like->author_url);
}
public function test_plaintext_like_content()
/** @test */
public function weDoNotModifyPlainTextContent(): void
{
$like = new Like();
$like->url = 'https://example.org/post/123';
@ -27,27 +31,28 @@ class LikesTest extends TestCase
$this->assertEquals('some plaintext content', $like->content);
}
public function test_html_like_content_is_filtered()
/** @test */
public function htmlLikeContentIsFiltered(): void
{
$htmlEvil = <<<HTML
<div class="h-entry">
<div class="e-content">
<p>Hello</p>
<img src="javascript:evil();" onload="evil();" />
</div>
</div>
HTML;
<div class="h-entry">
<div class="e-content">
<p>Hello</p>
<img src="javascript:evil();" onload="evil();" />
</div>
</div>
HTML;
$htmlFiltered = <<<HTML
<p>Hello</p>
<img />
HTML;
<p>Hello</p>
<img />
HTML;
$like = new Like();
$like->url = 'https://example.org/post/123';
$like->content = $htmlEvil;
$like->save();
// HTMLPurifer will leave the whitespace before the <img> tag
// trim it, saving whitespace in $htmlFilteres can get removed by text editors
// HTMLPurifier will leave the whitespace before the <img> tag
// trim it, saving whitespace in $htmlFiltered can get removed by text editors
$this->assertEquals($htmlFiltered, trim($like->content));
}
}

View file

@ -1,20 +1,28 @@
<?php
declare(strict_types=1);
namespace Tests\Unit;
use Tests\TestCase;
use App\Models\Media;
use App\Models\Note;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class MediaTest extends TestCase
{
public function test_get_note_from_media()
use RefreshDatabase;
/** @test */
public function getTheNoteThatMediaInstanceBelongsTo(): void
{
$media = Media::find(1);
$note = $media->note;
$this->assertInstanceOf('App\Models\Note', $note);
$media = Media::factory()->for(Note::factory())->create();
$this->assertInstanceOf(Note::class, $media->note);
}
public function test_media_absolute_url_returned_unmodified()
/** @test */
public function absoluteUrlsAreReturnedUnmodified(): void
{
$absoluteUrl = 'https://instagram-cdn.com/image/uuid';
$media = new Media();

View file

@ -1,17 +1,23 @@
<?php
declare(strict_types=1);
namespace Tests\Unit;
use Tests\TestCase;
use App\Models\MicropubClient;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class MicropbClientsTest extends TestCase
class MicropubClientsTest extends TestCase
{
public function test_notes_relationship()
use RefreshDatabase;
/** @test */
public function weCanGetNotesRelatingToClient(): void
{
$client = MicropubClient::find(1);
$client = MicropubClient::factory()->make();
$this->assertInstanceOf(Collection::class, $client->notes);
}
}

View file

@ -1,95 +1,153 @@
<?php
declare(strict_types=1);
namespace Tests\Unit;
use Tests\TestCase;
use App\Models\{Contact, Media, Note, Place, Tag};
use Illuminate\Filesystem\Filesystem;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Cache;
use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use App\Models\{Media, Note, Tag};
use GuzzleHttp\Handler\MockHandler;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\TestCase;
class NotesTest extends TestCase
{
use DatabaseTransactions;
use RefreshDatabase;
/**
* Test the getNoteAttribute method. This will then also call the
* relevant sub-methods.
*
* @test
* @return void
*/
public function test_get_note_attribute_method()
public function getNoteAttributeMethodCallsSubMethods(): void
{
// phpcs:ignore
$expected = '<p>Having a <a rel="tag" class="p-category" href="/notes/tagged/beer">#beer</a> at the local. 🍺</p>' . PHP_EOL;
$note = Note::find(2);
$note = Note::factory([
'note' => 'Having a #beer at the local. 🍺',
])->create();
$this->assertEquals($expected, $note->note);
}
/**
* Look for a default image in the contacts h-card for the makeHCards method.
*
* @test
* @return void
*/
public function test_default_image_used_in_makehcards_method()
public function defaultImageUsedAsFallbackInMakehcardsMethod(): void
{
// phpcs:ignore
$expected = '<p>Hi <span class="u-category h-card mini-h-card"><a class="u-url p-name" href="http://tantek.com">Tantek Çelik</a><span class="hovercard"> <a class="u-url" href="https://twitter.com/t"><img class="social-icon" src="/assets/img/social-icons/twitter.svg"> t</a><img class="u-photo" alt="" src="/assets/profile-images/default-image"></span></span></p>' . PHP_EOL;
$note = Note::find(4);
Contact::factory()->create([
'nick' => 'tantek',
'name' => 'Tantek Çelik',
'homepage' => 'http://tantek.com',
'twitter' => 't',
'facebook' => null,
]);
$note = Note::factory()->create([
'note' => 'Hi @tantek',
]);
$this->assertEquals($expected, $note->note);
}
/**
* Look for a specific profile image in the contacts h-card.
*
* @test
* @return void
*/
public function test_specific_profile_image_used_in_makehcards_method()
public function specificProfileImageUsedInMakehcardsMethod(): void
{
Contact::factory()->create([
'nick' => 'aaron',
'name' => 'Aaron Parecki',
'homepage' => 'https://aaronparecki.com',
'twitter' => null,
'facebook' => 123456,
]);
$fileSystem = new Filesystem();
$fileSystem->ensureDirectoryExists(public_path('/assets/profile-images/aaronparecki.com'));
if (!$fileSystem->exists(public_path('/assets/profile-images/aaronparecki.com/image'))) {
$fileSystem->copy('./tests/aaron.png', public_path('/assets/profile-images/aaronparecki.com/image'));
}
$note = Note::factory()->create([
'note' => 'Hi @aaron',
]);
// phpcs:ignore
$expected = '<p>Hi <span class="u-category h-card mini-h-card"><a class="u-url p-name" href="https://aaronparecki.com">Aaron Parecki</a><span class="hovercard"><a class="u-url" href="https://www.facebook.com/123456"><img class="social-icon" src="/assets/img/social-icons/facebook.svg"> Facebook</a> <img class="u-photo" alt="" src="/assets/profile-images/aaronparecki.com/image"></span></span></p>' . PHP_EOL;
$note = Note::find(5);
$this->assertEquals($expected, $note->note);
}
/**
* Look for twitter URL when theres no associated contact.
*
* @test
* @return void
*/
public function test_twitter_link_created_when_no_contact_found()
public function twitterLinkIsCreatedWhenNoContactFound(): void
{
$expected = '<p>Hi <a href="https://twitter.com/bob">@bob</a></p>' . PHP_EOL;
$note = Note::find(6);
$note = Note::factory()->create([
'note' => 'Hi @bob',
]);
$this->assertEquals($expected, $note->note);
}
public function test_shorturl_method()
/** @test */
public function shorturlMethodReturnsExpectedValue(): void
{
Note::factory(14)->create();
$note = Note::find(14);
$this->assertEquals(config('app.shorturl') . '/notes/E', $note->shorturl);
}
public function test_latlng_of_associated_place()
/** @test */
public function weGetLatitudeLongitudeValuesOfAssociatedPlaceOfNote(): void
{
$note = Note::find(2); // should be having beer at bridgewater note
$place = Place::factory()->create([
'latitude' => '53.4983',
'longitude' => '-2.3805',
]);
$note = Note::factory()->create([
'place_id' => $place->id,
]);
$this->assertEquals('53.4983', $note->latitude);
$this->assertEquals('-2.3805', $note->longitude);
}
public function test_latlng_returns_null_otherwise()
/** @test */
public function whenNoAssociatedPlaceWeGetNullForLatitudeLongitudeValues(): void
{
$note = Note::find(5);
$note = Note::factory()->create();
$this->assertNull($note->latitude);
$this->assertNull($note->longitude);
}
public function test_address_attribute_for_places()
/** @test */
public function weCanGetAddressAttributeForAssociatedPlace(): void
{
$note = Note::find(2);
$place = Place::factory()->create([
'name' => 'The Bridgewater Pub',
'latitude' => '53.4983',
'longitude' => '-2.3805',
]);
$note = Note::factory()->create([
'place_id' => $place->id,
]);
$this->assertEquals('The Bridgewater Pub', $note->address);
}
public function test_deleting_event_observer()
/** @test */
public function deletingNotesAlsoDeletesTagsViaTheEventObserver(): void
{
// first well create a temporary note to delete
$note = Note::create(['note' => 'temporary #temp']);
@ -104,7 +162,7 @@ class NotesTest extends TestCase
}
/** @test */
public function blank_note_should_be_saved_as_null()
public function saveBlankNotesAsNull(): void
{
$note = new Note(['note' => '']);
@ -112,11 +170,13 @@ class NotesTest extends TestCase
}
/** @test */
public function reverse_geocode_an_attraction()
public function reverseGeocodeAnAttraction(): void
{
// phpcs:disable Generic.Files.LineLength.TooLong
$json = <<<JSON
{"place_id":"198791063","licence":"Data © OpenStreetMap contributors, ODbL 1.0. https:\/\/osm.org\/copyright","osm_type":"relation","osm_id":"5208404","lat":"51.50084125","lon":"-0.142990166340849","display_name":"Buckingham Palace, Ambassador's Court, St. James's, Victoria, Westminster, London, Greater London, England, SW1E 6LA, United Kingdom","address":{"attraction":"Buckingham Palace","road":"Ambassador's Court","neighbourhood":"St. James's","suburb":"Victoria","city":"London","state_district":"Greater London","state":"England","postcode":"SW1E 6LA","country":"UK","country_code":"gb"},"boundingbox":["51.4997342","51.5019473","-0.143984","-0.1413002"]}
JSON;
{"place_id":"198791063","licence":"Data © OpenStreetMap contributors, ODbL 1.0. https:\/\/osm.org\/copyright","osm_type":"relation","osm_id":"5208404","lat":"51.50084125","lon":"-0.142990166340849","display_name":"Buckingham Palace, Ambassador's Court, St. James's, Victoria, Westminster, London, Greater London, England, SW1E 6LA, United Kingdom","address":{"attraction":"Buckingham Palace","road":"Ambassador's Court","neighbourhood":"St. James's","suburb":"Victoria","city":"London","state_district":"Greater London","state":"England","postcode":"SW1E 6LA","country":"UK","country_code":"gb"},"boundingbox":["51.4997342","51.5019473","-0.143984","-0.1413002"]}
JSON;
// phpcs:enable Generic.Files.LineLength.TooLong
$mock = new MockHandler([
new Response(200, ['Content-Type' => 'application/json'], $json),
]);
@ -128,15 +188,20 @@ JSON;
$note = new Note();
$address = $note->reverseGeoCode(51.50084, -0.14264);
$this->assertEquals('<span class="p-locality">Victoria, London</span>, <span class="p-country-name">UK</span>', $address);
$this->assertEquals(
'<span class="p-locality">Victoria, London</span>, <span class="p-country-name">UK</span>',
$address
);
}
/** @test */
public function reverse_geocode_a_suburb()
public function reverseGeocodeASuburb(): void
{
// phpcs:disable Generic.Files.LineLength.TooLong
$json = <<<JSON
{"place_id":"96518506","licence":"Data © OpenStreetMap contributors, ODbL 1.0. https:\/\/osm.org\/copyright","osm_type":"way","osm_id":"94107885","lat":"51.0225764535969","lon":"0.906664040464189","display_name":"Melon Lane, Newchurch, Shepway, Kent, South East, England, TN29 0AS, United Kingdom","address":{"road":"Melon Lane","suburb":"Newchurch","city":"Shepway","county":"Kent","state_district":"South East","state":"England","postcode":"TN29 0AS","country":"UK","country_code":"gb"},"boundingbox":["51.0140377","51.0371494","0.8873312","0.9109506"]}
JSON;
{"place_id":"96518506","licence":"Data © OpenStreetMap contributors, ODbL 1.0. https:\/\/osm.org\/copyright","osm_type":"way","osm_id":"94107885","lat":"51.0225764535969","lon":"0.906664040464189","display_name":"Melon Lane, Newchurch, Shepway, Kent, South East, England, TN29 0AS, United Kingdom","address":{"road":"Melon Lane","suburb":"Newchurch","city":"Shepway","county":"Kent","state_district":"South East","state":"England","postcode":"TN29 0AS","country":"UK","country_code":"gb"},"boundingbox":["51.0140377","51.0371494","0.8873312","0.9109506"]}
JSON;
// phpcs:enable Generic.Files.LineLength.TooLong
$mock = new MockHandler([
new Response(200, ['Content-Type' => 'application/json'], $json),
]);
@ -148,17 +213,22 @@ JSON;
$note = new Note();
$address = $note->reverseGeoCode(51.02, 0.91);
$this->assertEquals('<span class="p-locality">Newchurch, Shepway</span>, <span class="p-country-name">UK</span>', $address);
$this->assertEquals(
'<span class="p-locality">Newchurch, Shepway</span>, <span class="p-country-name">UK</span>',
$address
);
}
/** @test */
public function reverse_geocode_a_city()
public function reverseGeocodeACity(): void
{
// Note Ive modified this JSON response so it only contains the
// city the Uni is in
// phpcs:disable Generic.Files.LineLength.TooLong
$json = <<<JSON
{"place_id":"198561071","licence":"Data © OpenStreetMap contributors, ODbL 1.0. https:\/\/osm.org\/copyright","osm_type":"relation","osm_id":"1839026","lat":"53.46600455","lon":"-2.23300880782987","display_name":"University of Manchester - Main Campus, Brunswick Street, Curry Mile, Ardwick, Manchester, Greater Manchester, North West England, England, M13 9NR, United Kingdom","address":{"university":"University of Manchester - Main Campus","city":"Manchester","county":"Greater Manchester","state_district":"North West England","state":"England","postcode":"M13 9NR","country":"UK","country_code":"gb"},"boundingbox":["53.4598667","53.4716848","-2.2390346","-2.2262754"]}
JSON;
{"place_id":"198561071","licence":"Data © OpenStreetMap contributors, ODbL 1.0. https:\/\/osm.org\/copyright","osm_type":"relation","osm_id":"1839026","lat":"53.46600455","lon":"-2.23300880782987","display_name":"University of Manchester - Main Campus, Brunswick Street, Curry Mile, Ardwick, Manchester, Greater Manchester, North West England, England, M13 9NR, United Kingdom","address":{"university":"University of Manchester - Main Campus","city":"Manchester","county":"Greater Manchester","state_district":"North West England","state":"England","postcode":"M13 9NR","country":"UK","country_code":"gb"},"boundingbox":["53.4598667","53.4716848","-2.2390346","-2.2262754"]}
JSON;
// phpcs:enable Generic.Files.LineLength.TooLong
$mock = new MockHandler([
new Response(200, ['Content-Type' => 'application/json'], $json),
]);
@ -170,17 +240,22 @@ JSON;
$note = new Note();
$address = $note->reverseGeoCode(53.466277988406, -2.2304474827445);
$this->assertEquals('<span class="p-locality">Manchester</span>, <span class="p-country-name">UK</span>', $address);
$this->assertEquals(
'<span class="p-locality">Manchester</span>, <span class="p-country-name">UK</span>',
$address
);
}
/** @test */
public function reverse_geocode_a_county()
public function reverseGeocodeACounty(): void
{
// Note Ive removed everything below county to test for querires where
// thats all that is returned
// phpcs:disable Generic.Files.LineLength.TooLong
$json = <<<JSON
{"place_id":"98085404","licence":"Data © OpenStreetMap contributors, ODbL 1.0. https:\/\/osm.org\/copyright","osm_type":"way","osm_id":"103703318","lat":"51.0997470194065","lon":"0.609897771085209","display_name":"Biddenden, Ashford, Kent, South East, England, TN27 8ET, United Kingdom","address":{"county":"Kent","state_district":"South East","state":"England","postcode":"TN27 8ET","country":"UK","country_code":"gb"},"boundingbox":["51.0986632","51.104459","0.5954434","0.6167775"]}
JSON;
{"place_id":"98085404","licence":"Data © OpenStreetMap contributors, ODbL 1.0. https:\/\/osm.org\/copyright","osm_type":"way","osm_id":"103703318","lat":"51.0997470194065","lon":"0.609897771085209","display_name":"Biddenden, Ashford, Kent, South East, England, TN27 8ET, United Kingdom","address":{"county":"Kent","state_district":"South East","state":"England","postcode":"TN27 8ET","country":"UK","country_code":"gb"},"boundingbox":["51.0986632","51.104459","0.5954434","0.6167775"]}
JSON;
// phpcs:enable Generic.Files.LineLength.TooLong
$mock = new MockHandler([
new Response(200, ['Content-Type' => 'application/json'], $json),
]);
@ -196,13 +271,15 @@ JSON;
}
/** @test */
public function reverse_geocode_a_country()
public function reverseGeocodeACountry(): void
{
// Note Ive removed everything below country to test for querires where
// thats all that is returned
// phpcs:disable Generic.Files.LineLength.TooLong
$json = <<<JSON
{"place_id":"120553244","licence":"Data © OpenStreetMap contributors, ODbL 1.0. https:\/\/osm.org\/copyright","osm_type":"way","osm_id":"191508282","lat":"54.3004150140189","lon":"-9.39993720828084","display_name":"R314, Doonfeeny Lower, Ballycastle ED, Ballina, County Mayo, Connacht, Ireland","address":{"country":"Ireland","country_code":"ie"},"boundingbox":["54.2964027","54.3045856","-9.4337961","-9.3960403"]}
JSON;
{"place_id":"120553244","licence":"Data © OpenStreetMap contributors, ODbL 1.0. https:\/\/osm.org\/copyright","osm_type":"way","osm_id":"191508282","lat":"54.3004150140189","lon":"-9.39993720828084","display_name":"R314, Doonfeeny Lower, Ballycastle ED, Ballina, County Mayo, Connacht, Ireland","address":{"country":"Ireland","country_code":"ie"},"boundingbox":["54.2964027","54.3045856","-9.4337961","-9.3960403"]}
JSON;
// phpcs:enable Generic.Files.LineLength.TooLong
$mock = new MockHandler([
new Response(200, ['Content-Type' => 'application/json'], $json),
]);
@ -218,15 +295,15 @@ JSON;
}
/** @test */
public function add_image_element_to_note_content()
public function addImageElementToNoteContentWhenMediaAssociated(): void
{
$media = new Media([
$media = Media::factory()->create([
'type' => 'image',
'path' => 'test.png']
);
$media->save();
$note = new Note(['note' => 'A nice image']);
$note->save();
'path' => 'test.png',
]);
$note = Note::factory()->create([
'note' => 'A nice image',
]);
$note->media()->save($media);
$expected = "<p>A nice image</p>
@ -235,15 +312,15 @@ JSON;
}
/** @test */
public function add_video_element_to_note_content()
public function addVideoElementToNoteContentWhenMediaAssociated(): void
{
$media = new Media([
$media = Media::factory()->create([
'type' => 'video',
'path' => 'test.mkv']
);
$media->save();
$note = new Note(['note' => 'A nice video']);
$note->save();
'path' => 'test.mkv',
]);
$note = Note::factory()->create([
'note' => 'A nice video',
]);
$note->media()->save($media);
$expected = "<p>A nice video</p>
@ -252,15 +329,15 @@ JSON;
}
/** @test */
public function add_audio_element_to_note_content()
public function addAudioElementToNoteContentWhenMediaAssociated(): void
{
$media = new Media([
$media = Media::factory()->create([
'type' => 'audio',
'path' => 'test.flac']
);
$media->save();
$note = new Note(['note' => 'Some nice audio']);
$note->save();
'path' => 'test.flac',
]);
$note = Note::factory()->create([
'note' => 'Some nice audio',
]);
$note->media()->save($media);
$expected = "<p>Some nice audio</p>
@ -268,26 +345,31 @@ JSON;
$this->assertEquals($expected, $note->content);
}
/** @test */
public function blank_note_content()
/**
* @test
* @todo Why do we need to provide text?
*/
public function provideTextForBlankContent(): void
{
$note = new Note();
$this->assertEquals('A blank note', $note->content);
}
/** @test */
public function twitter_content_is_null_when_oembed_error_occurs()
// @todo Sort out twitter requests
/** @test
public function setTwitterContentToNullWhenOembedErrorOccurs(): void
{
$note = new Note();
$note->in_reply_to = 'https://twitter.com/search';
$this->assertNull($note->twitter);
}
}*/
public function test_markdown_gets_converted()
/** @test */
public function markdownContentGetsConverted(): void
{
$note = Note::create([
$note = Note::factory()->create([
'note' => 'The best search engine? https://duckduckgo.com',
]);
@ -296,4 +378,46 @@ JSON;
$note->note
);
}
/**
* For now, just reply on a cached object instead of actually querying Twitter.
*
* @test
*/
public function checkInReplyToIsTwitterLink(): void
{
$tempContent = (object) [
'html' => 'something random',
];
Cache::put('933662564587855877', $tempContent);
$note = Note::factory()->create([
'in_reply_to' => 'https://twitter.com/someRando/status/933662564587855877'
]);
$this->assertSame($tempContent, $note->twitter);
}
/** @test */
public function latitudeAndLongitudeCanBeParsedFromPlainLocation(): void
{
$note = Note::factory()->create([
'location' => '1.23,4.56',
]);
$this->assertSame(1.23, $note->latitude);
$this->assertSame(4.56, $note->longitude);
}
/** @test */
public function addressAttributeCanBeRetrievedFromPlainLocation(): void
{
Cache::put('1.23,4.56', '<span class="p-country-name">Antarctica</span>');
$note = Note::factory()->create([
'location' => '1.23,4.56',
]);
$this->assertSame('<span class="p-country-name">Antarctica</span>', $note->address);
}
}

View file

@ -1,56 +1,77 @@
<?php
declare(strict_types=1);
namespace Tests\Unit;
use Tests\TestCase;
use App\Models\Note;
use App\Models\Place;
use App\Services\PlaceService;
use MStaack\LaravelPostgis\Geometries\Point;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use InvalidArgumentException;
use Tests\TestCase;
class PlacesTest extends TestCase
{
use DatabaseTransactions;
use RefreshDatabase;
public function test_notes_method()
/** @test */
public function canRetrieveAssociatedNotes(): void
{
$place = Place::find(1);
$place = Place::factory()->create();
Note::factory(5)->create([
'place_id' => $place->id,
]);
$this->assertInstanceOf(Collection::class, $place->notes);
$this->assertCount(5, $place->notes);
}
/**
* Test the near method returns a collection.
*
* @return void
*/
public function test_near_method()
/** @test */
public function nearMethodReturnsCollection(): void
{
$nearby = Place::near((object) ['latitude' => 53.5, 'longitude' => -2.38], 1000)->get();
Place::factory()->create([
'name' => 'The Bridgewater Pub',
'latitude' => 53.4983,
'longitude' => -2.3805,
]);
$nearby = Place::near((object) ['latitude' => 53.5, 'longitude' => -2.38])->get();
$this->assertEquals('the-bridgewater-pub', $nearby[0]->slug);
}
public function test_longurl_method()
/** @test */
public function getLongurl(): void
{
$place = Place::find(1);
$place = Place::factory()->create([
'name' => 'The Bridgewater Pub',
]);
$this->assertEquals(config('app.url') . '/places/the-bridgewater-pub', $place->longurl);
}
public function test_uri_method()
/** @test */
public function getShorturl()
{
$place = Place::find(1);
$this->assertEquals(config('app.url') . '/places/the-bridgewater-pub', $place->uri);
}
public function test_shorturl_method()
{
$place = Place::find(1);
$place = Place::factory()->create([
'name' => 'The Bridgewater Pub',
]);
$this->assertEquals(config('app.shorturl') . '/places/the-bridgewater-pub', $place->shorturl);
}
public function test_service_returns_existing_place()
/** @test */
public function getUri(): void
{
$place = Place::factory()->create([
'name' => 'The Bridgewater Pub',
]);
$this->assertEquals(config('app.url') . '/places/the-bridgewater-pub', $place->uri);
}
/** @test */
public function placeServiceReturnsExistingPlaceBasedOnExternalUrlsSearch(): void
{
Place::factory(10)->create();
$place = new Place();
$place->name = 'Temp Place';
$place->latitude = 37.422009;
@ -63,32 +84,48 @@ class PlacesTest extends TestCase
'url' => ['https://www.openstreetmap.org/way/1234'],
]
]);
$this->assertInstanceOf('App\Models\Place', $ret); // a place was returned
$this->assertEquals(12, count(Place::all())); // still 2 places
$this->assertInstanceOf('App\Models\Place', $ret);
$this->assertCount(11, Place::all());
}
public function test_service_requires_name()
/** @test */
public function placeServiceRequiresNameWhenCreatingNewPlace(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Missing required name');
$service = new PlaceService();
$service->createPlaceFromCheckin(['foo' => 'bar']);
}
public function test_service_requires_latitude()
/** @test */
public function placeServiceRequiresLatitudeWhenCreatingNewPlace(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Missing required longitude/latitude');
$service = new PlaceService();
$service->createPlaceFromCheckin(['properties' => ['name' => 'bar']]);
}
public function test_updating_external_urls()
/** @test */
public function placeServiceCanUpdateExternalUrls(): void
{
$place = Place::find(1);
$place = Place::factory()->create([
'name' => 'The Bridgewater Pub',
'latitude' => 53.4983,
'longitude' => -2.3805,
'external_urls' => '',
]);
$place->external_urls = 'https://www.openstreetmap.org/way/987654';
$place->external_urls = 'https://foursquare.com/v/123435/the-bridgewater-pub';
$place->save();
$place->external_urls = 'https://bridgewater.pub';
$this->assertEquals('{"osm":"https:\/\/www.openstreetmap.org\/way\/987654","foursquare":"https:\/\/foursquare.com\/v\/123435\/the-bridgewater-pub","default":"https:\/\/bridgewater.pub"}', $place->external_urls);
$this->assertEquals(json_encode([
'default' => 'https://bridgewater.pub',
'osm' => 'https://www.openstreetmap.org/way/987654',
'foursquare' => 'https://foursquare.com/v/123435/the-bridgewater-pub',
]), $place->external_urls);
}
}

View file

@ -1,21 +1,55 @@
<?php
declare(strict_types=1);
namespace Tests\Unit;
use App\Models\Bookmark;
use App\Models\Note;
use App\Models\Tag;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class TagsTest extends TestCase
{
public function test_notes_method()
use RefreshDatabase;
/** @test */
public function canGetAssociatedNotes(): void
{
$tag = Tag::find(1); // should be beer tag
$this->assertEquals(1, count($tag->notes));
$note = Note::factory()->create();
$tag = Tag::factory()->create();
$note->tags()->save($tag);
$this->assertCount(1, $tag->notes);
}
public function test_bookmarks_method()
/** @test */
public function canGetAssociatedBookmarks(): void
{
$tag = Tag::find(5); //should be first random tag for bookmarks
$this->assertEquals(1, count($tag->bookmarks));
$bookmark = Bookmark::factory()->create();
$tag = Tag::factory()->create();
$bookmark->tags()->save($tag);
$this->assertCount(1, $tag->bookmarks);
}
/**
* @test
* @dataProvider tagsProvider
* @param string $input
* @param string $expected
*/
public function canNormalize(string $input, string $expected): void
{
$this->assertSame($expected, Tag::normalize($input));
}
public function tagsProvider(): array
{
return [
['test', 'test'],
['Test', 'test'],
['Tést', 'test'],
['MultiWord', 'multiword'],
];
}
}

View file

@ -1,32 +1,46 @@
<?php
declare(strict_types=1);
namespace Tests\Unit;
use Codebird\Codebird;
use Tests\TestCase;
use App\Models\Note;
use App\Models\WebMention;
use Thujohn\Twitter\Facades\Twitter;
use Codebird\Codebird;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Cache;
use Tests\TestCase;
class WebMentionTest extends TestCase
{
public function test_commentable_method()
use RefreshDatabase;
/** @test */
public function commentableMethodLinksToNotes(): void
{
$webmention = WebMention::find(1);
$this->assertInstanceOf('App\Models\Note', $webmention->commentable);
$note = Note::factory()->create();
$webmention = WebMention::factory()->make([
'commentable_id' => $note->id,
'commentable_type' => Note::class,
]);
$this->assertInstanceOf(Note::class, $webmention->commentable);
}
public function test_published_attribute_when_no_relavent_mf2()
/** @test */
public function publishedAttributeUsesUpdatedAtWhenNoRelevantMf2Data(): void
{
$webmention = new WebMention();
$updated_at = carbon()->now();
$updated_at = Carbon::now();
$webmention->updated_at = $updated_at;
$this->assertEquals($updated_at->toDayDateTimeString(), $webmention->published);
}
public function test_published_attribute_when_error_parsing_mf2()
/** @test */
public function publishedAttributeUsesUpdatedAtWhenErrorParsingMf2Data(): void
{
$webmention = new WebMention();
$updated_at = carbon()->now();
$updated_at = Carbon::now();
$webmention->updated_at = $updated_at;
$webmention->mf2 = json_encode([
'items' => [[
@ -40,12 +54,8 @@ class WebMentionTest extends TestCase
$this->assertEquals($updated_at->toDayDateTimeString(), $webmention->published);
}
/**
* Test a correct profile link is formed from a generic URL.
*
* @return void
*/
public function test_create_photo_link_with_non_cached_image()
/** @test */
public function createPhotoLinkDoesNothingWithGenericUrlAndNoLocallySavedImage(): void
{
$webmention = new WebMention();
$homepage = 'https://example.org/profile.png';
@ -53,12 +63,8 @@ class WebMentionTest extends TestCase
$this->assertEquals($expected, $webmention->createPhotoLink($homepage));
}
/**
* Test a correct profile link is formed from a generic URL (cached).
*
* @return void
*/
public function test_create_photo_link_with_cached_image()
/** @test */
public function createPhotoLinkReturnsLocallySavedImageUrlIfItExists(): void
{
$webmention = new WebMention();
$homepage = 'https://aaronparecki.com/profile.png';
@ -66,12 +72,8 @@ class WebMentionTest extends TestCase
$this->assertEquals($expected, $webmention->createPhotoLink($homepage));
}
/**
* Test a correct profile link is formed from a twitter URL.
*
* @return void
*/
public function test_create_photo_link_with_twimg_profile_image_url()
/** @test */
public function createPhotoLinkDealsWithSpecialCaseOfDirectTwitterPhotoLinks(): void
{
$webmention = new WebMention();
$twitterProfileImage = 'http://pbs.twimg.com/1234';
@ -79,12 +81,8 @@ class WebMentionTest extends TestCase
$this->assertEquals($expected, $webmention->createPhotoLink($twitterProfileImage));
}
/**
* Test `null` is returned for a twitter profile.
*
* @return void
*/
public function test_create_photo_link_with_cached_twitter_url()
/** @test */
public function createPhotoLinkReturnsCachedTwitterPhotoLinks(): void
{
$webmention = new WebMention();
$twitterURL = 'https://twitter.com/example';
@ -93,10 +91,12 @@ class WebMentionTest extends TestCase
$this->assertEquals($expected, $webmention->createPhotoLink($twitterURL));
}
public function test_create_photo_link_with_noncached_twitter_url()
/** @test */
public function createPhotoLinkResolvesTwitterPhotoLinks(): void
{
$info = new \stdClass();
$info->profile_image_url_https = 'https://pbs.twimg.com/static_profile_link.jpg';
$info = (object) [
'profile_image_url_https' => 'https://pbs.twimg.com/static_profile_link.jpg',
];
$codebirdMock = $this->getMockBuilder(Codebird::class)
->addMethods(['users_show'])
->getMock();
@ -117,13 +117,15 @@ class WebMentionTest extends TestCase
$this->assertEquals($expected, $webmention->createPhotoLink($twitterURL));
}
public function test_get_reply_attribute_returns_null()
/** @test */
public function getReplyAttributeDefaultsToNull(): void
{
$webmention = new WebMention();
$this->assertNull($webmention->reply);
}
public function test_get_reply_attribute_with_mf2_without_html_returns_null()
/** @test */
public function getReplyAttributeWithMf2WithoutHtmlReturnsNull(): void
{
$webmention = new WebMention();
$webmention->mf2 = json_encode(['no_html' => 'found_here']);

View file

@ -6,5 +6,5 @@
</div>
<p>Posted by:</p>
<div class="h-card">
<a class="u-url" href="http://tantek.com"><span class="p-name">Tantek Celik</span></a>
<a class="u-url" href="http://tantek.com"><span class="p-name">Tantek Çelik</span></a>
</div>

View file

@ -1,39 +0,0 @@
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param HTTPS $https if_not_empty;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
#fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# fastcgi_intercept_errors on;
fastcgi_ignore_client_abort off;
fastcgi_connect_timeout 60;
fastcgi_send_timeout 1800;
fastcgi_read_timeout 1800;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
fastcgi_keep_conn on;

View file

@ -1,47 +0,0 @@
#!/bin/bash
set -e
set -x
DIR=$(realpath $(dirname "$0"))
USER=$(whoami)
PHP_VERSION=$(phpenv version-name)
ROOT=$(realpath "$DIR/..")
HTTP_PORT=8000
PHP_PORT=9000
SERVER="/tmp/php.sock"
function tpl {
sed \
-e "s|{DIR}|$DIR|g" \
-e "s|{USER}|$USER|g" \
-e "s|{PHP_VERSION}|$PHP_VERSION|g" \
-e "s|{ROOT}|$ROOT|g" \
-e "s|{HTTP_PORT}|$HTTP_PORT|g" \
-e "s|{PHP_PORT}|$PHP_PORT|g" \
-e "s|{SERVER}|$SERVER|g" \
< $1 > $2
}
# Make some working directories.
mkdir "$DIR/nginx"
mkdir "$DIR/nginx/sites-enabled"
mkdir "$DIR/var"
PHP_FPM_BIN="$HOME/.phpenv/versions/$PHP_VERSION/sbin/php-fpm"
PHP_FPM_CONF="$DIR/nginx/php-fpm.conf"
# Build the php-fpm.conf.
tpl "$DIR/php-fpm.tpl.conf" "$PHP_FPM_CONF"
# Start php-fpm
"$PHP_FPM_BIN" --fpm-config "$PHP_FPM_CONF"
# Build the default nginx config files.
tpl "$DIR/nginx.tpl.conf" "$DIR/nginx/nginx.conf"
tpl "$DIR/fastcgi.tpl.conf" "$DIR/nginx/fastcgi.conf"
tpl "$DIR/longurl.tpl.conf" "$DIR/nginx/sites-enabled/longurl.conf"
tpl "$DIR/shorturl.tpl.conf" "$DIR/nginx/sites-enabled/shorturl.conf"
# Start nginx.
nginx -c "$DIR/nginx/nginx.conf"

View file

@ -1,21 +0,0 @@
server {
listen {HTTP_PORT} default_server;
listen [::]:{HTTP_PORT} default_server ipv6only=on;
server_name jonnybarnes.localhost;
root {ROOT}/public;
index index.php;
access_log /tmp/access.log;
error_log /tmp/error.log;
location / {
# First attempt to serve request as file, then as directory, then fall back to index.php.
try_files $uri $uri/ /index.php$is_args$args;
}
location ~* "\.php(/|$)" {
include fastcgi.conf;
fastcgi_pass php;
}
}

View file

@ -1,53 +0,0 @@
worker_processes 1;
error_log /tmp/error.log;
pid /tmp/nginx.pid;
events {
worker_connections 1024;
}
http {
# Set an array of temp and cache file options that will otherwise default to restricted locations accessible only to root.
client_body_temp_path /tmp/client_body;
fastcgi_temp_path /tmp/fastcgi_temp;
proxy_temp_path /tmp/proxy_temp;
scgi_temp_path /tmp/scgi_temp;
uwsgi_temp_path /tmp/uwsgi_temp;
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# Logging Settings
##
access_log /tmp/access.log;
error_log /tmp/error.log;
##
# Gzip Settings
##
gzip on;
gzip_disable "msie6";
##
# Virtual Host Configs
##
# include {DIR}/nginx/conf.d/*.conf;
include {DIR}/nginx/sites-enabled/*;
upstream php {
server 127.0.0.1:{PHP_PORT};
}
}

View file

@ -1,10 +0,0 @@
[global]
error_log = /tmp/php-fpm.error.log
[travis]
user = {USER}
listen = {PHP_PORT}
listen.mode = 0666
pm = static
pm.max_children = 5
php_admin_value[memory_limit] = 32M

View file

@ -1,21 +0,0 @@
server {
listen {HTTP_PORT};
listen [::]:{HTTP_PORT};
server_name jmb.localhost;
root {ROOT}/public;
index index.php;
access_log /tmp/access.log;
error_log /tmp/error.log;
location / {
# First attempt to serve request as file, then as directory, then fall back to index.php.
try_files $uri $uri/ /index.php$is_args$args;
}
location ~* "\.php(/|$)" {
include fastcgi.conf;
fastcgi_pass php;
}
}