Compare commits
8 Commits
f0e7be4476
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| e3b656a036 | |||
| 2d1c5e6397 | |||
| a99108649c | |||
| 946d6b0e68 | |||
| 5391eb465f | |||
| c4922b90a3 | |||
| a70b7670e7 | |||
| 6abb417430 |
42
README.md
Normal file
42
README.md
Normal file
@@ -0,0 +1,42 @@
|
||||
# Perceptron and neuronal networks
|
||||
|
||||
Using Laravel and Vue JS
|
||||
|
||||
## Installation
|
||||
|
||||
1. Install PHP, Composer and NodeJs
|
||||
- With Herd
|
||||
<https://herd.laravel.com/windows>
|
||||
|
||||
- Using a single command from the [Laravel installation page](https://laravel.com/docs/12.x/installation)
|
||||
|
||||
```powershell
|
||||
# Run as administrator...
|
||||
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://php.new/install/windows/8.4'))
|
||||
```
|
||||
|
||||
- Manually :
|
||||
1. PHP
|
||||
<https://www.php.net/downloads.php>
|
||||
2. Composer
|
||||
<https://getcomposer.org/download/>
|
||||
3. NodeJs (Node + NPM)
|
||||
<https://nodejs.org/en/download>
|
||||
|
||||
2. Install dependencies
|
||||
|
||||
```shell
|
||||
composer install
|
||||
```
|
||||
|
||||
## Running the project
|
||||
|
||||
There is a script inside `composer.json` that will launch each part of the application in parallel.
|
||||
|
||||
To run this script :
|
||||
|
||||
```shell
|
||||
composer run dev
|
||||
```
|
||||
|
||||
And go with your favorite browser to <http://127.0.0.1:8000/>
|
||||
@@ -7,7 +7,6 @@ use Illuminate\Broadcasting\InteractsWithSockets;
|
||||
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
|
||||
use Illuminate\Foundation\Events\Dispatchable;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class PerceptronTrainingIteration implements ShouldBroadcast
|
||||
{
|
||||
@@ -17,7 +16,7 @@ class PerceptronTrainingIteration implements ShouldBroadcast
|
||||
* Create a new event instance.
|
||||
*/
|
||||
public function __construct(
|
||||
public array $iterations, // ["iteration" => int, "exampleIndex" => int, "error" => float, "synaptic_weights" => array]
|
||||
public array $iterations, // ["epoch" => int, "exampleIndex" => int, "error" => float, "synaptic_weights" => array]
|
||||
public string $sessionId,
|
||||
public string $trainingId,
|
||||
)
|
||||
|
||||
@@ -8,6 +8,7 @@ use App\Models\SimpleBinaryPerceptronTraining;
|
||||
use App\Services\DataSetReader;
|
||||
use App\Services\ISynapticWeightsProvider;
|
||||
use App\Services\PerceptronIterationEventBuffer;
|
||||
use App\Services\PerceptronLimitedEpochEventBuffer;
|
||||
use App\Services\ZeroSynapticWeights;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
@@ -91,8 +92,15 @@ class PerceptronController extends Controller
|
||||
case 'simple':
|
||||
$dataset['defaultLearningRate'] = 0.015;
|
||||
break;
|
||||
case 'gradientdescent':
|
||||
$dataset['defaultLearningRate'] = 0.001;
|
||||
$dataset['defaultMinError'] = 2.0;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case 'table_2_11':
|
||||
$dataset['defaultMinError'] = 1.0;
|
||||
break;
|
||||
}
|
||||
$datasets[] = $dataset;
|
||||
}
|
||||
@@ -121,9 +129,14 @@ class PerceptronController extends Controller
|
||||
$synapticWeightsProvider = new ZeroSynapticWeights();
|
||||
}
|
||||
|
||||
$iterationEventBuffer = new PerceptronIterationEventBuffer($sessionId, $trainingId);
|
||||
if ($maxIterations > config('perceptron.limited_broadcast_iterations')) {
|
||||
$iterationsInterval = (int)($maxIterations / config('perceptron.limited_broadcast_iterations'));
|
||||
$iterationEventBuffer = new PerceptronLimitedEpochEventBuffer($sessionId, $trainingId, $iterationsInterval);
|
||||
}
|
||||
|
||||
$dataSetReader = $this->getDataSetReader($dataSet);
|
||||
|
||||
$iterationEventBuffer = new PerceptronIterationEventBuffer($sessionId, $trainingId);
|
||||
|
||||
$networkTraining = match ($perceptronType) {
|
||||
'simple' => new SimpleBinaryPerceptronTraining($dataSetReader, $learningRate, $maxIterations, $synapticWeightsProvider, $iterationEventBuffer, $sessionId, $trainingId),
|
||||
|
||||
@@ -4,6 +4,7 @@ namespace App\Models;
|
||||
|
||||
use App\Events\PerceptronTrainingEnded;
|
||||
use App\Services\DataSetReader;
|
||||
use App\Services\IPerceptronIterationEventBuffer;
|
||||
use App\Services\ISynapticWeightsProvider;
|
||||
use App\Services\PerceptronIterationEventBuffer;
|
||||
|
||||
@@ -18,36 +19,36 @@ class GradientDescentPerceptronTraining extends NetworkTraining
|
||||
public function __construct(
|
||||
DataSetReader $datasetReader,
|
||||
protected float $learningRate,
|
||||
int $maxIterations,
|
||||
int $maxEpochs,
|
||||
protected ISynapticWeightsProvider $synapticWeightsProvider,
|
||||
PerceptronIterationEventBuffer $iterationEventBuffer,
|
||||
IPerceptronIterationEventBuffer $iterationEventBuffer,
|
||||
string $sessionId,
|
||||
string $trainingId,
|
||||
private float $minError,
|
||||
) {
|
||||
parent::__construct($datasetReader, $maxIterations, $iterationEventBuffer, $sessionId, $trainingId);
|
||||
parent::__construct($datasetReader, $maxEpochs, $iterationEventBuffer, $sessionId, $trainingId);
|
||||
$this->perceptron = new GradientDescentPerceptron($synapticWeightsProvider->generate($datasetReader->getInputSize()));
|
||||
}
|
||||
|
||||
public function start(): void
|
||||
{
|
||||
$this->iteration = 0;
|
||||
$this->epoch = 0;
|
||||
do {
|
||||
$this->epochError = 0;
|
||||
$iterationErrorPerWeight = [];
|
||||
$this->iteration++;
|
||||
$epochCorrectorPerWeight = [];
|
||||
$this->epoch++;
|
||||
|
||||
while ($nextRow = $this->datasetReader->getRandomLine()) {
|
||||
$inputs = array_slice($nextRow, 0, -1);
|
||||
$correctOutput = (float) end($nextRow);
|
||||
|
||||
$iterationError = $this->iterationFunction($inputs, $correctOutput);
|
||||
$this->epochError += (1 / 2) * (abs($iterationError) ** 2); // TDDO REMOVEME abs()
|
||||
$this->epochError += ($iterationError ** 2) / 2;
|
||||
|
||||
// Store the iteration error for each weight
|
||||
$inputs_with_bias = array_merge([1], $inputs); // Add bias input
|
||||
foreach ($inputs_with_bias as $index => $input) {
|
||||
$iterationErrorPerWeight[$index][] = $iterationError * $input;
|
||||
$epochCorrectorPerWeight[$index][] = $iterationError * $input;
|
||||
}
|
||||
|
||||
// Broadcast the training iteration event
|
||||
@@ -57,14 +58,14 @@ class GradientDescentPerceptronTraining extends NetworkTraining
|
||||
// Synaptic weights correction after each epoch
|
||||
$synaptic_weights = $this->perceptron->getSynapticWeights();
|
||||
$new_weights = array_map(
|
||||
fn($weight, $weightIndex) => $weight + $this->learningRate * array_sum($iterationErrorPerWeight[$weightIndex]),
|
||||
fn($weight, $weightIndex) => $weight + $this->learningRate * array_sum($epochCorrectorPerWeight[$weightIndex]),
|
||||
$synaptic_weights,
|
||||
array_keys($synaptic_weights)
|
||||
);
|
||||
$this->perceptron->setSynapticWeights($new_weights);
|
||||
|
||||
$this->datasetReader->reset(); // Reset the dataset for the next iteration
|
||||
} while ($this->iteration < $this->maxIterations && !$this->stopCondition());
|
||||
} while ($this->epoch < $this->maxEpochs && !$this->stopCondition());
|
||||
|
||||
$this->iterationEventBuffer->flush(); // Ensure all iterations are sent to the frontend
|
||||
|
||||
|
||||
@@ -4,11 +4,11 @@ namespace App\Models;
|
||||
|
||||
use App\Events\PerceptronTrainingEnded;
|
||||
use App\Services\DataSetReader;
|
||||
use App\Services\PerceptronIterationEventBuffer;
|
||||
use App\Services\IPerceptronIterationEventBuffer;
|
||||
|
||||
abstract class NetworkTraining
|
||||
{
|
||||
protected int $iteration = 0;
|
||||
protected int $epoch = 0;
|
||||
|
||||
/**
|
||||
* @abstract
|
||||
@@ -18,8 +18,8 @@ abstract class NetworkTraining
|
||||
|
||||
public function __construct(
|
||||
protected DataSetReader $datasetReader,
|
||||
protected int $maxIterations,
|
||||
protected PerceptronIterationEventBuffer $iterationEventBuffer,
|
||||
protected int $maxEpochs,
|
||||
protected IPerceptronIterationEventBuffer $iterationEventBuffer,
|
||||
protected string $sessionId,
|
||||
protected string $trainingId,
|
||||
) {
|
||||
@@ -29,8 +29,8 @@ abstract class NetworkTraining
|
||||
abstract protected function stopCondition(): bool;
|
||||
|
||||
protected function checkPassedMaxIterations(?float $finalError) {
|
||||
if ($this->iteration >= $this->maxIterations) {
|
||||
$message = 'Le nombre maximal d\'itérations a été atteint';
|
||||
if ($this->epoch >= $this->maxEpochs) {
|
||||
$message = 'Le nombre maximal d\'epoch a été atteint';
|
||||
if ($finalError) {
|
||||
$message .= " avec une erreur finale de $finalError";
|
||||
}
|
||||
@@ -40,6 +40,6 @@ abstract class NetworkTraining
|
||||
}
|
||||
|
||||
protected function addIterationToBuffer(float $error, array $synapticWeights) {
|
||||
$this->iterationEventBuffer->addIteration($this->iteration, $this->datasetReader->getLastReadLineIndex(), $error, $synapticWeights);
|
||||
$this->iterationEventBuffer->addIteration($this->epoch, $this->datasetReader->getLastReadLineIndex(), $error, $synapticWeights);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,8 +4,8 @@ namespace App\Models;
|
||||
|
||||
use App\Events\PerceptronTrainingEnded;
|
||||
use App\Services\DataSetReader;
|
||||
use App\Services\IPerceptronIterationEventBuffer;
|
||||
use App\Services\ISynapticWeightsProvider;
|
||||
use App\Services\PerceptronIterationEventBuffer;
|
||||
|
||||
class SimpleBinaryPerceptronTraining extends NetworkTraining
|
||||
{
|
||||
@@ -19,25 +19,25 @@ class SimpleBinaryPerceptronTraining extends NetworkTraining
|
||||
public function __construct(
|
||||
DataSetReader $datasetReader,
|
||||
protected float $learningRate,
|
||||
int $maxIterations,
|
||||
int $maxEpochs,
|
||||
protected ISynapticWeightsProvider $synapticWeightsProvider,
|
||||
PerceptronIterationEventBuffer $iterationEventBuffer,
|
||||
IPerceptronIterationEventBuffer $iterationEventBuffer,
|
||||
string $sessionId,
|
||||
string $trainingId,
|
||||
) {
|
||||
parent::__construct($datasetReader, $maxIterations, $iterationEventBuffer, $sessionId, $trainingId);
|
||||
parent::__construct($datasetReader, $maxEpochs, $iterationEventBuffer, $sessionId, $trainingId);
|
||||
$this->perceptron = new SimpleBinaryPerceptron($synapticWeightsProvider->generate($datasetReader->getInputSize()));
|
||||
}
|
||||
|
||||
public function start(): void
|
||||
{
|
||||
$this->iteration = 0;
|
||||
$this->epoch = 0;
|
||||
$error = 0;
|
||||
do {
|
||||
$this->iterationErrorCounter = 0;
|
||||
$this->iteration++;
|
||||
$this->epoch++;
|
||||
|
||||
while ($nextRow = $this->datasetReader->getRandomLine()) {
|
||||
while ($nextRow = $this->datasetReader->getNextLine()) {
|
||||
$inputs = array_slice($nextRow, 0, -1);
|
||||
$correctOutput = (float) end($nextRow);
|
||||
$correctOutput = $correctOutput > 0 ? 1 : 0; // Modify labels for non binary datasets
|
||||
@@ -48,7 +48,7 @@ class SimpleBinaryPerceptronTraining extends NetworkTraining
|
||||
$this->addIterationToBuffer($error, [[$this->perceptron->getSynapticWeights()]]);
|
||||
}
|
||||
$this->datasetReader->reset(); // Reset the dataset for the next iteration
|
||||
} while ($this->iteration < $this->maxIterations && !$this->stopCondition());
|
||||
} while ($this->epoch < $this->maxEpochs && !$this->stopCondition());
|
||||
|
||||
$this->iterationEventBuffer->flush(); // Ensure all iterations are sent to the frontend
|
||||
|
||||
|
||||
@@ -51,6 +51,16 @@ class DataSetReader {
|
||||
return $randomLine;
|
||||
}
|
||||
|
||||
public function getNextLine(): array | null {
|
||||
if (!isset($this->currentLines[0])) {
|
||||
return null; // No more lines to read
|
||||
}
|
||||
|
||||
$this->lastReadLineIndex = array_search($this->currentLines[0], $this->lines, true);
|
||||
|
||||
return array_shift($this->currentLines);
|
||||
}
|
||||
|
||||
public function getInputSize(): int
|
||||
{
|
||||
return count($this->lines[0]) - 1; // Don't count the label
|
||||
|
||||
10
app/Services/IPerceptronIterationEventBuffer.php
Normal file
10
app/Services/IPerceptronIterationEventBuffer.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
interface IPerceptronIterationEventBuffer {
|
||||
|
||||
public function flush(): void ;
|
||||
|
||||
public function addIteration(int $iteration, int $exampleIndex, float $error, array $synaptic_weights): void ;
|
||||
}
|
||||
@@ -2,15 +2,11 @@
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class PerceptronIterationEventBuffer {
|
||||
class PerceptronIterationEventBuffer implements IPerceptronIterationEventBuffer {
|
||||
private $data;
|
||||
private int $nextSizeIncreaseThreshold;
|
||||
private int $underSizeIncreaseCount = 0;
|
||||
|
||||
private int $MAX_SIZE = 50;
|
||||
|
||||
public function __construct(
|
||||
private string $sessionId,
|
||||
private string $trainingId,
|
||||
@@ -26,9 +22,9 @@ class PerceptronIterationEventBuffer {
|
||||
$this->data = [];
|
||||
}
|
||||
|
||||
public function addIteration(int $iteration, int $exampleIndex, float $error, array $synaptic_weights): void {
|
||||
public function addIteration(int $epoch, int $exampleIndex, float $error, array $synaptic_weights): void {
|
||||
$this->data[] = [
|
||||
"iteration" => $iteration,
|
||||
"epoch" => $epoch,
|
||||
"exampleIndex" => $exampleIndex,
|
||||
"error" => $error,
|
||||
"weights" => $synaptic_weights,
|
||||
@@ -42,8 +38,8 @@ class PerceptronIterationEventBuffer {
|
||||
$this->flush();
|
||||
$this->nextSizeIncreaseThreshold *= $this->sizeIncreaseFactor;
|
||||
|
||||
if ($this->nextSizeIncreaseThreshold > $this->MAX_SIZE) {
|
||||
$this->nextSizeIncreaseThreshold = $this->MAX_SIZE; // Cap the threshold to the maximum size
|
||||
if ($this->nextSizeIncreaseThreshold > config('perceptron.broadcast_iteration_size')) {
|
||||
$this->nextSizeIncreaseThreshold = config('perceptron.broadcast_iteration_size'); // Cap the threshold to the maximum size
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
51
app/Services/PerceptronLimitedEpochEventBuffer.php
Normal file
51
app/Services/PerceptronLimitedEpochEventBuffer.php
Normal file
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
class PerceptronLimitedEpochEventBuffer implements IPerceptronIterationEventBuffer {
|
||||
private array $data;
|
||||
private int $underSizeIncreaseCount = 0;
|
||||
|
||||
public function __construct(
|
||||
private string $sessionId,
|
||||
private string $trainingId,
|
||||
private int $epochInterval,
|
||||
private int $sizeIncreaseStart = 10,
|
||||
) {
|
||||
$this->data = [];
|
||||
}
|
||||
|
||||
public function flush(): void {
|
||||
event(new \App\Events\PerceptronTrainingIteration($this->data, $this->sessionId, $this->trainingId));
|
||||
$this->data = [];
|
||||
}
|
||||
|
||||
public function addIteration(int $epoch, int $exampleIndex, float $error, array $synaptic_weights): void {
|
||||
$newData = [
|
||||
"epoch" => $epoch,
|
||||
"exampleIndex" => $exampleIndex,
|
||||
"error" => $error,
|
||||
"weights" => $synaptic_weights,
|
||||
];
|
||||
|
||||
if ($this->underSizeIncreaseCount <= $this->sizeIncreaseStart) { // Special case where we need to send each iteration separately
|
||||
$this->underSizeIncreaseCount++;
|
||||
$this->data[] = $newData;
|
||||
$this->flush();
|
||||
return;
|
||||
}
|
||||
|
||||
$lastEpoch = $this->data[0]['epoch'] ?? null;
|
||||
if ($this->data && $lastEpoch !== $epoch) { // Current Epoch has changed from the last one
|
||||
if ($lastEpoch % $this->epochInterval === 0) { // The last epoch need to be sent
|
||||
$this->flush(); // Flush all data from the previous epoch
|
||||
}
|
||||
else {
|
||||
$this->data = [];
|
||||
}
|
||||
|
||||
$lastEpoch = $epoch;
|
||||
}
|
||||
$this->data[] = $newData;
|
||||
}
|
||||
}
|
||||
19
config/perceptron.php
Normal file
19
config/perceptron.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
return [
|
||||
|
||||
/**
|
||||
* Minimum number of iterations for which the broadcast of the training progress is allowed in full.
|
||||
* Beyond this number of iterations, the broadcast will be splitted every x iterations,
|
||||
* x is limited_broadcast_number
|
||||
*/
|
||||
'limited_broadcast_iterations' => 200,
|
||||
|
||||
/**
|
||||
* How much broadcasts is sent when in limmited broadcast mode
|
||||
*/
|
||||
'limited_broadcast_number' => 200,
|
||||
|
||||
'broadcast_iteration_size' => 75,
|
||||
|
||||
];
|
||||
4
public/data_sets/logic_or.csv
Normal file
4
public/data_sets/logic_or.csv
Normal file
@@ -0,0 +1,4 @@
|
||||
0, 0, -1
|
||||
0, 1, 1
|
||||
1, 0, 1
|
||||
1, 1, 1
|
||||
|
@@ -15,14 +15,29 @@ const allWeightPerIteration: ComputedRef<number[][]> = computed(() => {
|
||||
return iteration.weights.flat(2);
|
||||
});
|
||||
});
|
||||
|
||||
const rowBgDark = computed(() => {
|
||||
let isEven = false;
|
||||
return props.iterations.map((iteration, index, arr) => {
|
||||
if (index > 0 && arr[index - 1].epoch !== iteration.epoch) {
|
||||
isEven = !isEven;
|
||||
}
|
||||
return isEven;
|
||||
});
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<table class="table w-full border-collapse border border-gray-300">
|
||||
<tr class="text-left" v-if="props.iterations.length > 0">
|
||||
<th>Itération</th>
|
||||
<th>Époch</th>
|
||||
<th>Exemple</th>
|
||||
<th v-for="(weight, index) in allWeightPerIteration[allWeightPerIteration.length - 1]" v-bind:key="index">
|
||||
<th
|
||||
v-for="(weight, index) in allWeightPerIteration[
|
||||
allWeightPerIteration.length - 1
|
||||
]"
|
||||
v-bind:key="index"
|
||||
>
|
||||
X<sub>{{ index }}</sub>
|
||||
</th>
|
||||
<th>Erreur</th>
|
||||
@@ -31,12 +46,15 @@ const allWeightPerIteration: ComputedRef<number[][]> = computed(() => {
|
||||
v-for="(iteration, index) in props.iterations"
|
||||
v-bind:key="index"
|
||||
:class="{
|
||||
'bg-gray-900': iteration.iteration % 2 === 0,
|
||||
'bg-gray-900': rowBgDark[index],
|
||||
}"
|
||||
>
|
||||
<td>{{ iteration.iteration }}</td>
|
||||
<td>{{ iteration.epoch }}</td>
|
||||
<td>{{ iteration.exampleIndex }}</td>
|
||||
<td v-for="(weight, index) in allWeightPerIteration[index]" v-bind:key="index">
|
||||
<td
|
||||
v-for="(weight, index) in allWeightPerIteration[index]"
|
||||
v-bind:key="index"
|
||||
>
|
||||
{{ weight.toFixed(2) }}
|
||||
</td>
|
||||
<td>{{ iteration.error.toFixed(2) }}</td>
|
||||
|
||||
@@ -46,7 +46,7 @@ function getPerceptronDecisionBoundaryDataset(
|
||||
networkWeights[0].length == 1 &&
|
||||
networkWeights[0][0].length == 3
|
||||
) { // Unique, 3 weights perceptron
|
||||
const perceptronWeights = networkWeights[0][0]; // We take the unique
|
||||
const perceptronWeights = networkWeights[0][0]; // We take the unique perceptron
|
||||
|
||||
function perceptronLine(x: number): number {
|
||||
// w0 + w1*x + w2*y = 0 => y = -(w1/w2)*x - w0/w2
|
||||
@@ -161,7 +161,7 @@ function getPerceptronDecisionBoundaryDataset(
|
||||
color: function (context) {
|
||||
if (context.tick.value == 0) {
|
||||
return gridColorBold;
|
||||
}
|
||||
}
|
||||
|
||||
return gridColor;
|
||||
},
|
||||
@@ -174,7 +174,7 @@ function getPerceptronDecisionBoundaryDataset(
|
||||
color: function (context) {
|
||||
if (context.tick.value == 0) {
|
||||
return gridColorBold;
|
||||
}
|
||||
}
|
||||
|
||||
return gridColor;
|
||||
},
|
||||
|
||||
@@ -39,8 +39,8 @@ function getPerceptronErrorsPerIteration(): ChartData<
|
||||
dataset.data.push(iteration.error);
|
||||
|
||||
// Epoch error
|
||||
epochAverageError[iteration.iteration - 1] =
|
||||
(epochAverageError[iteration.iteration - 1] || 0) +
|
||||
epochAverageError[iteration.epoch - 1] =
|
||||
(epochAverageError[iteration.epoch - 1] || 0) +
|
||||
iteration.error ** 2 / 2;
|
||||
});
|
||||
|
||||
@@ -81,7 +81,7 @@ function getPerceptronErrorsPerIteration(): ChartData<
|
||||
plugins: {
|
||||
title: {
|
||||
display: true,
|
||||
text: 'Nombre d\'erreurs par itération',
|
||||
text: 'Nombre d\'erreurs par epoch',
|
||||
},
|
||||
},
|
||||
scales: {
|
||||
@@ -96,7 +96,7 @@ function getPerceptronErrorsPerIteration(): ChartData<
|
||||
color: function (context) {
|
||||
if (context.tick.value == 0) {
|
||||
return gridColorBold;
|
||||
}
|
||||
}
|
||||
|
||||
return gridColor;
|
||||
},
|
||||
@@ -106,8 +106,8 @@ function getPerceptronErrorsPerIteration(): ChartData<
|
||||
}"
|
||||
:data="{
|
||||
labels: props.iterations.reduce((labels, iteration) => {
|
||||
if (!labels.includes(`Itération ${iteration.iteration}`)) {
|
||||
labels.push(`Itération ${iteration.iteration}`);
|
||||
if (!labels.includes(`Époch ${iteration.epoch}`)) {
|
||||
labels.push(`Époch ${iteration.epoch}`);
|
||||
}
|
||||
return labels;
|
||||
}, [] as string[]),
|
||||
|
||||
@@ -47,11 +47,17 @@ watch(selectedDatasetCopy, (newvalue) => {
|
||||
(dataset) => dataset.label === newvalue
|
||||
) || null;
|
||||
|
||||
let defaultLearningRate = props.defaultLearningRate;
|
||||
// LearningRate
|
||||
learningRate.value = props.defaultLearningRate;
|
||||
if (selectedDatasetCopy && selectedDatasetCopy.defaultLearningRate !== undefined) {
|
||||
defaultLearningRate = selectedDatasetCopy.defaultLearningRate;
|
||||
learningRate.value = selectedDatasetCopy.defaultLearningRate;
|
||||
}
|
||||
learningRate.value = defaultLearningRate;
|
||||
// MinError
|
||||
minError.value = props.minError;
|
||||
if (selectedDatasetCopy && selectedDatasetCopy.defaultMinError !== undefined) {
|
||||
minError.value = selectedDatasetCopy.defaultMinError;
|
||||
}
|
||||
// MaxIterations
|
||||
maxIterations.value = props.defaultMaxIterations;
|
||||
})
|
||||
|
||||
|
||||
@@ -1,44 +0,0 @@
|
||||
<script setup lang="ts">
|
||||
import { Head, Link } from '@inertiajs/vue3';
|
||||
|
||||
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<Head title="Welcome">
|
||||
</Head>
|
||||
<div
|
||||
class="flex min-h-screen flex-col items-center bg-[#FDFDFC] p-6 text-[#1b1b18] lg:justify-center lg:p-8 dark:bg-[#0a0a0a]"
|
||||
>
|
||||
<header
|
||||
class="mb-6 w-full max-w-[335px] text-sm not-has-[nav]:hidden lg:max-w-4xl"
|
||||
>
|
||||
<nav class="flex items-center justify-end gap-4">
|
||||
<Link
|
||||
href="/test"
|
||||
class="inline-block rounded-sm border border-[#19140035] px-5 py-1.5 text-sm leading-normal text-[#1b1b18] hover:border-[#1915014a] dark:border-[#3E3E3A] dark:text-[#EDEDEC] dark:hover:border-[#62605b]"
|
||||
view-transition
|
||||
>
|
||||
Test
|
||||
</Link>
|
||||
<Link
|
||||
href="/perceptron"
|
||||
class="inline-block rounded-sm border border-[#19140035] px-5 py-1.5 text-sm leading-normal text-[#1b1b18] hover:border-[#1915014a] dark:border-[#3E3E3A] dark:text-[#EDEDEC] dark:hover:border-[#62605b]"
|
||||
:data="{type: 'simple'}"
|
||||
view-transition
|
||||
>
|
||||
Perceptron Simple
|
||||
</Link>
|
||||
</nav>
|
||||
</header>
|
||||
<div
|
||||
class="flex w-full items-center justify-center opacity-100 transition-opacity duration-750 lg:grow starting:opacity-0"
|
||||
>
|
||||
<main
|
||||
class="flex w-full max-w-[335px] flex-col-reverse overflow-hidden rounded-lg lg:max-w-4xl lg:flex-row"
|
||||
>
|
||||
|
||||
</main>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
@@ -1,11 +0,0 @@
|
||||
<script setup lang="ts">
|
||||
import { Head, Link } from '@inertiajs/vue3';
|
||||
|
||||
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<p>
|
||||
I'm you father
|
||||
</p>
|
||||
</template>
|
||||
@@ -34,4 +34,4 @@ export const colors = [
|
||||
] as const;
|
||||
|
||||
export const gridColor = '#444';
|
||||
export const gridColorBold = '#999';
|
||||
export const gridColorBold = '#999';
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
import type { Point } from "chart.js";
|
||||
|
||||
export type Iteration = {
|
||||
iteration: number;
|
||||
epoch: number;
|
||||
exampleIndex: number;
|
||||
weights: number[][][];
|
||||
error: number;
|
||||
@@ -10,7 +8,8 @@ export type Iteration = {
|
||||
export type Dataset = {
|
||||
label: string;
|
||||
data: DatasetPoint[];
|
||||
defaultLearningRate: number | undefined;
|
||||
defaultLearningRate?: number;
|
||||
defaultMinError?: number;
|
||||
};
|
||||
|
||||
export type DatasetPoint = {
|
||||
|
||||
@@ -4,9 +4,7 @@ use App\Http\Controllers\PerceptronController;
|
||||
use Illuminate\Support\Facades\Broadcast;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
|
||||
Route::inertia('/', 'Home')->name('home');
|
||||
|
||||
Route::inertia('/test', 'Test')->name('test');
|
||||
Route::redirect('/', '/perceptron?type=simple')->name('home');
|
||||
|
||||
Route::resource('perceptron', PerceptronController::class)->only(['index']);
|
||||
|
||||
|
||||
@@ -1,99 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Auth;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\RateLimiter;
|
||||
use Laravel\Fortify\Features;
|
||||
use Tests\TestCase;
|
||||
|
||||
class AuthenticationTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_login_screen_can_be_rendered()
|
||||
{
|
||||
$response = $this->get(route('login'));
|
||||
|
||||
$response->assertOk();
|
||||
}
|
||||
|
||||
public function test_users_can_authenticate_using_the_login_screen()
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
$response = $this->post(route('login.store'), [
|
||||
'email' => $user->email,
|
||||
'password' => 'password',
|
||||
]);
|
||||
|
||||
$this->assertAuthenticated();
|
||||
$response->assertRedirect(route('dashboard', absolute: false));
|
||||
}
|
||||
|
||||
public function test_users_with_two_factor_enabled_are_redirected_to_two_factor_challenge()
|
||||
{
|
||||
if (! Features::canManageTwoFactorAuthentication()) {
|
||||
$this->markTestSkipped('Two-factor authentication is not enabled.');
|
||||
}
|
||||
|
||||
Features::twoFactorAuthentication([
|
||||
'confirm' => true,
|
||||
'confirmPassword' => true,
|
||||
]);
|
||||
|
||||
$user = User::factory()->create();
|
||||
|
||||
$user->forceFill([
|
||||
'two_factor_secret' => encrypt('test-secret'),
|
||||
'two_factor_recovery_codes' => encrypt(json_encode(['code1', 'code2'])),
|
||||
'two_factor_confirmed_at' => now(),
|
||||
])->save();
|
||||
|
||||
$response = $this->post(route('login'), [
|
||||
'email' => $user->email,
|
||||
'password' => 'password',
|
||||
]);
|
||||
|
||||
$response->assertRedirect(route('two-factor.login'));
|
||||
$response->assertSessionHas('login.id', $user->id);
|
||||
$this->assertGuest();
|
||||
}
|
||||
|
||||
public function test_users_can_not_authenticate_with_invalid_password()
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
$this->post(route('login.store'), [
|
||||
'email' => $user->email,
|
||||
'password' => 'wrong-password',
|
||||
]);
|
||||
|
||||
$this->assertGuest();
|
||||
}
|
||||
|
||||
public function test_users_can_logout()
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
$response = $this->actingAs($user)->post(route('logout'));
|
||||
|
||||
$this->assertGuest();
|
||||
$response->assertRedirect(route('home'));
|
||||
}
|
||||
|
||||
public function test_users_are_rate_limited()
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
RateLimiter::increment(md5('login'.implode('|', [$user->email, '127.0.0.1'])), amount: 5);
|
||||
|
||||
$response = $this->post(route('login.store'), [
|
||||
'email' => $user->email,
|
||||
'password' => 'wrong-password',
|
||||
]);
|
||||
|
||||
$response->assertTooManyRequests();
|
||||
}
|
||||
}
|
||||
@@ -1,110 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Auth;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Auth\Events\Verified;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
use Illuminate\Support\Facades\URL;
|
||||
use Tests\TestCase;
|
||||
|
||||
class EmailVerificationTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_email_verification_screen_can_be_rendered()
|
||||
{
|
||||
$user = User::factory()->unverified()->create();
|
||||
|
||||
$response = $this->actingAs($user)->get(route('verification.notice'));
|
||||
|
||||
$response->assertOk();
|
||||
}
|
||||
|
||||
public function test_email_can_be_verified()
|
||||
{
|
||||
$user = User::factory()->unverified()->create();
|
||||
|
||||
Event::fake();
|
||||
|
||||
$verificationUrl = URL::temporarySignedRoute(
|
||||
'verification.verify',
|
||||
now()->addMinutes(60),
|
||||
['id' => $user->id, 'hash' => sha1($user->email)],
|
||||
);
|
||||
|
||||
$response = $this->actingAs($user)->get($verificationUrl);
|
||||
|
||||
Event::assertDispatched(Verified::class);
|
||||
$this->assertTrue($user->fresh()->hasVerifiedEmail());
|
||||
$response->assertRedirect(route('dashboard', absolute: false).'?verified=1');
|
||||
}
|
||||
|
||||
public function test_email_is_not_verified_with_invalid_hash()
|
||||
{
|
||||
$user = User::factory()->unverified()->create();
|
||||
|
||||
Event::fake();
|
||||
|
||||
$verificationUrl = URL::temporarySignedRoute(
|
||||
'verification.verify',
|
||||
now()->addMinutes(60),
|
||||
['id' => $user->id, 'hash' => sha1('wrong-email')],
|
||||
);
|
||||
|
||||
$this->actingAs($user)->get($verificationUrl);
|
||||
|
||||
Event::assertNotDispatched(Verified::class);
|
||||
$this->assertFalse($user->fresh()->hasVerifiedEmail());
|
||||
}
|
||||
|
||||
public function test_email_is_not_verified_with_invalid_user_id(): void
|
||||
{
|
||||
$user = User::factory()->unverified()->create();
|
||||
|
||||
Event::fake();
|
||||
|
||||
$verificationUrl = URL::temporarySignedRoute(
|
||||
'verification.verify',
|
||||
now()->addMinutes(60),
|
||||
['id' => 123, 'hash' => sha1($user->email)],
|
||||
);
|
||||
|
||||
$this->actingAs($user)->get($verificationUrl);
|
||||
|
||||
Event::assertNotDispatched(Verified::class);
|
||||
$this->assertFalse($user->fresh()->hasVerifiedEmail());
|
||||
}
|
||||
|
||||
public function test_verified_user_is_redirected_to_dashboard_from_verification_prompt(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
Event::fake();
|
||||
|
||||
$response = $this->actingAs($user)->get(route('verification.notice'));
|
||||
|
||||
Event::assertNotDispatched(Verified::class);
|
||||
$response->assertRedirect(route('dashboard', absolute: false));
|
||||
}
|
||||
|
||||
public function test_already_verified_user_visiting_verification_link_is_redirected_without_firing_event_again(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
Event::fake();
|
||||
|
||||
$verificationUrl = URL::temporarySignedRoute(
|
||||
'verification.verify',
|
||||
now()->addMinutes(60),
|
||||
['id' => $user->id, 'hash' => sha1($user->email)],
|
||||
);
|
||||
|
||||
$this->actingAs($user)->get($verificationUrl)
|
||||
->assertRedirect(route('dashboard', absolute: false).'?verified=1');
|
||||
|
||||
Event::assertNotDispatched(Verified::class);
|
||||
$this->assertTrue($user->fresh()->hasVerifiedEmail());
|
||||
}
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Auth;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Inertia\Testing\AssertableInertia as Assert;
|
||||
use Tests\TestCase;
|
||||
|
||||
class PasswordConfirmationTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_confirm_password_screen_can_be_rendered()
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
$response = $this->actingAs($user)->get(route('password.confirm'));
|
||||
|
||||
$response->assertOk();
|
||||
|
||||
$response->assertInertia(fn (Assert $page) => $page
|
||||
->component('auth/ConfirmPassword'),
|
||||
);
|
||||
}
|
||||
|
||||
public function test_password_confirmation_requires_authentication()
|
||||
{
|
||||
$response = $this->get(route('password.confirm'));
|
||||
|
||||
$response->assertRedirect(route('login'));
|
||||
}
|
||||
}
|
||||
@@ -1,87 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Auth;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Auth\Notifications\ResetPassword;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Notification;
|
||||
use Tests\TestCase;
|
||||
|
||||
class PasswordResetTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_reset_password_link_screen_can_be_rendered()
|
||||
{
|
||||
$response = $this->get(route('password.request'));
|
||||
|
||||
$response->assertOk();
|
||||
}
|
||||
|
||||
public function test_reset_password_link_can_be_requested()
|
||||
{
|
||||
Notification::fake();
|
||||
|
||||
$user = User::factory()->create();
|
||||
|
||||
$this->post(route('password.email'), ['email' => $user->email]);
|
||||
|
||||
Notification::assertSentTo($user, ResetPassword::class);
|
||||
}
|
||||
|
||||
public function test_reset_password_screen_can_be_rendered()
|
||||
{
|
||||
Notification::fake();
|
||||
|
||||
$user = User::factory()->create();
|
||||
|
||||
$this->post(route('password.email'), ['email' => $user->email]);
|
||||
|
||||
Notification::assertSentTo($user, ResetPassword::class, function ($notification) {
|
||||
$response = $this->get(route('password.reset', $notification->token));
|
||||
|
||||
$response->assertOk();
|
||||
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
public function test_password_can_be_reset_with_valid_token()
|
||||
{
|
||||
Notification::fake();
|
||||
|
||||
$user = User::factory()->create();
|
||||
|
||||
$this->post(route('password.email'), ['email' => $user->email]);
|
||||
|
||||
Notification::assertSentTo($user, ResetPassword::class, function ($notification) use ($user) {
|
||||
$response = $this->post(route('password.update'), [
|
||||
'token' => $notification->token,
|
||||
'email' => $user->email,
|
||||
'password' => 'password',
|
||||
'password_confirmation' => 'password',
|
||||
]);
|
||||
|
||||
$response
|
||||
->assertSessionHasNoErrors()
|
||||
->assertRedirect(route('login'));
|
||||
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
public function test_password_cannot_be_reset_with_invalid_token(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
$response = $this->post(route('password.update'), [
|
||||
'token' => 'invalid-token',
|
||||
'email' => $user->email,
|
||||
'password' => 'newpassword123',
|
||||
'password_confirmation' => 'newpassword123',
|
||||
]);
|
||||
|
||||
$response->assertSessionHasErrors('email');
|
||||
}
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Auth;
|
||||
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
class RegistrationTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_registration_screen_can_be_rendered()
|
||||
{
|
||||
$response = $this->get(route('register'));
|
||||
|
||||
$response->assertOk();
|
||||
}
|
||||
|
||||
public function test_new_users_can_register()
|
||||
{
|
||||
$response = $this->post(route('register.store'), [
|
||||
'name' => 'Test User',
|
||||
'email' => 'test@example.com',
|
||||
'password' => 'password',
|
||||
'password_confirmation' => 'password',
|
||||
]);
|
||||
|
||||
$this->assertAuthenticated();
|
||||
$response->assertRedirect(route('dashboard', absolute: false));
|
||||
}
|
||||
}
|
||||
@@ -1,56 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Auth;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Inertia\Testing\AssertableInertia as Assert;
|
||||
use Laravel\Fortify\Features;
|
||||
use Tests\TestCase;
|
||||
|
||||
class TwoFactorChallengeTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_two_factor_challenge_redirects_to_login_when_not_authenticated(): void
|
||||
{
|
||||
if (! Features::canManageTwoFactorAuthentication()) {
|
||||
$this->markTestSkipped('Two-factor authentication is not enabled.');
|
||||
}
|
||||
|
||||
$response = $this->get(route('two-factor.login'));
|
||||
|
||||
$response->assertRedirect(route('login'));
|
||||
}
|
||||
|
||||
public function test_two_factor_challenge_can_be_rendered(): void
|
||||
{
|
||||
if (! Features::canManageTwoFactorAuthentication()) {
|
||||
$this->markTestSkipped('Two-factor authentication is not enabled.');
|
||||
}
|
||||
|
||||
Features::twoFactorAuthentication([
|
||||
'confirm' => true,
|
||||
'confirmPassword' => true,
|
||||
]);
|
||||
|
||||
$user = User::factory()->create();
|
||||
|
||||
$user->forceFill([
|
||||
'two_factor_secret' => encrypt('test-secret'),
|
||||
'two_factor_recovery_codes' => encrypt(json_encode(['code1', 'code2'])),
|
||||
'two_factor_confirmed_at' => now(),
|
||||
])->save();
|
||||
|
||||
$this->post(route('login'), [
|
||||
'email' => $user->email,
|
||||
'password' => 'password',
|
||||
]);
|
||||
|
||||
$this->get(route('two-factor.login'))
|
||||
->assertOk()
|
||||
->assertInertia(fn (Assert $page) => $page
|
||||
->component('auth/TwoFactorChallenge'),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Auth;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Auth\Notifications\VerifyEmail;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Notification;
|
||||
use Tests\TestCase;
|
||||
|
||||
class VerificationNotificationTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_sends_verification_notification(): void
|
||||
{
|
||||
Notification::fake();
|
||||
|
||||
$user = User::factory()->unverified()->create();
|
||||
|
||||
$this->actingAs($user)
|
||||
->post(route('verification.send'))
|
||||
->assertRedirect(route('home'));
|
||||
|
||||
Notification::assertSentTo($user, VerifyEmail::class);
|
||||
}
|
||||
|
||||
public function test_does_not_send_verification_notification_if_email_is_verified(): void
|
||||
{
|
||||
Notification::fake();
|
||||
|
||||
$user = User::factory()->create();
|
||||
|
||||
$this->actingAs($user)
|
||||
->post(route('verification.send'))
|
||||
->assertRedirect(route('dashboard', absolute: false));
|
||||
|
||||
Notification::assertNothingSent();
|
||||
}
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
class DashboardTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_guests_are_redirected_to_the_login_page()
|
||||
{
|
||||
$response = $this->get(route('dashboard'));
|
||||
$response->assertRedirect(route('login'));
|
||||
}
|
||||
|
||||
public function test_authenticated_users_can_visit_the_dashboard()
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
$this->actingAs($user);
|
||||
|
||||
$response = $this->get(route('dashboard'));
|
||||
$response->assertOk();
|
||||
}
|
||||
}
|
||||
@@ -1,62 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Settings;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Tests\TestCase;
|
||||
|
||||
class PasswordUpdateTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_password_update_page_is_displayed()
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
$response = $this
|
||||
->actingAs($user)
|
||||
->get(route('user-password.edit'));
|
||||
|
||||
$response->assertOk();
|
||||
}
|
||||
|
||||
public function test_password_can_be_updated()
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
$response = $this
|
||||
->actingAs($user)
|
||||
->from(route('user-password.edit'))
|
||||
->put(route('user-password.update'), [
|
||||
'current_password' => 'password',
|
||||
'password' => 'new-password',
|
||||
'password_confirmation' => 'new-password',
|
||||
]);
|
||||
|
||||
$response
|
||||
->assertSessionHasNoErrors()
|
||||
->assertRedirect(route('user-password.edit'));
|
||||
|
||||
$this->assertTrue(Hash::check('new-password', $user->refresh()->password));
|
||||
}
|
||||
|
||||
public function test_correct_password_must_be_provided_to_update_password()
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
$response = $this
|
||||
->actingAs($user)
|
||||
->from(route('user-password.edit'))
|
||||
->put(route('user-password.update'), [
|
||||
'current_password' => 'wrong-password',
|
||||
'password' => 'new-password',
|
||||
'password_confirmation' => 'new-password',
|
||||
]);
|
||||
|
||||
$response
|
||||
->assertSessionHasErrors('current_password')
|
||||
->assertRedirect(route('user-password.edit'));
|
||||
}
|
||||
}
|
||||
@@ -1,99 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Settings;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
class ProfileUpdateTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_profile_page_is_displayed()
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
$response = $this
|
||||
->actingAs($user)
|
||||
->get(route('profile.edit'));
|
||||
|
||||
$response->assertOk();
|
||||
}
|
||||
|
||||
public function test_profile_information_can_be_updated()
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
$response = $this
|
||||
->actingAs($user)
|
||||
->patch(route('profile.update'), [
|
||||
'name' => 'Test User',
|
||||
'email' => 'test@example.com',
|
||||
]);
|
||||
|
||||
$response
|
||||
->assertSessionHasNoErrors()
|
||||
->assertRedirect(route('profile.edit'));
|
||||
|
||||
$user->refresh();
|
||||
|
||||
$this->assertSame('Test User', $user->name);
|
||||
$this->assertSame('test@example.com', $user->email);
|
||||
$this->assertNull($user->email_verified_at);
|
||||
}
|
||||
|
||||
public function test_email_verification_status_is_unchanged_when_the_email_address_is_unchanged()
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
$response = $this
|
||||
->actingAs($user)
|
||||
->patch(route('profile.update'), [
|
||||
'name' => 'Test User',
|
||||
'email' => $user->email,
|
||||
]);
|
||||
|
||||
$response
|
||||
->assertSessionHasNoErrors()
|
||||
->assertRedirect(route('profile.edit'));
|
||||
|
||||
$this->assertNotNull($user->refresh()->email_verified_at);
|
||||
}
|
||||
|
||||
public function test_user_can_delete_their_account()
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
$response = $this
|
||||
->actingAs($user)
|
||||
->delete(route('profile.destroy'), [
|
||||
'password' => 'password',
|
||||
]);
|
||||
|
||||
$response
|
||||
->assertSessionHasNoErrors()
|
||||
->assertRedirect(route('home'));
|
||||
|
||||
$this->assertGuest();
|
||||
$this->assertNull($user->fresh());
|
||||
}
|
||||
|
||||
public function test_correct_password_must_be_provided_to_delete_account()
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
$response = $this
|
||||
->actingAs($user)
|
||||
->from(route('profile.edit'))
|
||||
->delete(route('profile.destroy'), [
|
||||
'password' => 'wrong-password',
|
||||
]);
|
||||
|
||||
$response
|
||||
->assertSessionHasErrors('password')
|
||||
->assertRedirect(route('profile.edit'));
|
||||
|
||||
$this->assertNotNull($user->fresh());
|
||||
}
|
||||
}
|
||||
@@ -1,92 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Settings;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Inertia\Testing\AssertableInertia as Assert;
|
||||
use Laravel\Fortify\Features;
|
||||
use Tests\TestCase;
|
||||
|
||||
class TwoFactorAuthenticationTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_two_factor_settings_page_can_be_rendered()
|
||||
{
|
||||
if (! Features::canManageTwoFactorAuthentication()) {
|
||||
$this->markTestSkipped('Two-factor authentication is not enabled.');
|
||||
}
|
||||
|
||||
Features::twoFactorAuthentication([
|
||||
'confirm' => true,
|
||||
'confirmPassword' => true,
|
||||
]);
|
||||
|
||||
$user = User::factory()->create();
|
||||
|
||||
$this->actingAs($user)
|
||||
->withSession(['auth.password_confirmed_at' => time()])
|
||||
->get(route('two-factor.show'))
|
||||
->assertInertia(fn (Assert $page) => $page
|
||||
->component('settings/TwoFactor')
|
||||
->where('twoFactorEnabled', false),
|
||||
);
|
||||
}
|
||||
|
||||
public function test_two_factor_settings_page_requires_password_confirmation_when_enabled()
|
||||
{
|
||||
if (! Features::canManageTwoFactorAuthentication()) {
|
||||
$this->markTestSkipped('Two-factor authentication is not enabled.');
|
||||
}
|
||||
|
||||
$user = User::factory()->create();
|
||||
|
||||
Features::twoFactorAuthentication([
|
||||
'confirm' => true,
|
||||
'confirmPassword' => true,
|
||||
]);
|
||||
|
||||
$response = $this->actingAs($user)
|
||||
->get(route('two-factor.show'));
|
||||
|
||||
$response->assertRedirect(route('password.confirm'));
|
||||
}
|
||||
|
||||
public function test_two_factor_settings_page_does_not_requires_password_confirmation_when_disabled()
|
||||
{
|
||||
if (! Features::canManageTwoFactorAuthentication()) {
|
||||
$this->markTestSkipped('Two-factor authentication is not enabled.');
|
||||
}
|
||||
|
||||
$user = User::factory()->create();
|
||||
|
||||
Features::twoFactorAuthentication([
|
||||
'confirm' => true,
|
||||
'confirmPassword' => false,
|
||||
]);
|
||||
|
||||
$this->actingAs($user)
|
||||
->get(route('two-factor.show'))
|
||||
->assertOk()
|
||||
->assertInertia(fn (Assert $page) => $page
|
||||
->component('settings/TwoFactor'),
|
||||
);
|
||||
}
|
||||
|
||||
public function test_two_factor_settings_page_returns_forbidden_response_when_two_factor_is_disabled()
|
||||
{
|
||||
if (! Features::canManageTwoFactorAuthentication()) {
|
||||
$this->markTestSkipped('Two-factor authentication is not enabled.');
|
||||
}
|
||||
|
||||
config(['fortify.features' => []]);
|
||||
|
||||
$user = User::factory()->create();
|
||||
|
||||
$this->actingAs($user)
|
||||
->withSession(['auth.password_confirmed_at' => time()])
|
||||
->get(route('two-factor.show'))
|
||||
->assertForbidden();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user