<commit_before><?php

namespace CodeIgniter\Models;

use CodeIgniter\Database\Exceptions\DataException;
use CodeIgniter\Entity;
use CodeIgniter\I18n\Time;
use CodeIgniter\Model;
use stdClass;
use Tests\Support\Models\EntityModel;
use Tests\Support\Models\JobModel;
use Tests\Support\Models\SecondaryModel;
use Tests\Support\Models\UserModel;
use Tests\Support\Models\ValidModel;
use Tests\Support\Models\WithoutAutoIncrementModel;

final class SaveModelTest extends LiveModelTestCase
{
	public function testSaveNewRecordObject(): void
	{
		$this->createModel(JobModel::class);

		$data              = new stdClass();
		$data->name        = 'Magician';
		$data->description = 'Makes peoples things dissappear.';

		$this->model->protect(false)->save($data);
		$this->seeInDatabase('job', ['name' => 'Magician']);
	}

	public function testSaveNewRecordArray(): void
	{
		$this->createModel(JobModel::class);

		$data = [
			'name'        => 'Apprentice',
			'description' => 'That thing you do.',
		];

		$this->model->protect(false)->save($data);
		$this->seeInDatabase('job', ['name' => 'Apprentice']);
	}

	public function testSaveNewRecordArrayFail(): void
	{
		$this->setPrivateProperty($this->db, 'DBDebug', false);
		$this->createModel(JobModel::class);

		$data = [
			'name123'     => 'Apprentice',
			'description' => 'That thing you do.',
		];

		$result = $this->model->protect(false)->save($data);
		$this->assertFalse($result);
		$this->dontSeeInDatabase('job', ['name' => 'Apprentice']);
	}

	public function testSaveUpdateRecordArray(): void
	{
		$this->createModel(JobModel::class);

		$data = [
			'name'        => 'Apprentice',
			'description' => 'That thing you do.',
		];

		$result = $this->model->protect(false)->save($data);
		$this->seeInDatabase('job', ['name' => 'Apprentice']);
		$this->assertTrue($result);
	}

	public function testSaveUpdateRecordArrayFail(): void
	{
		$this->setPrivateProperty($this->db, 'DBDebug', false);
		$this->createModel(JobModel::class);

		$data = [
			'id'          => 1,
			'name123'     => 'Apprentice',
			'description' => 'That thing you do.',
		];

		$result = $this->model->protect(false)->save($data);
		$this->assertFalse($result);
		$this->dontSeeInDatabase('job', ['name' => 'Apprentice']);
	}

	public function testSaveUpdateRecordObject(): void
	{
		$this->createModel(JobModel::class);
		$data = new stdClass();

		// Sqlsrv does not allow forcing an ID into an autoincrement field.
		if ($this->db->DBDriver !== 'Sqlsrv')
		{
			$data->id = 1;
		}

		$data->name        = 'Engineer';
		$data->description = 'A fancier term for Developer.';

		$result = $this->model->protect(false)->save($data);
		$this->seeInDatabase('job', ['name' => 'Engineer']);
		$this->assertTrue($result);
	}

	public function testSaveProtected(): void
	{
		$this->createModel(JobModel::class);

		$data               = new stdClass();
		$data->id           = 1;
		$data->name         = 'Engineer';
		$data->description  = 'A fancier term for Developer.';
		$data->random_thing = 'Something wicked'; // If not protected, this would kill the script.

		$result = $this->model->protect(true)->save($data);
		$this->assertTrue($result);
	}

	public function testSelectAndEntitiesSaveOnlyChangedValues(): void
	{
		$this->hasInDatabase('job', [<commit_msg>Rename to SQLSRV<commit_after><?php

namespace CodeIgniter\Models;

use CodeIgniter\Database\Exceptions\DataException;
use CodeIgniter\Entity;
use CodeIgniter\I18n\Time;
use CodeIgniter\Model;
use stdClass;
use Tests\Support\Models\EntityModel;
use Tests\Support\Models\JobModel;
use Tests\Support\Models\SecondaryModel;
use Tests\Support\Models\UserModel;
use Tests\Support\Models\ValidModel;
use Tests\Support\Models\WithoutAutoIncrementModel;

final class SaveModelTest extends LiveModelTestCase
{
	public function testSaveNewRecordObject(): void
	{
		$this->createModel(JobModel::class);

		$data              = new stdClass();
		$data->name        = 'Magician';
		$data->description = 'Makes peoples things dissappear.';

		$this->model->protect(false)->save($data);
		$this->seeInDatabase('job', ['name' => 'Magician']);
	}

	public function testSaveNewRecordArray(): void
	{
		$this->createModel(JobModel::class);

		$data = [
			'name'        => 'Apprentice',
			'description' => 'That thing you do.',
		];

		$this->model->protect(false)->save($data);
		$this->seeInDatabase('job', ['name' => 'Apprentice']);
	}

	public function testSaveNewRecordArrayFail(): void
	{
		$this->setPrivateProperty($this->db, 'DBDebug', false);
		$this->createModel(JobModel::class);

		$data = [
			'name123'     => 'Apprentice',
			'description' => 'That thing you do.',
		];

		$result = $this->model->protect(false)->save($data);
		$this->assertFalse($result);
		$this->dontSeeInDatabase('job', ['name' => 'Apprentice']);
	}

	public function testSaveUpdateRecordArray(): void
	{
		$this->createModel(JobModel::class);

		$data = [
			'name'        => 'Apprentice',
			'description' => 'That thing you do.',
		];

		$result = $this->model->protect(false)->save($data);
		$this->seeInDatabase('job', ['name' => 'Apprentice']);
		$this->assertTrue($result);
	}

	public function testSaveUpdateRecordArrayFail(): void
	{
		$this->setPrivateProperty($this->db, 'DBDebug', false);
		$this->createModel(JobModel::class);

		$data = [
			'id'          => 1,
			'name123'     => 'Apprentice',
			'description' => 'That thing you do.',
		];

		$result = $this->model->protect(false)->save($data);
		$this->assertFalse($result);
		$this->dontSeeInDatabase('job', ['name' => 'Apprentice']);
	}

	public function testSaveUpdateRecordObject(): void
	{
		$this->createModel(JobModel::class);
		$data = new stdClass();

		// Sqlsrv does not allow forcing an ID into an autoincrement field.
		if ($this->db->DBDriver !== 'SQLSRV')
		{
			$data->id = 1;
		}

		$data->name        = 'Engineer';
		$data->description = 'A fancier term for Developer.';

		$result = $this->model->protect(false)->save($data);
		$this->seeInDatabase('job', ['name' => 'Engineer']);
		$this->assertTrue($result);
	}

	public function testSaveProtected(): void
	{
		$this->createModel(JobModel::class);

		$data               = new stdClass();
		$data->id           = 1;
		$data->name         = 'Engineer';
		$data->description  = 'A fancier term for Developer.';
		$data->random_thing = 'Something wicked'; // If not protected, this would kill the script.

		$result = $this->model->protect(true)->save($data);
		$this->assertTrue($result);
	}

	public function testSelectAndEntitiesSaveOnlyChangedValues(): void
	{
		$this->hasInDatabase('job', [
{'fraction_non_alphanumeric': 0.17997273140433268, 'fraction_numerical': 0.003332828359339494, 'mean_word_length': 4.178039215686274, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 143, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3614417', 'n_tokens_mistral': 2348, 'n_tokens_neox': 2147, 'n_words': 414}