Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7775909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:44:37+00:00 2026-06-01T17:44:37+00:00

I want to test one of my Model classes, so i have to insert,

  • 0

I want to test one of my Model classes, so i have to insert, update and delete data from my database in order to test if my methods work well.

I am working with a defined Test database where i have already some data.
To test all methos i use two roles, the admin one and the user one. So i get their data using the setUp method like this:

public function setUp() {
    parent::setUp();
    $this->User = ClassRegistry::init('User');

    $admin = $this->User->query("select * from users where admin = 1");
    $this->testUser['admin']['id'] = $admin[0]['users']['id'];
    $this->testUser['admin']['username'] = $admin[0]['users']['username'];
    $this->testUser['admin']['password'] = $admin[0]['users']['password'];
    $this->testUser['admin']['verified'] = $admin[0]['users']['verified'];
    $this->testUser['admin']['created'] = $admin[0]['users']['created'];
    $this->testUser['admin']['nick'] = $admin[0]['users']['nick'];
    $this->testUser['admin']['admin'] = $admin[0]['users']['admin'];

    $user = $this->User->query("select * from users where admin = 0 and verified = 0");

    $this->testUser['user']['id'] = $user[0]['users']['id'];
    $this->testUser['user']['username'] = $user[0]['users']['username'];
    $this->testUser['user']['password'] = $user[0]['users']['password'];
    $this->testUser['user']['verified'] = $user[0]['users']['verified'];
    $this->testUser['user']['created'] = $user[0]['users']['created'];
    $this->testUser['user']['nick'] = $user[0]['users']['nick'];
    $this->testUser['user']['admin'] = $user[0]['users']['admin'];

}

When i want to test methods like the “banAccess” one who moves data from the Users table to the bannedUsers table, then i have a problem because the Test won’t run well the next time as the user i selected for the Test won’t be in the same table.
It seems that setUP() and tearDown() methods are only executed once after all test methods are called.
This way, if the bannAccess test methods is executed before the testGetUserName method, for example, this last one will fail as the user is not on Users table.

For the moment i am testing the method and deleting the user after it in order to solve this problem but i am sure it have to be a better way to do it:

public function testBanAccess() {
    $result = $this->User->banAccess($this->testUser['user']['id'], 'spam', '42');
    $expected = true;

    $this->assertEquals($expected, $result);

    $this->User->query("delete from banUsers where id = ".$this->testUser['user']['id']);
}

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T17:44:38+00:00Added an answer on June 1, 2026 at 5:44 pm

    Your whole test setup is not good. You should use fixtures for that an have the records present in the fixtures. See http://book.cakephp.org/2.0/en/development/testing.html#fixtures

    setUp() and tearDown() are executed only one time while startTest() and endTest() are for each test*() method.

    Further you should not use query() because it is potentially unsafe because of SQL injections. The CakePHP ORM will take care of that if you would use it… To see query() present in the test make me think you’ve used it in the app to and built a pretty unsafe app.

    Also why do you have to copy users to another table instead simply flagging them as banned with a simple tinyint field?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two model classes, each is a table in a database. One model
I have one GSM Modem Series 900. I want to test it for sending
I have a model User and when I create one, I want to pragmatically
I want to have one model & view that is served by multiple controllers
I want to test in a programmatically way one rule generated from a tree.
I want to test if a type implements one of a set of interfaces.
I want to redirect one specific url like: www.example.com/test/ex.gif to static.example.com/ex.gif How can i
I have two MS SQL 2005 servers, one for production and one for test
I am building some test application with Neo4j. I want to model small social
I have a list of div's. This is one div structure: <div class=commentWrap id=@Model.CommentId>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.