Skip to content

Commit e12c565

Browse files
committed
API set up and test complete
1 parent b836cc3 commit e12c565

File tree

10 files changed

+193
-33
lines changed

10 files changed

+193
-33
lines changed

.env.example

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
APP_NAME=Laravel
1+
APP_NAME='WordPress Plugin Management System'
22
APP_ENV=local
33
APP_KEY=
44
APP_DEBUG=true
55
APP_URL=http://localhost
66

7+
DISCORD_HOOK=
8+
79
LOG_CHANNEL=daily
810
LOG_DEPRECATIONS_CHANNEL=null
911
LOG_LEVEL=debug

app/Models/PluginUser.php

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use DB;
6+
use Http;
7+
use Illuminate\Database\Eloquent\Factories\HasFactory;
8+
use Illuminate\Database\Eloquent\Model;
9+
10+
class PluginUser extends Model
11+
{
12+
use HasFactory;
13+
14+
public const ACTIVE = 1;
15+
public const INACTIVE = 2;
16+
public const UNINSTALL = 3;
17+
18+
public $timestamps = false;
19+
protected $fillable = [
20+
'name',
21+
'version',
22+
'website',
23+
'plugins',
24+
'server',
25+
'status',
26+
'activated_at',
27+
'deactivated_at',
28+
'uninstalled_at'
29+
];
30+
protected $casts = [
31+
'plugins' => 'array',
32+
'server' => 'array'
33+
];
34+
35+
public function getStatusAttribute($status): string
36+
{
37+
return match ($status) {
38+
self::ACTIVE => 'activate',
39+
self::INACTIVE => 'deactivate',
40+
default => 'uninstalled',
41+
};
42+
}
43+
}
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Database\Factories;
4+
5+
use Illuminate\Database\Eloquent\Factories\Factory;
6+
7+
/**
8+
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\PluginUser>
9+
*/
10+
class PluginUserFactory extends Factory
11+
{
12+
/**
13+
* Define the model's default state.
14+
*
15+
* @return array<string, mixed>
16+
*/
17+
public function definition(): array
18+
{
19+
return [
20+
//
21+
];
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*/
12+
public function up(): void
13+
{
14+
Schema::create('plugin_users', function (Blueprint $table) {
15+
$table->id();
16+
$table->string('name', 20)->nullable();
17+
$table->string('version',10)->default('1.0.0');
18+
$table->string('website',100)->nullable();
19+
$table->text('plugins')->nullable();
20+
$table->text('server')->nullable();
21+
$table->tinyInteger('status')->default(1);
22+
$table->date('activated_at')->default(now());
23+
$table->date('deactivated_at')->nullable();
24+
$table->date('uninstalled_at')->nullable();
25+
});
26+
}
27+
28+
/**
29+
* Reverse the migrations.
30+
*/
31+
public function down(): void
32+
{
33+
Schema::dropIfExists('plugin_users');
34+
}
35+
};

phpunit.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
<env name="APP_ENV" value="testing"/>
1414
<env name="BCRYPT_ROUNDS" value="4"/>
1515
<env name="CACHE_DRIVER" value="array"/>
16-
<!-- <env name="DB_CONNECTION" value="sqlite"/> -->
17-
<!-- <env name="DB_DATABASE" value=":memory:"/> -->
16+
<env name="DB_CONNECTION" value="sqlite"/>
17+
<env name="DB_DATABASE" value=":memory:"/>
1818
<env name="MAIL_MAILER" value="array"/>
1919
<env name="QUEUE_CONNECTION" value="sync"/>
2020
<env name="SESSION_DRIVER" value="array"/>

routes/api.php

+63-17
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,65 @@
11
<?php
22

3-
use Illuminate\Http\Request;
4-
use Illuminate\Support\Facades\Route;
5-
6-
/*
7-
|--------------------------------------------------------------------------
8-
| API Routes
9-
|--------------------------------------------------------------------------
10-
|
11-
| Here is where you can register API routes for your application. These
12-
| routes are loaded by the RouteServiceProvider and all of them will
13-
| be assigned to the "api" middleware group. Make something great!
14-
|
15-
*/
16-
17-
Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
18-
return $request->user();
19-
});
3+
use App\Models\PluginUser;
4+
5+
Route::post('org/support', static function (\Illuminate\Http\Request $request) {
6+
$origin = '';
7+
$parsed_domain = parse_url($request->url);
8+
9+
if (isset($parsed_domain['scheme'])) {
10+
$origin .= $parsed_domain['scheme'] . '://';
11+
}
12+
if (isset($parsed_domain['host'])) {
13+
$origin .= $parsed_domain['host'];
14+
}
15+
if (isset($parsed_domain['port'])) {
16+
$origin .= ':' . $parsed_domain['port'];
17+
}
18+
19+
if (app()->isProduction() && preg_match('/^.*\.(dev|test|local|localhost)$/', $origin)) {
20+
info($request);
21+
return response('ok', 200);
22+
}
23+
24+
try {
25+
[$name, $version] = explode(':', $request->name);
26+
$plugins = $request->plugins;
27+
$server = $request->server_info;
28+
29+
$user = PluginUser::where('name', $name)
30+
->where('version', $version)
31+
->where('website', $origin)
32+
->first();
33+
34+
if (!$user) {
35+
$user = new PluginUser();
36+
}
37+
38+
if ($request->action === 'activate') {
39+
$user->name = $name;
40+
$user->version = $version;
41+
$user->website = $origin;
42+
$user->status = PluginUser::ACTIVE;
43+
$user->activated_at = now();
44+
} elseif ($request->action === 'deactivate') {
45+
$user->status = PluginUser::INACTIVE;
46+
$user->deactivated_at = now();
47+
} elseif ($request->action === 'uninstall') {
48+
$user->status = PluginUser::UNINSTALL;
49+
$user->uninstalled_at = now();
50+
}
51+
52+
$user->plugins = $plugins;
53+
$user->server = $server;
54+
$user->save();
55+
56+
return response('ok', 200);
57+
} catch (Exception $exception) {
58+
Log::alert($request->all());
59+
Log::alert($exception->getMessage());
60+
Log::info($exception->getTrace()[0]);
61+
62+
return response('error', 500);
63+
}
64+
65+
})->name('org.support');

tests/Feature/ApiTest.php

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
4+
use function Pest\Laravel\assertDatabaseHas;
5+
use function Pest\Laravel\postJson;
6+
7+
it('can store plugin information', function () {
8+
// Send a request with prepared data.
9+
$data = [
10+
'name' => 'test-plugin:1.0.0',
11+
'url' => 'https://example.com',
12+
'plugins' => array('test-plugin'),
13+
'server' => array('7.0.0'),
14+
'action' => 'activate'
15+
];
16+
$response = postJson('/api/org/support', $data);
17+
$response->assertSuccessful();
18+
$response->assertSeeText('ok');
19+
assertDatabaseHas('plugin_users', array(
20+
'name' => 'test-plugin',
21+
'website' => 'https://example.com',
22+
));
23+
});

tests/Feature/ExampleTest.php

-7
This file was deleted.

tests/Pest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
uses(
1515
Tests\TestCase::class,
16-
// Illuminate\Foundation\Testing\RefreshDatabase::class,
16+
Illuminate\Foundation\Testing\RefreshDatabase::class,
1717
)->in('Feature');
1818

1919
/*

tests/Unit/ExampleTest.php

-5
This file was deleted.

0 commit comments

Comments
 (0)